-
Notifications
You must be signed in to change notification settings - Fork 0
272 lines (215 loc) · 9.39 KB
/
build-and-test.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# build-and-test.yml
# Date started: 30th March 2021
# Date of last update: 31st March 2021
# Purpose:
# Attempts to deploy smart-infrastructure codebase to TUM's Duckie-Town LIFX
# bulbs on push/pull-request to trunk
# By: Ameena Hassan and Julian Christl
# Shout outs to:
# Paul Schmiedmayer
# Hialus && He Xiaoning
# Developed also by discussions with:
# Timor Morrien
# Ivan Procaccini
# Related documentation can be found at:
# - https://docs.docker.com/engine/reference/commandline/images/
# - https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/
#
# >> Ping Ameena if we forgot you!
#
name: Build and Test # We should make this more accurate, but later on.
# Specifically mentions when to trigger the pipeline #
on:
push:
branches:
- trunk
pull_request:
branches:
- trunk
# Used to store global variables, adapt as necessary. #
env:
# The name of our Docker image to deploy
IMAGE_NAME: jass-smart-infrastructure-webservice
# Jobs the pipeline is supposed to run #
jobs:
# Job # 1: #
# test_build:
# name: Linux test build
#
# # Setup build environment #
# container:
# image: swift:focal
# runs-on: ubuntu-latest
#
# steps:
# - uses: actions/checkout@v2
#
# # Dependency #
# - name: Install libsqlite3
# run: apt-get update && apt-get install -y --no-install-recommends libsqlite3-dev
#
# - uses: actions/cache@v2
# # Caching to make subsequent deployment faster #
# with:
# path: .build
# key: ${{ runner.os }}-focal-spm-cache-${{ hashFiles('Package.resolved') }}
#
# - name: Check Swift version
# # ApodiniLIFX/lifx/main.swift demands swift ver >= 5.3.0
# run: swift --version
#
# - name: Build & Test
# run: swift test --configuration debug --enable-test-discovery
# Job # 3: #
build_and_push_to_registry:
# Only executed after codebase passes all tests #
# needs: [test_build]
# Build image and push to GitHub registry if on correct branch #
name: Build and Push Docker Image # to GitHub Container Registry
env:
DOCKER_TARGET_PLATFORM: linux/arm64/v8
runs-on: ubuntu-latest
# if this conditional fails, none of the steps are executed #
if: github.ref == 'refs/heads/trunk'
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@master
with:
platforms: all
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
# - uses: actions/cache@v2
# with:
# path: /tmp/.buildx-cache
# key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
# restore-keys: |
# ${{ runner.os }}-spm
- name: Generate cache filename
id: filenamegen
run: |
FILENAME=$( echo ${{ github.event.repository.name}}-${{ github.ref }} | sed 's/\//-/g' )-$( echo ${{ env.DOCKER_TARGET_PLATFORM }} | sed 's/\//-/g' )
echo "::set-output name=filename::$FILENAME"
- name: Log into registry
# This is where you will update the PATH to GITHUB_TOKEN
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
platforms: ${{ env.DOCKER_TARGET_PLATFORM }}
push: false
load: true
tags: jass-smart-infrastructure-webservice
labels: "runnumber=${GITHUB_RUN_ID}"
# cache-from: type=local,src=/tmp/.buildx-cache
# cache-to: type=local,dest=/tmp/.buildx-cache-new
build-args: |
FILENAME=${{ steps.filenamegen.outputs.filename }}
TOKEN=${{ secrets.CACHE_TOKEN }}
# - name: Move cache
# run: |
# rm -rf /tmp/.buildx-cache
# mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Log into registry
# This is where you will update the PATH to GITHUB_TOKEN
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "trunk" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
# Job # 5: # ## DEPLOY ##
deploy_to_raspberry:
# Only executed once Docker image is successfully built and pushed to registry #
needs: [ build_and_push_to_registry ] #, test_build]
# Move Docker image from Registry to RPi #
name: Deploy Docker image to the Raspberry
runs-on: ubuntu-latest
# if head is not at trunk, fail before running the steps! #
if: github.ref == 'refs/heads/trunk'
steps:
# Perform deployment via SSH
- name: Deploy with SSH
uses: fifsky/ssh-action@v0.0.5
with:
# Command to execute on the remote server, i.e. another mess
command: |
# cd into smart-infrastructure directory #
cd ~/infrastructure
# log on into docker using github secrets #
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
REGISTRY_ADDR=ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest
# Change all uppercase to lowercase to follow convention of repository name
REGISTRY_ADDR=$(echo $REGISTRY_ADDR | tr '[A-Z]' '[a-z]')
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
# Change all uppercase to lowercase to follow convention of image_id
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
echo REGISTRY_ADDR=$REGISTRY_ADDR
# Pull the image out of where we stored it
docker pull $REGISTRY_ADDR
# -----------------------------------------------------
# Store the old port and container_id to make sure we can find, stop and rm it
NGINX_CONFIG_PATH="nginx/conf.d/nginx.conf"
OLDPORT="$(grep -hnr 'proxy_pass' $NGINX_CONFIG_PATH | sed -r 's/.*?proxy_pass http\:\/\/.+?\:([[:digit:]]+).*/\1/')"
OLDCID="$(grep -hnr 'proxy_pass' $NGINX_CONFIG_PATH | sed -r 's/.*?proxy_pass http\:\/\/(.+?)\:[[:digit:]]+.*/\1/')"
echo $OLDCID
echo $OLDPORT
# Initialise ISINIT as false.
ISINIT=false
# if container hasn't been set up yet, then OLDCID is just IP of localhost.#
# and then we set ISINIT to true. #
[ $OLDCID == "127.0.0.1" ] && ISINIT=true
# Set the old url to be this:
OLDURL="http://$OLDCID:$OLDPORT"
# Set the old image to the id of the one container we can
# find on running docker inspect
$ISINIT || OLDIMGID=$(docker inspect --format='{{.Image}}' $OLDCID)
# --------------------------------------------------------
# CREATE THE NEW CONTAINER'S DETAILS:
# New port is the one that is not the old port's
([ $OLDPORT == "8090" ] && NEWPORT=8092) || NEWPORT=8091
# New container id is whatever id we get when running the newest image,
# after syncing up with current container.
$ISINIT || NEWCID=$(docker run -d -p $NEWPORT:8080 -e SYNCHOST=$OLDURL $IMAGE_ID:latest)
$ISINIT && NEWCID=$(docker run -d -p $NEWPORT:8080 $IMAGE_ID:latest)
# ... here we are waiting for the sync to happen.
# How long does that take? 1 second?
sleep 1
# Regex the new URL to ...?
sed -ri "s/(.*?proxy_pass http\:\/\/)(.+?)\:([[:digit:]]+)(.*)/\1$NEWCID\:$NEWPORT\4/" $NGINX_CONFIG_PATH
# --------------------------------------------------------
# Reload the nginx container, refer to wiki.
docker exec infrastructure_nginx /etc/init.d/nginx reload
# ----------------------------------------------------
# STOP THE OLD CONTAINER (if it exists)
# ISINIT is false => stop old container #
# ISINIT is true => there was never a container present, just pass through #
$ISINIT || docker container stop $OLDCID
$ISINIT || docker container rm $OLDCID
$ISINIT || docker image rm $OLDIMGID
# ----------------------------------------------------
# Log out of registry
docker logout ghcr.io
# IP address of the MWN server
host: 131.159.38.52
# Username for authentication
user: ubuntu
# Port number of the server
port: 10010
# String that contains a private key, stored on JASS-2021 secrets (OpenSSH format)
key: ${{ secrets.TUM_RBP_1_SECRET }}