-
Notifications
You must be signed in to change notification settings - Fork 9
161 lines (139 loc) · 5.59 KB
/
docker-build.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
152
153
154
155
156
157
158
159
160
# This workflow is for building and possibly pushing the docker image for
# a commit tagged by a version number. It is triggered either manually or by
# a tag event of type "v*.*.*", which is intended for new releases. Currently,
# pushing to ghcr.io can happen only to commits tagged by a version number. This
# is intended to associate every docker image with a version number. This means
# that there is no option to push the image for the latest commit, even though
# we can build the image for testing purposes.
#
# Manual trigger
# In Actions, go to "Docker image build" and click
# "Run workflow" and choose from the following options.
# Options:
# - 'Build latest commit': for testing for the latest commit
# - 'Build existing version': for testing a particular version
# - 'Build + push existing version': for publishing a particular version
#
# Tag event
# In a tag event of type "v*.*.*", such as new release or retagging, this work
# flow is triggered to build and publish the image for the tagged version
# number.
#
# The image can be pulled by one of the following commands.
#
# docker pull ghcr.io/smithlabcode/dnmtools:latest
# docker pull ghcr.io/smithlabcode/dnmtools:<7-DIGIT SHA>
# docker pull ghcr.io/smithlabcode/dnmtools:v<VERSION NUMBER> (e.g. v1.3.0)
name: Docker image build
on:
workflow_dispatch:
branches: ['master']
inputs:
build_option:
description: 'Build/push option'
required: true
default: 'Build latest commit'
type: choice
options:
- 'Build latest commit'
- 'Build existing version'
- 'Build + push existing version'
version:
description: 'Version number: "v*.*.*"
(latest version if not specified)'
required: false
push:
tags:
- 'v*.*.*'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout current repository to get latest version
uses: actions/checkout@v3
with:
fetch-depth: 0 # Needed to get tags information
# Copy Dockerfile in case the commit to be used does not have it
# This is to be used to build older versions that didn't have Dockerfile.
# This temporary Dockerfile should not be needed for new versions.
- name: Copy Dockerfile to Temporary File
run: |
cp Dockerfile /tmp/Dockerfile
- name: 'Get latest version'
run: |
echo "LATEST_VERSION=$(git describe --abbrev=0 --tags)" >> $GITHUB_ENV
- name: Set version for manual trigger
if: github.event_name == 'workflow_dispatch'
run: |
if [[ "${{ github.event.inputs.version }}" == "" ]]; then
echo "VERSION=${{env.LATEST_VERSION}}" >> $GITHUB_ENV
else
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
fi
- name: Set version for tag trigger
if: startsWith(github.ref, 'refs/tags/v')
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: "Set commit hash"
run: |
if [[ "${{ github.event.inputs.build_option }}" == \
"Build latest commit" ]]; then
COMMIT_HASH=${{ github.sha }}
else
# for specified version in manual trigger or tag event
COMMIT_HASH=$(git rev-parse ${{ env.VERSION }})
fi
SHA=$(git rev-parse --short "${COMMIT_HASH}")
echo "COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
echo "SHA=${SHA}" >> $GITHUB_ENV
- name: "Checkout the commit: ${{ env.SHA }}"
uses: actions/checkout@v3
with:
ref: ${{ env.COMMIT_HASH }}
submodules: 'recursive'
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Decide whether to push the image to ghcr.io
run: |
if [[ "${{ github.event.inputs.build_option }}" == \
"Build + push existing version" ]] ||
[[ "${{ github.event_name }}" == "push" ]]; then
echo "PUSH=true" >> $GITHUB_ENV
else
echo "PUSH=false" >> $GITHUB_ENV
fi
# The default tags are the SHA hash and the version number.
# Below command adds the "latest" tag if the version number is the latest.
- name: Determine image tags
env:
IMAGE_TAGS: "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.SHA }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}"
run: |
if [ "${{ env.VERSION }}" == "${{ env.LATEST_VERSION }}" ]; then
echo "IMAGE_TAGS=${{ env.IMAGE_TAGS }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_ENV
else
echo "IMAGE_TAGS=${{ env.IMAGE_TAGS }}" >> $GITHUB_ENV
fi
- name: Use temp.Dockerfile if Dockerfile doesn't exist
run: |
if [ -f Dockerfile ]; then
echo "DOCKERFILE=./Dockerfile" >> $GITHUB_ENV
else
echo "DOCKERFILE=/tmp/Dockerfile" >> $GITHUB_ENV
fi
- name: 'Build Docker image (push: ${{ env.PUSH }}, dockerfile: ${{ env.DOCKERFILE }})'
uses: docker/build-push-action@v4
with:
context: .
file: ${{ env.DOCKERFILE }}
push: ${{ env.PUSH }}
tags: |
${{ env.IMAGE_TAGS }}