-
Notifications
You must be signed in to change notification settings - Fork 0
193 lines (169 loc) · 6.37 KB
/
release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
name: Release Deployment
on:
push:
tags:
- "v*"
permissions:
contents: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
check-ci:
name: Check CI
uses: ./.github/workflows/ci.yml
push-images:
name: Push Docker image to Docker Hub and Github Packages
runs-on: ubuntu-latest
needs: check-ci
permissions:
packages: write
contents: read
attestations: write
id-token: write
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set Commit Env from git command
run: |
echo GIT_COMMIT_TIME=$(git log -1 --format="%ct") >> $GITHUB_ENV
echo GIT_COMMIT_ID=$(git log -1 --format="%H") >> $GITHUB_ENV
- name: Inspect Commit Env
run: |
echo GIT_COMMIT_ID=$GIT_COMMIT_ID vs ${{ env.GIT_COMMIT_ID }} in env context
echo GIT_COMMIT_TIME=$GIT_COMMIT_TIME vs ${{ env.GIT_COMMIT_TIME }} in env context
# https://github.com/docker/login-action
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# https://github.com/docker/metadata-action
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
cao7113/hello-api-elixir
ghcr.io/${{ github.repository }}
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# https://github.com/docker/build-push-action?tab=readme-ov-file#customizing
# GIT_COMMIT_TIME=$(git log -1 --format="%ct")
build-args: |
GIT_COMMIT_ID=${{ github.sha }}
GIT_COMMIT_TIME=${{ env.GIT_COMMIT_TIME }}
# https://docs.github.com/en/actions/publishing-packages/publishing-docker-images#publishing-images-to-docker-hub
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
test-docker-images:
name: Test latest docker images
needs: push-images
uses: ./.github/workflows/docker.yml
fly-deploy:
name: Deploy fly app
runs-on: ubuntu-latest
needs: test-docker-images
concurrency: deploy-group
steps:
- name: Checkout code
uses: actions/checkout@v4
# https://github.com/superfly/flyctl-actions
# https://github.com/marketplace/actions/github-action-for-flyctl
- name: Setup flyctl
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy fly app
run: flyctl deploy --image ghcr.io/${{ github.repository }}:${{ github.ref_name }} --debug --verbose
env:
# https://github.com/superfly/flyctl-actions?tab=readme-ov-file#secrets
# https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/#speed-run-your-way-to-continuous-deployment
# https://github.com/cao7113/hello-phx/settings/secrets/actions
# gen cmd: fly tokens create deploy -x 999999h
# set in Github Settings -> Secrets -> Actions -> Repository secrets
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
release-tar:
# strategy:
# matrix:
# # erlef/setup-beam only supports Ubuntu and Windows at this time
# os: [ubuntu-latest]
# otp: [27.x]
# elixir: [1.17.x]
name: Publish mix release tar
runs-on: ubuntu-latest
needs: check-ci
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
version-file: .tool-versions
version-type: strict
- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-
- name: Install dependencies
run: mix deps.get --only prod
- name: Make mix release
run: |
export GIT_COMMIT_ID=${{ github.sha }}
# export GIT_COMMIT_ID=$(git log -1 --format="%H")
export GIT_COMMIT_TIME=$(git log -1 --format="%ct")
MIX_ENV=prod RELEASE_TAR=1 mix release
# https://github.com/softprops/action-gh-release
- name: Make Github release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
# todo: add runner.os & runner.arch info in final tar filename
with:
files: _build/prod/*.tar.gz
# ssh-deploy:
# name: Deoloy to remote server by ssh
# runs-on: ubuntu-latest
# needs: release-tar
# steps:
# # https://github.com/appleboy/ssh-action
# # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
# - name: executing remote ssh commands
# uses: appleboy/[email protected]
# with:
# host: ${{ secrets.REMOTE_HOST }}
# username: ${{ secrets.REMOTE_USERNAME }}
# key: ${{ secrets.REMOTE_PRIVKEY }}
# port: 22
# script: |
# vsn=${{ github.ref_name }}
# git_repo=${{ github.repository }}
# app_root=~/hello-api-elixir
# script_path=${app_root}/deploy.sh
# mkdir -p $app_root
# script_url="https://raw.githubusercontent.com/${git_repo}/main/run/deploy.sh"
# if [ ! -e $script_path ]; then
# wget -q -O $script_path $script_url
# chmod +x $script_path
# echo "Setup deploy-script into ${script_path} from ${script_url}"
# else
# echo "Use existed deploy-script: ${script_path}, manually delete it when need update"
# fi
# $script_path $vsn bin/hello_api ${git_repo} 3