-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (126 loc) · 4.46 KB
/
docker.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
name: Docker Image Build and Publish
on:
push:
branches:
- main
schedule:
# At 20:00 UTC everyday (roughly 06:00 AEST)
- cron: '0 20 * * *'
workflow_dispatch:
inputs:
dockerfile_dir:
type: choice
description: Docker Image to Build
options:
- debug-image
- ruby-image
defaults:
run:
shell: bash
env:
DOCKER_REPOSITORY: ${{ github.repository }}
DOCKER_REGISTRY: 'ghcr.io'
jobs:
matrix_prep:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set_matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- name: Matrix Setup
id: set_matrix
run: |
ruby .pipeline/generate_matrix.rb -e "${{ github.event_name }}" -d "${{ github.event.inputs.dockerfile_dir }}"
build:
runs-on: ubuntu-latest
needs: matrix_prep
permissions:
contents: read
packages: write
id-token: write
strategy:
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
fail-fast: false
env:
DOCKERFILE_DIR: ${{ matrix.dockerfile_dir }}
BUILD_ARGS: ${{ matrix.build_args }}
NO_CACHE: false
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set build cache
run: |
# Never use cache if debug image to force patching.
if [[ "${{ env.DOCKERFILE_DIR }}" == "debug-image" ]]; then
echo "Setting NO_CACHE to true"
echo "NO_CACHE=true" >> $GITHUB_ENV
else
echo "Setting NO_CACHE to false"
echo "NO_CACHE=false" >> $GITHUB_ENV
fi
- name: Log into registry ${{ env.DOCKER_REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Set build arg tag version
- name: Set image tags based on build args.
run: |
export BUILD_ARGS="${{ env.BUILD_ARGS }}"
if [[ -z ${BUILD_ARGS} ]]; then
echo "BUILD_ARGS is empty"
APP_NAME="${{ matrix.dockerfile_dir }}"
export APP_NAME
APP_VERSION="${{ matrix.dockerfile_dir }}"
export APP_VERSION
else
export APP_VERSION="${BUILD_ARGS#*=}"
APP_NAME=${BUILD_ARGS%%=*}
fi
echo "The APP_VERSION is '${APP_VERSION}'"
echo "APP_VERSION=${APP_VERSION}" >> $GITHUB_ENV
echo "The APP_NAME is '${APP_NAME}'"
echo "APP_NAME=${APP_NAME}" >> $GITHUB_ENV
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
# uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
uses: docker/[email protected]
with:
# list of Docker images to use as base name for tags
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKERFILE_DIR }}
# generate Docker tags based on the following events/attributes
tags: |
type=raw,value={{date 'YYYY.MM.DD.hhmm' tz='Australia/Brisbane'}}
type=ref,event=branch
type=sha,priority=100,prefix=sha-,suffix=-shrt,format=short
type=sha,format=long,prefix=,priority=9999
type=raw,value=${{ env.APP_NAME }}-${{ env.APP_VERSION }}
type=raw,value=${{ github.ref_name }}-${{ env.APP_VERSION }}
type=raw,value=${{ env.APP_VERSION }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push ${{ env.APP_NAME }} Docker image 🐳
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
file: ./${{ env.DOCKERFILE_DIR }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ env.BUILD_ARGS }}
no-cache: ${{ env.NO_CACHE }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Annotate job with docker pull commands
run: |
tags=$(echo $DOCKER_METADATA_OUTPUT_JSON| jq -r '.tags[]')
for tag in $tags
do
echo "docker pull $tag" >> $GITHUB_STEP_SUMMARY
done