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