-
Notifications
You must be signed in to change notification settings - Fork 15
175 lines (158 loc) · 5.78 KB
/
dockerhub_build.yaml
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
175
#
# Author: Hari Sekhon
# Date: 2022-01-27 18:38:27 +0000 (Thu, 27 Jan 2022)
#
# vim:ts=2:sts=2:sw=2:et
#
# https://github.com/HariSekhon/GitHub-Actions
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# D o c k e r B u i l d -> D o c k e r H u b
# ============================================================================ #
# Docker Build, Tag & Push to DockerHub
---
name: Docker Build -> DockerHub
on:
workflow_call:
inputs:
repo:
description: The DockerHub repository in <user>/<repo> format
type: string
required: true
tags:
description: Tags to assign, space or line separated
type: string
default: latest
context:
description: the Docker context directory for the docker build
type: string
default: .
file:
description: the Dockerfile to build from. Defaults to Dockerfile inside the context. If specifying context, needs to include the context path prefix
type: string
#default: Dockerfile # breaks auto-pathing when specifying context, you'd have to remember to set file to $context/Dockerfile yourself, better to omit this
max-cache:
description: Use external cache image to capture multi-stage build cache
type: boolean
default: false
build-cache:
description: The build cache tag to use
type: string
default: buildcache
dockerfile-repo:
description: A separate repo where the Dockerfile for the build can be found. Will be checked out first to a directory of the same name
type: string
required: false
dockerfile-ref:
description: The branch from dockerfile-repo to checkout (defaults to the default branch if not specified)
type: string
required: false
debug:
type: string
required: false
default: false
secrets:
DOCKERHUB_USER:
required: true
DOCKERHUB_TOKEN:
required: true
permissions:
contents: read
packages: write
defaults:
run:
shell: bash -euxo pipefail {0}
env:
REPO: ${{ inputs.repo }}
TAGS: ${{ inputs.tags }}
BUILDCACHE: ${{ inputs.build-cache }}
DEBUG: ${{ inputs.debug == true || github.event.inputs.debug == 'true' || '' }}
jobs:
dockerhub_build:
name: DockerHub Build
runs-on: ubuntu-latest
# better done in the calling workflow to prevent manual docker builds on the wrong branches
#if: github.ref_name == 'master' || github.ref_name == 'main' || contains(github.ref_name, 'docker')
steps:
- name: Environment
run: env | sort
- uses: actions/checkout@v3
with:
submodules: 'true' # requires Git 2.18+ to be installed first
- name: Generate environment variable DOCKERFILE_CHECKOUT_PATH
if: ${{ inputs.dockerfile-repo }}
run: |
DOCKERFILE_CHECKOUT_PATH="$DOCKERFILE_REPO"
DOCKERFILE_CHECKOUT_PATH="${DOCKERFILE_CHECKOUT_PATH##*/}"
echo "DOCKERFILE_CHECKOUT_PATH=$DOCKERFILE_CHECKOUT_PATH" >> "$GITHUB_ENV"
env:
DOCKERFILE_REPO: ${{ inputs.dockerfile-repo }}
- name: Git Checkout Dockerfile repo
if: ${{ inputs.dockerfile-repo }}
uses: actions/checkout@v3
with:
repository: ${{ inputs.dockerfile-repo }}
ref: ${{ inputs.dockerfile-ref }}
submodules: 'true' # requires Git 2.18+ to be installed first
path: ${{ env.DOCKERFILE_CHECKOUT_PATH }}
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Generate REPO_TAGS environment variable
run: |
echo "REPO_TAGS<<EOF" >> "$GITHUB_ENV"
for tag in ${{ env.TAGS }}; do
if ! [[ "$tag" =~ : ]]; then
echo "${{env.REPO}}:$tag" >> "$GITHUB_ENV"
else
echo "$tag" >> "$GITHUB_ENV"
fi
done
echo EOF >> "$GITHUB_ENV"
- name: Generate CACHE_FROM environment variable
run: |
echo "CACHE_FROM<<EOF" >> "$GITHUB_ENV"
for repo_tag in $(tr '\n' ' ' <<< "${{ env.REPO_TAGS }}"); do
echo "type=registry,ref=$repo_tag" >> "$GITHUB_ENV"
done
echo "EOF" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# XXX: Normal build, use inline cache when max-cache input is not set to 'true' - fine for most purposes except for multi-stage builds
- name: Docker Build & Push
if: ${{ ! inputs.max-cache }}
uses: docker/build-push-action@v2
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
tags: ${{ env.REPO_TAGS }}
push: true
cache-from: ${{ env.CACHE_FROM }}
cache-to: type=inline
# call like so:
#
# jobs:
# dockerhub_build:
# uses: HariSekhon/GitHub-Actions/.github/workflows/dockerhub_build.yaml@master
# with:
# max-cache: true
# ...
#
- name: Docker Build & Push with external build cache
if: ${{ inputs.max-cache }}
uses: docker/build-push-action@v2
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
tags: ${{ env.REPO_TAGS }}
push: true
cache-from: |
type=registry,ref=${{env.REPO}}:${{env.BUILDCACHE}}
${{ env.CACHE_FROM }}
cache-to: type=registry,ref=${{env.REPO}}:${{env.BUILDCACHE}},mode=max