This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
160 lines (139 loc) · 4.79 KB
/
manual-build-push-deploy.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
name: Build, push and deploy service
on:
workflow_dispatch:
inputs:
client:
description: client
type: boolean
server:
description: server
type: boolean
load-balancer:
description: load-balancer
type: boolean
deploy-env:
description: Environment
type: choice
options:
- stage
- prod
build:
description: Build services
type: boolean
default: true
deploy:
description: Deploy services
type: boolean
default: true
jobs:
generate-matrix:
runs-on: ubuntu-latest
name: Generate service matrix
outputs:
matrix: ${{ steps.generate-service-matrix.outputs.matrix }}
steps:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18.16.0
- name: Generate service matrix
id: generate-service-matrix
run: |
JSON='${{ toJSON(inputs) }}'
SERVICES=client,server,load-balancer
MATRIX_ARRAY=$(node -e "const services = '$SERVICES'.split(',');console.log(Object.entries($JSON).filter((e) => services.includes(e[0]) && Boolean(e[1])).map((e) => e[0]))")
echo ::set-output name=matrix::${MATRIX_ARRAY}
build-and-push:
runs-on: ubuntu-latest
name: Build and push ${{ matrix.service }} (${{ inputs.deploy-env }})
needs:
- generate-matrix
if: inputs.build
strategy:
matrix:
service: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ secrets.PACKAGES_USERNAME }}
password: ${{ secrets.PACKAGES_TOKEN }}
- name: Get package.json version
id: pkgversion
run: |
PKG_VERSION=$(jq -r ".version" "apps/${{ matrix.service }}/package.json")
echo "$PKG_VERSION"
echo "::set-output name=PKG_VERSION::$PKG_VERSION"
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}/${{ matrix.service }}-${{ inputs.deploy-env }}
tags: |
type=semver,pattern={{version}},value=${{ steps.pkgversion.outputs.PKG_VERSION }}
- name: Build and push ${{ matrix.service }}
uses: docker/build-push-action@v3
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: deploy/dockerfiles/Dockerfile.${{ matrix.service }}
platforms: linux/amd64
target: runner-prod
build-args: |
ENV=${{ inputs.deploy-env }}
deploy:
runs-on: ubuntu-latest
name: Deploy ${{ matrix.service }} (${{ inputs.deploy-env }})
needs:
- generate-matrix
- build-and-push
if: |
always() &&
inputs.deploy &&
(needs.build-and-push.result == 'success' || needs.build-and-push.result == 'skipped')
strategy:
matrix:
service: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Get package.json version
id: pkgversion
run: |
PKG_VERSION=$(jq -r ".version" "apps/${{ matrix.service }}/package.json")
echo "$PKG_VERSION"
echo "::set-output name=PKG_VERSION::$PKG_VERSION"
- name: Deploy ${{ matrix.service }}
uses: appleboy/[email protected]
env:
REPOSITORY: ${{ github.repository }}
APP_PATH: /app
SOURCE_DIR: source-${{ matrix.service }}-${{ inputs.deploy-env }}
SERVERS_TO_START: 2
DEPLOY_ENV: ${{ inputs.deploy-env }}
VERSION: ${{ steps.pkgversion.outputs.PKG_VERSION }}
UPDATE_TOKEN: ${{ secrets.UPDATE_TOKEN }}
with:
host: ${{ secrets.PROD_HOST }}
username: ${{ secrets.PROD_USERNAME }}
password: ${{ secrets.PROD_PASSWORD }}
envs: REPOSITORY,APP_PATH,SOURCE_DIR,SERVERS_TO_START,DEPLOY_ENV,VERSION,UPDATE_TOKEN
script: |
git clone --single-branch --branch ${{ github.ref_name }} https://github.com/$REPOSITORY.git $APP_PATH/$SOURCE_DIR
$APP_PATH/$SOURCE_DIR/deploy/scripts/update-${{ matrix.service }}.sh
purge-cache:
needs:
- deploy
if: (inputs.client && inputs.deploy)
name: Purge cloudflare cache
runs-on: ubuntu-latest
steps:
- name: Purge cloudflare cache
uses: nathanvaughn/[email protected]
with:
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}