-
Notifications
You must be signed in to change notification settings - Fork 4
109 lines (89 loc) · 4.36 KB
/
test-and-publish.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
name: Test and publish
on:
push:
branches: [ "dev", "main" ]
pull_request:
branches: [ "dev", "main" ]
release:
types: [published]
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
lfs: true
- name: Checkout LFS objects
run: git lfs checkout
- name: Build software
run: |
docker compose -f docker-compose.yml -f docker-compose.test.yml pull # Force download of any newer dependency images
docker compose -f docker-compose.yml -f docker-compose.test.yml up -d --build
docker compose -f docker-compose.yml -f docker-compose.test.yml images # Useful for debugging
docker compose -f docker-compose.yml -f docker-compose.test.yml images -q | xargs docker inspect | grep -C3 RepoTags # Useful for debugging
- name: Run tests
# Here we execute a command inside the ssm container built in the previous step.
# If the tests fail we still want to run the next step to archive the test results, so we set FAIL to 0 initially and to 1 on failure but in doing so ensure that the test command always succeeds.
# The value of ${FAIL} is written into ${GITHUB_ENV} for use in the next step.
run: |
FAIL=0
docker compose -f docker-compose.yml -f docker-compose.test.yml exec -T ssm sh -c "cd /system-modeller && ./gradlew test" || FAIL=1
docker cp $(docker compose -f docker-compose.yml -f docker-compose.test.yml ps -q ssm):/system-modeller/build/build/reports/tests/test/ test-artifacts
echo "FAIL=${FAIL}" >> ${GITHUB_ENV}
- name: Archive test results
uses: actions/upload-artifact@v3
with:
name: test-artifacts
path: test-artifacts/
- name: Return test success
# If the tests failed then make the whole job fail
run: test ${FAIL} -eq 0
publish:
# This job should only run if the test job succeeds
# Don't run this job if it's a pull request, otherwise we get an image with branch name "merge" for every PR
needs: test
if: success() && github.event_name != 'pull_request'
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
lfs: true
- name: Checkout LFS objects
run: git lfs checkout
- name: Build the Docker image
# The metadata inside the image will include the final git commit SHA and the time of the final commit.
# If the commit is a tag then the image is also tagged with spyderisk/system-modeller:<tag-name> and the image will include the release number.
# Otherwise, the tag applied to the image will be like spyderisk/system-modeller:<branch-name>-<timestamp>
# e.g. spyderisk/system-modeller:dev-20230405T1012
# (where the timestamp is the time of the final commit in the build)
# and the image will also be tagged with spyderisk/system-modeller:<branch-name>-latest
run: |
TAG_ROOT=spyderisk/system-modeller
TIMESTAMP=$(git show -s --format=%cI ${GITHUB_SHA})
SHORT_TIME=$(echo ${TIMESTAMP} | sed 's/[-:]//g')
REF_END=$(echo ${GITHUB_REF} | sed 's/.*\///')
if [[ ${GITHUB_REF} == refs/tags/* ]]; then
TAG_RELEASE=${TAG_ROOT}:${REF_END}
echo "TAG_RELEASE=${TAG_RELEASE}" >> ${GITHUB_ENV}
docker build --tag ${TAG_RELEASE} --build-arg CI_VERSION=${REF_END} --build-arg CI_RELEASE=${TAG_RELEASE} --build-arg CI_COMMIT_SHA=${GITHUB_SHA} --build-arg CI_COMMIT_TIMESTAMP=${TIMESTAMP} --file Dockerfile --target ssm-production "."
else
TAG_DATE=${TAG_ROOT}:${REF_END}-${SHORT_TIME:0:13}
TAG_LATEST=${TAG_ROOT}:${REF_END}-latest
echo "TAG_DATE=${TAG_DATE}" >> ${GITHUB_ENV}
echo "TAG_LATEST=${TAG_LATEST}" >> ${GITHUB_ENV}
docker build --tag ${TAG_DATE} --tag ${TAG_LATEST} --build-arg CI_VERSION=${REF_END}-latest --build-arg CI_COMMIT_SHA=${GITHUB_SHA} --build-arg CI_COMMIT_TIMESTAMP=${TIMESTAMP} --file Dockerfile --target ssm-production "."
fi
- name: Push Docker image to registry
run: |
docker login -u ${{ vars.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_RW_SECRET }}
if [[ ${GITHUB_REF} == refs/tags/* ]]; then
docker push ${TAG_RELEASE}
else
docker push ${TAG_DATE}
docker push ${TAG_LATEST}
fi