-
Notifications
You must be signed in to change notification settings - Fork 0
215 lines (209 loc) · 9.03 KB
/
rest.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
name: rest
on:
push:
branches:
- master
- develop
paths:
- "cmd/rest/**"
- ".github/workflows/rest.yml"
pull_request:
paths:
- "cmd/rest/**"
- ".github/workflows/rest.yml"
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
environment_push:
if: github.event_name == 'push'
runs-on: ubuntu-latest
env:
GITHUB_SHA: ${{ github.sha }}
GITHUB_REF: ${{ github.ref }}
steps:
- name: Get git source
run: |
git_source=`echo $GITHUB_REF | sed 's/refs\/heads\///'`
git_source=`echo $git_source | sed 's/refs\/tags\///'`
git_source=`echo $git_source | sed 's/refs\\///'`
echo $git_source
echo "git_source=$git_source" >> $GITHUB_ENV
- name: Get git SHA
run: |
git_sha=`echo $GITHUB_SHA`
echo $git_sha
echo "git_sha=$git_sha" >> $GITHUB_ENV
- name: Checkout repository code
uses: actions/checkout@v2
- name: Get version
run: |
version_source=`cat /home/runner/work/go-hello-world/go-hello-world/version.json | jq '.Version' | sed 's/"//g'`
echo $version_source
echo "version_source=$version_source" >> $GITHUB_ENV
- name: Generate build artifacts
run: |
mkdir -p /tmp
echo ${{ env.version_source }} >> /tmp/version_source
echo ${{ env.git_source }} >> /tmp/git_source
echo ${{ env.git_sha }} >> /tmp/git_sha
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: artifacts
path: |
/tmp/version_source
/tmp/git_source
/tmp/git_sha
environment_pull_request:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
env:
GITHUB_SHA: ${{ github.event.pull_request.head.sha }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
GITHUB_BASE_REF: ${{ github.base_ref }}
steps:
- name: Get git target
run: |
git_target=`echo $GITHUB_BASE_REF | sed 's/refs\/heads\///'`
git_target=`echo $git_target | sed 's/refs\/tags\///'`
git_target=`echo $git_target| sed 's/refs\\///'`
echo $git_target
echo "git_target=$git_target" >> $GITHUB_ENV
- name: Get git source
run: |
git_source=`echo $GITHUB_HEAD_REF | sed 's/refs\/heads\///'`
git_source=`echo $git_source | sed 's/refs\/tags\///'`
git_source=`echo $git_source | sed 's/refs\\///'`
echo $git_source
echo "git_source=$git_source" >> $GITHUB_ENV
- name: Get git SHA
run: |
git_sha=`echo $GITHUB_SHA`
echo $git_sha
echo "git_sha=$git_sha" >> $GITHUB_ENV
- name: Checkout repository code (source)
uses: actions/checkout@v2
- name: Get source version
run: |
version_source=`cat /home/runner/work/go-hello-world/go-hello-world/version.json | jq '.Version' | sed 's/"//g'`
echo $version_source
echo "version_source=$version_source" >> $GITHUB_ENV
- name: Checkout repository code (target)
uses: actions/checkout@v2
with:
ref: ${{ github.base_ref }}
- name: Get target version
run: |
version_target=`cat /home/runner/work/go-hello-world/go-hello-world/version.json | jq '.Version' | sed 's/"//g'`
echo $version_target
echo "version_target=$version_target" >> $GITHUB_ENV
- name: Generate build artifacts
run: |
mkdir -p /tmp
echo ${{ env.version_source }} >> /tmp/version_source
echo ${{ env.version_target }} >> /tmp/version_target
echo ${{ env.git_target }} >> /tmp/git_target
echo ${{ env.git_source }} >> /tmp/git_source
echo ${{ env.git_sha }} >> /tmp/git_sha
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: artifacts
path: |
/tmp/version_source
/tmp/version_target
/tmp/git_target
/tmp/git_source
/tmp/git_sha
build_images:
if: ${{ always() && (needs.environment_push.result=='success' || needs.environment_pull_request.result=='success') }}
needs: [environment_push, environment_pull_request]
runs-on: ubuntu-latest
env:
PLATFORM_AMD64: linux/amd64
PLATFORM_ARMV7: linux/arm/v7
permissions:
contents: read
steps:
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: artifacts
path: /tmp
- name: Get environment
run: |
version=`cat /tmp/version_source`
echo "version=$version" >> $GITHUB_ENV
git_source=`cat /tmp/git_source`
echo "git_source=$git_source" >> $GITHUB_ENV
- name: Check out repository code
uses: actions/checkout@v2
- name: Build docker images
run: |
docker build -f ./cmd/rest/Dockerfile . -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_${{ env.version }} --build-arg GIT_COMMIT=$GITHUB_SHA --build-arg GIT_BRANCH=${{ env.git_source }} --build-arg PLATFORM=$PLATFORM_AMD64 --build-arg GO_ARCH=amd64
docker build -f ./cmd/rest/Dockerfile . -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }} --build-arg GIT_COMMIT=$GITHUB_SHA --build-arg GIT_BRANCH=${{ env.git_source }} --build-arg PLATFORM=$PLATFORM_ARMV7 --build-arg GO_ARCH=arm --build-arg GO_ARM=7
- name: Generate build artifacts
run: |
mkdir -p /tmp
docker save -o /tmp/go-hello-world-rest_amd64-${{ env.version }}.tar.gz ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_${{ env.version }}
docker save -o /tmp/go-hello-world-rest_armv7-${{ env.version }}.tar.gz ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }}
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: artifacts
path: |
/tmp/go-hello-world-rest_amd64-${{ env.version }}.tar.gz
/tmp/go-hello-world-rest_armv7-${{ env.version }}.tar.gz
retention-days: 1
- name: Docker clean-up
run: |
docker image prune -f
docker container prune -f
push_images:
if: ${{ always() && needs.build_images.result=='success' &&
(github.event_name == 'release' || github.ref == 'refs/heads/master') }}
runs-on: ubuntu-latest
permissions:
packages: write
needs: [build_images]
steps:
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: artifacts
path: /tmp
- name: Get environment
run: |
version=`cat /tmp/version_source`
echo "version=$version" >> $GITHUB_ENV
- name: Login to ghcr.io
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Load docker images
run: |
docker load -i /tmp/go-hello-world-rest_amd64-${{ env.version }}.tar.gz
docker load -i /tmp/go-hello-world-rest_armv7-${{ env.version }}.tar.gz
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_${{ env.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_latest
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_latest
- name: Push docker images
run: |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_${{ env.version }}
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_latest
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }}
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_latest
- name: Create/Push docker manifest
run: |
docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:${{ env.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_${{ env.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }}
docker manifest annotate --arch arm --os linux --variant 7 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:${{ env.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_${{ env.version }}
docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:${{ env.version }} --purge
docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:amd64_latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_latest
docker manifest annotate --arch arm --os linux --variant 7 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:armv7_latest
docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-rest:latest --purge
- name: Docker clean-up
run: |
docker image prune -f
docker container prune -f