Migrate app from Heroku.com to Fly.io
I saw the other day that fly.io might be a good alternative to heroku in a discussion linked from Hackernews. So I decided to move the heroku deployment of biokiste there.
I tried to follow their easy to follow documentation, made one mistake, searched a bit here, and there and finally got it. Here you find a walkthrough how I deployed my app on fly.io. Read it first and act after that.
Other people might not deploy the app right at the first usage of
fly launch
and rather do the first launch after fixing
app secrets.
I added ...
where appropriate.
Sign up to fly.io
root@somename:/home/username# flyctl auth signup
failed opening browser. Copy the url (https://fly.io/app/auth/cli/5c4c6f8f88b071017781955537b91fcd) into a browser and continue
Opening https://fly.io/app/auth/cli/5c4c6f8f88b071017781955537b91fcd ...
Waiting for session... Done
successfully logged in as user.name@somedoma.in
Change to project folder
root@somename:/home/username# cd IdeaProjects/biokiste/
Create project on fly.io
root@somename:/home/username/IdeaProjects/biokiste# fly launch
Creating app in /home/username/IdeaProjects/biokiste
Scanning source code
Detected a Dockerfile app
? App Name (leave blank to use an auto-generated name): biokiste
Automatically selected personal organization: User Name
? Select region: fra (Frankfurt, Germany)
Created app biokiste in organization personal
Wrote config file fly.toml
? Would you like to set up a Postgresql database now? No
? Would you like to deploy now? Yes
Deploying biokiste
==> Validating app configuration
--> Validating app configuration done
Services
TCP 80/443 ⇢ 8080
Remote builder fly-builder-bold-moon-1658 ready
==> Creating build context
--> Creating build context done
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
[+] Building 50.4s (0/1)
[+] Building 19.5s (6/6) FINISHED
=> [internal] load remote build context 0.0s
=> copy /context / 12.6s
=> [internal] load metadata for docker.io/library/openjdk:17 1.1s
=> [1/2] FROM docker.io/library/openjdk:17@sha256:528707081fdb9562 4.7s
=> => resolve docker.io/library/openjdk:17@sha256:528707081fdb9562 0.0s
...
=> => extracting sha256:a7203ca35e75e068651c9907d659adc721dba82344 2.0s
=> [2/2] ADD backend/target/app.jar app.jar 0.2s
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:2b126209d6cb8a5fe4250b64e2690a9643bcad1 0.0s
=> => naming to registry.fly.io/biokiste:deployment-01GF39ZWFQ5NWC 0.0s
--> Building image done
==> Pushing image to fly
The push refers to repository [registry.fly.io/biokiste]
f5b4b49fbb03: Pushed
dc9fa3d8b576: Pushed
27ee19dc88f2: Pushed
c8dd97366670: Pushed
deployment-01GF39ZWFQ5NWC4CGN1RV8R1ZN: digest: sha256:1d05955caf98351498a2d8422c087e5af192f8be2aa84181797cc54f843d86ed size: 1166
--> Pushing image done
Image: registry.fly.io/biokiste:deployment-01GF39ZWFQ5NWC4CGN1RV8R1ZN
Image size: 511 MB
==> Creating release
Release v2 created
You can detach the terminal anytime without stopping the deployment
Monitoring Deployment
1 desired, 1 placed, 0 healthy, 0 unhealthy [restarts: 1] [health checks: 1 desired, 1 placed, 0 healthy, 0 unhealthy [restarts: 2] [health checks: 1 desired, 1 placed, 0 healthy, 0 unhealthy [restarts: 2] [health checks: 1 desired, 1 placed, 0 healthy, 1 unhealthy [restarts: 2] [health checks: 1 total]
v0 failed - Failed due to unhealthy allocations - no stable job version to auto revert to
Failed Instances
==> Failure #1
Instance
ID = 3f0297fd
Process = app
Version = 0
Region = fra
Desired = run
Status = running
Health Checks = 1 total
Restarts = 2
Created = 43s ago
Recent Events
TIMESTAMP TYPE MESSAGE
2022-10-11T10:40:41Z Received Task received by client
2022-10-11T10:40:41Z Task Setup Building Task Directory
2022-10-11T10:41:01Z Started Task started by client
2022-10-11T10:41:09Z Terminated Exit Code: 1
2022-10-11T10:41:09Z Restarting Task restarting in 1.000913253s
2022-10-11T10:41:16Z Started Task started by client
2022-10-11T10:41:22Z Terminated Exit Code: 1
2022-10-11T10:41:22Z Restarting Task restarting in 1.016588055s
2022-10-11T10:41:29Z Started Task started by client
Recent Logs
2022-10-11T10:41:35.000 [info] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.19.jar!/:5.3.19]
...
2022-10-11T10:41:35.000 [info] ... 94 common frames omitted
2022-10-11T10:41:35.000 [info] Starting clean up.
***v0 failed - Failed due to unhealthy allocations - no stable job version to auto revert to and deploying as v1
Troubleshooting guide at https://fly.io/docs/getting-started/troubleshooting/
Error abort
Status of app
root@somename:/home/username/Development/IdeaProjects/biokiste# flyctl status -a biokiste
App
Name = biokiste
Owner = personal
Version = 3
Status = running
Hostname = biokiste.fly.dev
Platform = nomad
Instances
ID PROCESS VERSION REGION DESIRED STATUS HEALTH CHECKS RESTARTS CREATED
850c94fb app 3 fra run running 1 total, 1 passing 0 2022-10-11T10:48:48Z
Setting secrets
Go to https://dashboard.heroku.com/apps/biokiste/settings
and note the secrets you gave your app. You get them under Config Vars
.These secrets have to be configured also in fly.io.
Set fly.io secrets
root@somename:/home/username/Development/IdeaProjects/biokiste# flyctl secrets set JWTSECRET <somesecretsecret> OTHER_SECRET <other secret>
Error Could not find App
root@somename:/home/username/Development/IdeaProjects/biokiste# flyctl secrets list -a biokiste
NAME DIGEST CREATED AT
EMAIL_PASS 880da3cc314d7b48 23m16s ago
EMAIL_USERNAME 0e180a420d474bb4 23m16s ago
JWTSECRET 880da3cc314d7b48 23m16s ago
MONGO_DB_URI ad51cebe16c92201 2022-10-11T10:48:26Z
SMTP_SERVER d910184be71bdfeb 2m17s ago
Github Actions
At project creation a toml file was created. We worked in branches, so make a new branch and add this toml to version control. Change the DeployToHeroku.yml
so that a fly.io API key is used to deploy there. From
name: Deploy to Heroku
on:
push:
branches:
- main
jobs:
build-frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: '14'
- name: Build Frontend
run: |
cd web-frontend
npm install
npm run build
- uses: actions/upload-artifact@v2
with:
name: web-frontend-build
path: web-frontend/build/
build-backend:
runs-on: ubuntu-latest
needs: build-frontend
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
- uses: actions/download-artifact@v2
with:
name: web-frontend-build
path: backend/src/main/resources/static
- name: Build with maven
env:
EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME}}
EMAIL_PASS: ${{ secrets.EMAIL_PASS }}
SMTP_SERVER: ${{ secrets.SMTP_SERVER }}
run: mvn -B package --file backend/pom.xml
- uses: actions/upload-artifact@v2
with:
name: app.jar
path: backend/target/app.jar
deploy:
runs-on: ubuntu-latest
needs: build-backend
environment:
name: production-heroku
url: https://biokiste.herokuapp.com/
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v2
with:
name: app.jar
path: backend/target
- name: Login to Heroku
run: heroku container:login
- name: Push Docker Container
run: heroku container:push web -a biokiste
- name: Release Docker Container
run: heroku container:release web -a biokiste
to a renamed file DeployToFly.yml
with
name: Deploy to Fly.io
on:
push:
branches:
- main
jobs:
build-frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: '14'
- name: Build Frontend
run: |
cd web-frontend
npm install
npm run build
- uses: actions/upload-artifact@v2
with:
name: web-frontend-build
path: web-frontend/build/
build-backend:
runs-on: ubuntu-latest
needs: build-frontend
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
- uses: actions/download-artifact@v2
with:
name: web-frontend-build
path: backend/src/main/resources/static
- name: Build with maven
env:
EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME}}
EMAIL_PASS: ${{ secrets.EMAIL_PASS }}
SMTP_SERVER: ${{ secrets.SMTP_SERVER }}
run: mvn -B package --file backend/pom.xml
- uses: actions/upload-artifact@v2
with:
name: app.jar
path: backend/target/app.jar
deploy:
runs-on: ubuntu-latest
needs: build-backend
environment:
name: production-fly
url: https://biokiste.fly.dev/
env:
FLY_API_TOKEN: ${{ secrets.FLY_IO_API_KEY }}
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- uses: actions/download-artifact@v2
with:
name: app.jar
path: backend/target
- run: flyctl deploy --remote-only