-
Notifications
You must be signed in to change notification settings - Fork 13
174 lines (169 loc) · 5.22 KB
/
container-image.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
name: Container image
on:
push:
paths-ignore:
- 'ci/**'
- 'README.md'
schedule:
- cron: '40 08 * * 1' # Run every week to get updated dependencies.
pull_request:
types: [opened, reopened, synchronize]
workflow_dispatch:
jobs:
build:
name: Build image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Docker build
run: docker build -t mreg .
- name: Save image
run: docker save mreg | gzip > mreg.tgz
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: mreg
path: mreg.tgz
test:
name: Unit tests
needs: build
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: mreg
POSTGRES_PASSWORD: mreg
# Set health checks to wait until postgres has started
options: >-
--health-cmd "pg_isready --username=mreg"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Map the containerized port to localhost.
- 5432:5432
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: mreg
- name: Load image
run: docker load --input mreg.tgz
- name: Run tests
run: |
docker run --rm -t --network host --entrypoint /app/entrypoint-test.sh \
-e MREG_DB_HOST=localhost -e MREG_DB_PASSWORD=mreg -e MREG_DB_USER=mreg \
mreg
mreg-cli:
name: Test with mreg-cli
needs: build
runs-on: ubuntu-latest
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: mreg
- name: Load container image
run: docker load --input mreg.tgz
- name: Tag container image
# There's a docker-compose.yml file in the mreg-cli repo that wants the image from ghcr.io,
# but we want to use the newly built custom image
run: docker tag mreg ghcr.io/unioslo/mreg:latest
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.11
- name: Install mreg-cli
run: |
wget -nd https://github.com/unioslo/mreg-cli/archive/refs/heads/master.zip
unzip master.zip
cd mreg-cli-master
pip install -r requirements.txt
pip install -e .
- name: Run the tests
run: mreg-cli-master/ci/run_testsuite_and_record.sh
- name: Upload the log as an artifact
uses: actions/upload-artifact@v3
with:
name: new_testsuite_log.json
path: mreg-cli-master/ci/new_testsuite_log.json
test-with-curl:
name: Test with curl
needs: build
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: mreg
POSTGRES_PASSWORD: mreg
# Set health checks to wait until postgres has started
options: >-
--health-cmd "pg_isready --username=mreg"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Map the containerized port to localhost.
- 5432:5432
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: mreg
- name: Load container image
run: docker load --input mreg.tgz
- name: Start mreg
run: |
docker run --rm -t --network host --detach --name mreg \
-e MREG_DB_HOST=localhost -e MREG_DB_PASSWORD=mreg -e MREG_DB_USER=mreg \
mreg
- name: Wait for mreg to create the database schema and start up
run: sleep 10s
- name: Create a user
run: docker exec -t mreg /app/manage.py create_mreg_superuser --username test --password test123
- name: Authenticate using curl
shell: bash
run: |
curl http://127.0.0.1:8000/api/token-auth/ \
-X POST -H "Content-Type: application/json" \
--data "{\"username\":\"test\",\"password\":\"test123\"}" \
--output /tmp/curl_output.txt \
--verbose --no-progress-meter \
--write-out %{http_code} \
> /tmp/http_status_code.txt 2> /tmp/curl_errors.txt
STATUS=$(cat /tmp/http_status_code.txt)
if [ $STATUS -ge 400 ]; then
cat /tmp/curl_output.txt
exit 1
fi
publish:
name: Publish
# only publish the image if this event was triggered on the master branch, and not by a pull request
if: ${{ github.ref == 'refs/heads/master' && github.event_name != 'pull_request' }}
needs: [test, mreg-cli, test-with-curl]
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: mreg
- name: Load image
run: docker load --input mreg.tgz
- name: Log in to registry
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 }}/mreg
TAG_NAME=latest
docker tag mreg:latest $IMAGE_ID:$TAG_NAME
docker push $IMAGE_ID:$TAG_NAME