1+ name : Release
2+ permissions :
3+ packages : write
4+ contents : write
5+ on :
6+ workflow_run :
7+ workflows : ["Build Container"]
8+ types :
9+ - completed
10+ branches :
11+ - main
12+ - master
13+ workflow_dispatch :
14+
15+ env :
16+ DOCKER_BUILDKIT : 1
17+ KAMAL_REGISTRY_PASSWORD : ${{ secrets.GITHUB_TOKEN }}
18+ KAMAL_REGISTRY_USERNAME : ${{ github.actor }}
19+
20+ jobs :
21+ release :
22+ runs-on : ubuntu-latest
23+ if : ${{ github.event.workflow_run.conclusion == 'success' }}
24+ steps :
25+ - name : Checkout code
26+ uses : actions/checkout@v5
27+
28+ - name : Set up environment variables
29+ run : |
30+ echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
31+ echo "repository_name=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_ENV
32+ echo "repository_name_lower=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
33+ echo "org_name=$(echo ${{ github.repository }} | cut -d '/' -f 1)" >> $GITHUB_ENV
34+ if find . -maxdepth 2 -type f -name "Configure.Db.Migrations.cs" | grep -q .; then
35+ echo "HAS_MIGRATIONS=true" >> $GITHUB_ENV
36+ else
37+ echo "HAS_MIGRATIONS=false" >> $GITHUB_ENV
38+ fi
39+
40+ # This step is for the deployment of the templates only, safe to delete
41+ - name : Modify deploy.yml
42+ env :
43+ KAMAL_DEPLOY_IP : ${{ secrets.KAMAL_DEPLOY_IP }}
44+ if : env.KAMAL_DEPLOY_IP != null
45+ run : |
46+ sed -i "s/service: my-app/service: ${{ env.repository_name_lower }}/g" config/deploy.yml
47+ sed -i "s#image: my-user/myapp#image: ${{ env.image_repository_name }}#g" config/deploy.yml
48+ sed -i "s/- 192.168.0.1/- ${{ secrets.KAMAL_DEPLOY_IP }}/g" config/deploy.yml
49+ sed -i "s/host: my-app.example.com/host: ${{ secrets.KAMAL_DEPLOY_HOST }}/g" config/deploy.yml
50+ sed -i "s/MyApp/${{ env.repository_name }}/g" config/deploy.yml
51+
52+ - name : Login to GitHub Container Registry
53+ uses : docker/login-action@v3
54+ with :
55+ registry : ghcr.io
56+ username : ${{ env.KAMAL_REGISTRY_USERNAME }}
57+ password : ${{ env.KAMAL_REGISTRY_PASSWORD }}
58+
59+ - name : Set up SSH key
60+ uses :
webfactory/[email protected] 61+ with :
62+ ssh-private-key : ${{ secrets.SSH_PRIVATE_KEY }}
63+
64+ - name : Setup Ruby
65+ uses : ruby/setup-ruby@v1
66+ with :
67+ ruby-version : 3.3.0
68+ bundler-cache : true
69+
70+ - name : Install Kamal
71+ run : gem install kamal -v 2.3.0
72+
73+ - name : Set up Docker Buildx
74+ uses : docker/setup-buildx-action@v3
75+ with :
76+ driver-opts : image=moby/buildkit:master
77+
78+ - name : Kamal bootstrap
79+ run : |
80+ kamal server bootstrap
81+
82+ - name : Check if first run and execute kamal app boot if necessary
83+ run : |
84+ FIRST_RUN_FILE=".${{ env.repository_name }}"
85+ if ! kamal server exec --no-interactive -q "test -f $FIRST_RUN_FILE"; then
86+ kamal server exec --no-interactive -q "touch $FIRST_RUN_FILE" || true
87+ kamal deploy -q -P --version latest || true
88+ else
89+ echo "Not first run, skipping kamal app boot"
90+ fi
91+
92+ - name : Ensure file permissions
93+ run : |
94+ kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data && chown -R 1654:1654 /opt/docker/${{ env.repository_name }}"
95+
96+ - name : Migration
97+ if : env.HAS_MIGRATIONS == 'true'
98+ run : |
99+ kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
100+ kamal server exec --no-interactive "docker pull ghcr.io/${{ env.image_repository_name }}:latest || true"
101+ kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate"
102+
103+ - name : Deploy with Kamal
104+ run : |
105+ kamal lock release -v
106+ kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
107+ kamal deploy -P --version latest
0 commit comments