-
Notifications
You must be signed in to change notification settings - Fork 0
151 lines (129 loc) · 5.1 KB
/
build-and-publish-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
# Creates a multiplatform image of cousins matter and push it to ghcr.io for each new release
name: Image Build & Push
on:
push:
branches: []
tags-ignore: []
release:
types: [prereleased, published]
workflow_dispatch:
env:
# The registry where the image will be pushed.
REGISTRY: 'ghcr.io'
# name of the docker image
IMAGE_NAME: ${{ github.repository }}
# a list of platform architecture to be built in the image.
PLATFORMS: 'linux/amd64,linux/arm64'
# Should the built image be pushed to the registry?
PUSH: ${{ github.event_name == 'release' && true || github.event_name == 'push' && true || false }}
# how to compute image tags
TAGS_DEF: ${{ github.event_name == 'release' && 'type=semver,pattern={{version}}' || github.event_name == 'push' && 'type=ref,event=branch' || 'type=sha' }}
# version of the application
# VERSION: ${{ github.event.release.tag_name || github.ref_name }}
jobs:
test-build-image-publish:
if: github.event_name == 'release' || github.event_name == 'push' && startsWith(github.ref, 'refs/heads/')
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: trace event trigger
env:
EVENT: ${{ toJson(github.event) }}
run: |
echo $EVENT
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to get all tags
- name: Determine version
id: get_version
run: |
if [[ ${{ github.event_name }} == 'release' ]]; then
echo "VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
elif [[ ${{ github.ref }} == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
else
echo "VERSION=$(git describe --tags --abbrev=0)+$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
fi
- name: Create release.txt
run: echo ${{ steps.get_version.outputs.VERSION }} > release.txt
- name: Upload release.txt
uses: actions/upload-artifact@v4
with:
name: release
path: release.txt
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: '3.10'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 tblib
pip install -r requirements.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --indent-size 2 --exclude migrations
- name: Make envfile
uses: SpicyPizza/create-envfile@v2.0.3
with:
envkey_SECRET_KEY: 'django-insecure-this-is-just-for-test'
envkey_SITE_NAME: 'My Beautiful Site'
envkey_ALLOWED_HOSTS: '127.0.0.1,localhost'
envkey_LANGUAGE_CODE: 'fr'
envkey_EMAIL_HOST: 'smtp.gmail.com'
envkey_EMAIL_PORT: 465
envkey_EMAIL_HOST_USER: 'nobody@gmail.com'
envkey_EMAIL_HOST_PASSWORD: 'no matter'
directory: .
file_name: .env
fail_on_empty: false
sort_keys: false
- name: Test django (skip redis needing tests)
run: |
python ./manage.py test --exclude-tag needs-redis
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GHCR.io
uses: docker/login-action@v3
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ${{ env.REGISTRY }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: ${{ env.TAGS_DEF }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ env.PLATFORMS }}
push: ${{ env.PUSH }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Test Docker Image
env:
SOCKS_MOUNT: -v "/var/run/docker.sock":"/var/run/docker.sock"
MOUNTS: -v ./data:/app/data -v ./.env:/app/.env -v ./media:/app/media
TAGS: ${{ steps.meta.outputs.tags }}
run: |
mkdir -p ./data ./media ./media/public
touch ./media/public/theme.css
FIRST_TAG=$(echo "$TAGS" | head -n 1)
echo "Tested image is $FIRST_TAG"
docker run --name cousins-matter -p 8000:8000 -d $MOUNTS $FIRST_TAG
# let the server start before running tests
sleep 10
docker exec cousins-matter python manage.py test