-
Notifications
You must be signed in to change notification settings - Fork 2
170 lines (145 loc) · 5.42 KB
/
test-and-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
161
162
163
164
165
166
167
168
169
170
name: 'Test and deploy'
env: # Keep this in sync with Dockerfile version
NODE_VERSION: "20.17.0"
on:
push:
branches: [ main ]
pull_request:
types: [ opened, synchronize ]
workflow_dispatch:
jobs:
lint:
name: 'Lint'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: 'Install'
run: npm install
- name: 'Lint'
run: npm run lint
env:
NODE_OPTIONS: '--max-old-space-size=8192'
test:
name: 'Test'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: 'Install'
run: npm install
- name: 'Test'
run: npm run test:ci
- name: 'Upload test results'
uses: actions/upload-artifact@v4
if: always()
with:
name: Test Results
path: junit.xml
# Upload event file so test results can be processed for the PR
# https://github.com/EnricoMi/publish-unit-test-result-action#support-fork-repositories-and-dependabot-branches
event_file:
name: "Event File"
runs-on: ubuntu-latest
steps:
- name: Upload
uses: actions/upload-artifact@v4
with:
name: Event File
path: ${{ github.event_path }}
build:
name: 'Build and deploy'
runs-on: ubuntu-22.04
if: vars.DEPLOY == 'true'
env:
VERSION: snapshot
COMPOSE_PROJECT_NAME: ${{ vars.DEPLOY_PROJECT_NAME }}-snapshot
COMPOSE_FILE: docker-compose.yml:docker-compose.traefik.yml
BASE_HREF: /
HOST: ${{ vars.DEPLOY_HOSTNAME }}
ADMIN_HASHED_PASSWORD: ${{ vars.ADMIN_HASHED_PASSWORD }}
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
needs: [ lint, test ]
steps:
- uses: actions/checkout@v4
with:
# If tags aren't fetched the bin/post-build-localized.js script will leave fields in generated/version.json empty
# https://github.com/actions/checkout/issues/701
fetch-depth: 0
- name: 'GitHub Slug Action'
uses: rlespinasse/github-slug-action@v4
- name: 'Find Current Pull Request'
uses: jwalton/gh-find-current-pr@v1
id: find-pr
- name: 'Set variables for PR'
# When running on a PR, build and tag the Docker image for a deployment with a different base-href and a static-only deployment on
# a path prefix, with the frontend using the /api URL from the main branch deployment.
if: ${{ success() && steps.find-pr.outputs.number }}
env:
PR: ${{ steps.find-pr.outputs.number }}
run: |
echo "VERSION=pr-${PR}" >> $GITHUB_ENV
echo "COMPOSE_PROJECT_NAME=${{ vars.DEPLOY_PROJECT_NAME }}-pr-${PR}" >> $GITHUB_ENV
echo "COMPOSE_FILE=docker-compose.yml:docker-compose.traefik.yml:ci/docker-compose.pr.yml" >> $GITHUB_ENV
echo "BASE_HREF=/pull-request/${PR}/${GITHUB_REF_NAME_SLUG_URL}/" >> $GITHUB_ENV
- name: 'Build image'
# Always uses the 'snapshot' tag of the tailormap-api base image by setting the API_VERSION build arg
run: |
docker build --progress plain --build-arg BASE_HREF=${BASE_HREF} --build-arg VERSION=${VERSION} --build-arg API_VERSION=snapshot --tag ${{ vars.DEPLOY_IMAGE_TAG }}:${VERSION} .
docker save ${{ vars.DEPLOY_IMAGE_TAG }}:${VERSION} > image.tar
- name: 'Set Docker context for deployment'
uses: arwynfr/actions-docker-context@v2
with:
docker_host: ${{ secrets.DEPLOY_DOCKER_HOST }}
context_name: 'dev-server'
ssh_cert: ${{ secrets.DEPLOY_DOCKER_HOST_SSH_CERT }}
ssh_key: ${{ secrets.DEPLOY_DOCKER_HOST_SSH_KEY }}
use_context: true
- name: 'Add known hosts'
run: |
DEPLOY_DOCKER_HOST=${{ secrets.DEPLOY_DOCKER_HOST }}
ssh-keyscan -H ${DEPLOY_DOCKER_HOST##*@} > $HOME/.ssh/known_hosts
- name: 'Load Docker image'
run: |
docker image rm ${{ vars.DEPLOY_IMAGE_TAG }}:${VERSION} || true
cat image.tar | docker load
- uses: actions/checkout@v4
with:
repository: B3Partners/tailormap-viewer
path: tailormap-viewer
sparse-checkout: |
docker-compose*.yml
sparse-checkout-cone-mode: false
- name: 'Update deployment using Docker Compose'
env:
TAILORMAP_IMAGE: ${{ vars.DEPLOY_IMAGE_TAG }}
run: |
cd tailormap-viewer
docker compose config
docker compose up -d --remove-orphans
- name: 'Create GitHub deployment'
if: success()
uses: chrnorm/deployment-action@v2
with:
token: "${{ secrets.GITHUB_TOKEN }}"
environment-url: "https://${{ vars.DEPLOY_HOSTNAME }}${{ env.BASE_HREF }}"
description: "Deployment for ${{ env.VERSION }}"
environment: test
initial-status: success
ref: "${{ env.GITHUB_HEAD_REF }}"