-
-
Notifications
You must be signed in to change notification settings - Fork 110
144 lines (128 loc) · 4.57 KB
/
build_ci.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
---
name: Build and Deploy CI
on:
push:
branches: [ 'dev-**', 'pr-**', staging, master ]
tags: [ '**' ]
pull_request:
branches: [ staging, master ]
schedule:
# At 02:30 on Saturday
- cron: '30 2 * * 6'
jobs:
build_docker:
name: Build Docker Image
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
flavour: ["debian", "arch"]
steps:
# Fetch shallow git repository
- name: Checkout
uses: actions/checkout@v2
# Use QEMU to build
- name: Set up QEMU
if: success()
id: qemu
uses: docker/setup-qemu-action@v1
# Use docker buildx to build the docker image
- name: Build the Docker image
uses: docker/setup-buildx-action@v1
if: success()
id: buildx
with:
version: latest
# Generate 'prepare' build arguments to be retrieved later on
- name: Prepare
if: success()
id: prepare
run: |
FLAVOUR=${{ matrix.flavour }}
echo "FLAVOUR='${FLAVOUR}'"
echo "GITHUB_REF='${GITHUB_REF}'"
echo "GITHUB_REPOSITORY='${GITHUB_REPOSITORY}'"
DOCKER_IMAGE=docker.io/josh5/steam-headless
VERSION_TAG=${GITHUB_REF#refs/*/}
DOCKER_TAGS=""
if [[ ${VERSION_TAG%/merge} == 'master' ]]; then
if [[ ${FLAVOUR} == 'debian' ]]; then
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:latest,"
fi
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR},"
elif [[ ${VERSION_TAG%/merge} == 'staging' ]]; then
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-staging,"
elif [[ ${VERSION_TAG%/merge} =~ "dev-"* ]]; then
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-${VERSION_TAG%/merge},"
fi
if [[ ${GITHUB_REF} == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
if [[ ${VERSION} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[-\w]*$ ]]; then
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:${FLAVOUR}-${VERSION},"
if [[ ${FLAVOUR} == 'debian' ]]; then
DOCKER_TAGS="${DOCKER_TAGS}${DOCKER_IMAGE}:latest,"
fi
fi
fi
echo "DOCKER_TAGS='${DOCKER_TAGS}'"
echo "Build: [$(date +"%F %T")] [${GITHUB_REF_NAME}] [${GITHUB_SHA}] [${FLAVOUR}]" > ./overlay/version.txt
DOCKER_PUSH="true"
if [[ ${DOCKER_IMAGE} != 'docker.io/josh5/steam-headless' ]]; then
DOCKER_PUSH="false"
fi
if [[ ${VERSION_TAG%/merge} =~ "pr-"* ]]; then
DOCKER_PUSH="false"
fi
if [[ ${VERSION_TAG%/merge} =~ ^[0-9]+$ ]]; then
DOCKER_PUSH="false"
fi
if [[ "X${DOCKER_TAGS}" == "X" ]]; then
DOCKER_PUSH="false"
fi
echo "DOCKER_PUSH='${DOCKER_PUSH}'"
cat ./overlay/version.txt
echo "docker_image=${DOCKER_IMAGE}" >> $GITHUB_OUTPUT
echo "docker_tags=$(echo ${DOCKER_TAGS} | sed 's/,$//')" >> $GITHUB_OUTPUT
echo "docker_platforms=linux/amd64" >> $GITHUB_OUTPUT
echo "docker_push=${DOCKER_PUSH}" >> $GITHUB_OUTPUT
# Cache the build
- name: Cache Docker layers
uses: actions/cache@v2
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ matrix.flavour }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.flavour }}-
# Login to Docker Hub
- name: Login to Docker Hub
if: success() && (startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/'))
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Run docker build and push
- name: Docker Build and Push
if: success()
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile.${{ matrix.flavour }}
pull: true
platforms: ${{ steps.prepare.outputs.docker_platforms }}
push: ${{ steps.prepare.outputs.docker_push }}
tags: |
${{ steps.prepare.outputs.docker_tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
# Keep only latest cache
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
if: always()
run: |
if [[ -e /tmp/.buildx-cache-new ]]; then
echo "Cleaning up old cache..."
rm -rf /tmp/.buildx-cache
mv -v /tmp/.buildx-cache-new /tmp/.buildx-cache
fi