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@v3
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+ if [ -n "${{ secrets.KAMAL_DEPLOY_IP }}" ]; then
40+ echo "HAS_DEPLOY_ACTION=true" >> $GITHUB_ENV
41+ else
42+ echo "HAS_DEPLOY_ACTION=false" >> $GITHUB_ENV
43+ fi
44+
45+ # This step is for the deployment of the templates only, safe to delete
46+ - name : Modify deploy.yml
47+ if : env.HAS_DEPLOY_ACTION == 'true'
48+ run : |
49+ sed -i "s/service: ssg-examples/service: ${{ env.repository_name_lower }}/g" config/deploy.yml
50+ sed -i "s#image: my-user/ssgexamples#image: ${{ env.image_repository_name }}#g" config/deploy.yml
51+ sed -i "s/- 192.168.0.1/- ${{ secrets.KAMAL_DEPLOY_IP }}/g" config/deploy.yml
52+ sed -i "s/host: ssg-examples.example.com/host: ${{ secrets.KAMAL_DEPLOY_HOST }}/g" config/deploy.yml
53+ sed -i "s/SsgExamples/${{ env.repository_name }}/g" config/deploy.yml
54+
55+ - name : Login to GitHub Container Registry
56+ uses : docker/login-action@v3
57+ with :
58+ registry : ghcr.io
59+ username : ${{ env.KAMAL_REGISTRY_USERNAME }}
60+ password : ${{ env.KAMAL_REGISTRY_PASSWORD }}
61+
62+ - name : Set up SSH key
63+ uses : webfactory/ssh-agent@v0.9.0
64+ with :
65+ ssh-private-key : ${{ secrets.SSH_PRIVATE_KEY }}
66+
67+ - name : Setup Ruby
68+ uses : ruby/setup-ruby@v1
69+ with :
70+ ruby-version : 3.3.0
71+ bundler-cache : true
72+
73+ - name : Install Kamal
74+ run : gem install kamal -v 2.3.0
75+
76+ - name : Set up Docker Buildx
77+ uses : docker/setup-buildx-action@v3
78+ with :
79+ driver-opts : image=moby/buildkit:master
80+
81+ - name : Kamal bootstrap
82+ run : kamal server bootstrap
83+
84+ - name : Check if first run and execute kamal app boot if necessary
85+ run : |
86+ FIRST_RUN_FILE=".${{ env.repository_name }}"
87+ if ! kamal server exec --no-interactive -q "test -f $FIRST_RUN_FILE"; then
88+ kamal server exec --no-interactive -q "touch $FIRST_RUN_FILE" || true
89+ kamal deploy -q -P --version latest || true
90+ else
91+ echo "Not first run, skipping kamal app boot"
92+ fi
93+
94+ - name : Ensure file permissions
95+ run : |
96+ kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data && chown -R 1654:1654 /opt/docker/${{ env.repository_name }}"
97+
98+ - name : Migration
99+ if : env.HAS_MIGRATIONS == 'true'
100+ run : |
101+ kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
102+ kamal server exec --no-interactive "docker pull ghcr.io/${{ env.image_repository_name }}:latest || true"
103+ kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate"
104+
105+ - name : Deploy with Kamal
106+ run : |
107+ kamal lock release -v
108+ kamal deploy -P --version latest
0 commit comments