-
Notifications
You must be signed in to change notification settings - Fork 6
197 lines (174 loc) · 6.3 KB
/
Copy pathdeploy-api.yml
File metadata and controls
197 lines (174 loc) · 6.3 KB
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
194
195
196
197
name: Deploy API
on:
workflow_dispatch:
inputs:
image_tag:
description: "Image tag (defaults to short git SHA)"
required: false
type: string
workflow_call:
inputs:
image_tag:
description: "Image tag (defaults to short git SHA)"
required: false
type: string
env:
REGISTRY_ALIAS: m8q5m4u3
REPOSITORY: mega/campsite-api
HARBOR_REGISTRY: registry.xuanwu.openatom.cn
RUBY_VERSION: 3.3.4
NODE_VERSION: 18.16.1
BUNDLER_VERSION: 2.3.14
permissions:
id-token: write
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.meta.outputs.image_tag }}
steps:
- name: Set image tag
id: meta
run: |
if [ -n "${{ inputs.image_tag }}" ]; then
IMAGE_TAG="${{ inputs.image_tag }}"
else
IMAGE_TAG="${GITHUB_SHA::7}"
fi
echo "image_tag=${IMAGE_TAG}" >> "$GITHUB_OUTPUT"
echo "Using image tag: ${IMAGE_TAG}"
build-push-amd64:
needs: prepare
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR Public
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
- name: Login to Harbor
uses: docker/login-action@v3
with:
registry: ${{ env.HARBOR_REGISTRY }}
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
- name: Build & push amd64 image (AWS + Harbor)
env:
AWS_REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
IMAGE_TAG: ${{ needs.prepare.outputs.image_tag }}
run: |
set -euo pipefail
AWS_IMAGE_BASE="$AWS_REGISTRY/${{ env.REGISTRY_ALIAS }}/${{ env.REPOSITORY }}"
HARBOR_IMAGE_BASE="${{ env.HARBOR_REGISTRY }}/${{ env.REPOSITORY }}"
docker buildx build \
--platform linux/amd64 \
--provenance=false \
--sbom=false \
--build-arg RUBY_VERSION=${{ env.RUBY_VERSION }} \
--build-arg NODE_VERSION=${{ env.NODE_VERSION }} \
--build-arg BUNDLER_VERSION=${{ env.BUNDLER_VERSION }} \
-f ./api/Dockerfile \
-t "$AWS_IMAGE_BASE:${IMAGE_TAG}" \
-t "$AWS_IMAGE_BASE:${IMAGE_TAG}-amd64" \
-t "$AWS_IMAGE_BASE:latest-amd64" \
-t "$HARBOR_IMAGE_BASE:${IMAGE_TAG}" \
-t "$HARBOR_IMAGE_BASE:${IMAGE_TAG}-amd64" \
-t "$HARBOR_IMAGE_BASE:latest-amd64" \
--push ./api
# Merge locally-built arm64 image (pushed separately) with CI-built amd64
manifest:
needs: [prepare, build-push-amd64]
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR Public
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
- name: Login to Harbor
uses: docker/login-action@v3
with:
registry: ${{ env.HARBOR_REGISTRY }}
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
- name: Create & push manifest lists
env:
REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
IMAGE_TAG: ${{ needs.prepare.outputs.image_tag }}
run: |
set -euo pipefail
IMAGE_BASE="$REGISTRY/${{ env.REGISTRY_ALIAS }}/${{ env.REPOSITORY }}"
HARBOR_BASE="${{ env.HARBOR_REGISTRY }}/${{ env.REPOSITORY }}"
push_latest() {
local base="$1"
local refs=("$base:latest-amd64")
if docker manifest inspect "$base:latest-arm64" >/dev/null 2>&1; then
refs+=("$base:latest-arm64")
else
echo "WARN: $base:latest-arm64 not found; publishing amd64-only latest"
fi
docker manifest create "$base:latest" "${refs[@]}"
docker manifest push "$base:latest"
echo "Pushed manifest: $base:latest"
}
push_tag_if_arm64() {
local base="$1"
if docker manifest inspect "$base:${IMAGE_TAG}-arm64" >/dev/null 2>&1; then
docker buildx imagetools create \
-t "$base:${IMAGE_TAG}" \
"$base:${IMAGE_TAG}-amd64" \
"$base:${IMAGE_TAG}-arm64"
echo "Pushed multi-arch manifest: $base:${IMAGE_TAG}"
else
echo "Using amd64-only tag: $base:${IMAGE_TAG}"
echo "Push arm64 locally to upgrade to multi-arch: ./script/demo/build-images-arm64-local.sh ${IMAGE_TAG} --push"
fi
}
push_latest "$IMAGE_BASE"
push_tag_if_arm64 "$IMAGE_BASE"
push_latest "$HARBOR_BASE"
push_tag_if_arm64 "$HARBOR_BASE"
deploy-aws:
if: ${{ github.repository == 'web3infra-foundation/campsite' }}
needs: manifest
runs-on: ubuntu-latest
strategy:
matrix:
include:
- cluster: gitmega-com
service: campsite-api-staging-service-njx3hjyx
- cluster: gitmono-com-mega-app
service: campsite-api-service
steps:
- name: Force ECS redeploy
run: |
aws ecs update-service \
--cluster ${{ matrix.cluster }} \
--service ${{ matrix.service }} \
--force-new-deployment
env:
AWS_REGION: ap-southeast-2
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}