1
- name : Docker
1
+ name : Build and push Docker image
2
2
3
3
# This workflow uses actions that are not certified by GitHub.
4
4
# They are provided by a third-party and are governed by
@@ -7,75 +7,125 @@ name: Docker
7
7
8
8
on :
9
9
push :
10
- branches : [ "main" ]
11
- # Publish semver tags as releases.
12
- tags : [ 'v*.*.*' ]
13
- pull_request :
14
- branches : [ "main" ]
10
+ branches : [ '*' ] # Trigger on push to any branch
11
+ tags : [ 'v*.*.*' ] # Trigger on push to any tag matching the pattern
12
+ workflow_dispatch :
13
+ inputs :
14
+ Force_build :
15
+ description : ' Force build without checking files.'
16
+ required : true
17
+ default : ' warning'
18
+ type : choice
19
+ options :
20
+ - true
21
+ - false
15
22
16
- env :
17
- # Use docker.io for Docker Hub if empty
18
- REGISTRY : ghcr.io
19
- # github.repository as <account>/<repo>
20
- IMAGE_NAME : ${{ github.repository }}
21
23
24
+ env :
25
+ REGISTRY : ghcr.io # Use GitHub Container Registry
26
+ IMAGE_NAME : ${{ github.repository }} # Set IMAGE_NAME to the repository name
27
+ BRANCH_TAG : ${{ github.ref_name }} # Set BRANCH_TAG to the branch name
28
+ FORCE : ${{ inputs.Force_build }}
22
29
23
30
jobs :
24
- build :
25
-
31
+ build_and_push :
26
32
runs-on : ubuntu-latest
27
33
permissions :
28
- contents : read
29
- packages : write
30
- # This is used to complete the identity challenge
31
- # with sigstore/fulcio when running outside of PRs.
32
- id-token : write
34
+ contents : read # Read access to repository contents
35
+ packages : write # Write access to packages
36
+ # This is used to complete the identity challenge with sigstore/fulcio when running outside of PRs.
37
+ id-token : write # Write access to id-token for identity challenge with sigstore/fulcio
33
38
34
39
steps :
35
- - name : Checkout repository
36
- uses : actions/checkout@v3
40
+ - name : Checkout repository.
41
+ uses : actions/checkout@v3 # Checkout the repository
42
+
43
+ # List modified files : If only files present in .dockerignore have changed, the workflow will not run.
44
+ - name : Compare modified files to .dockerignore.
45
+ id : changes
46
+ run : |
47
+ git fetch --unshallow
48
+ last_commit=$(git rev-parse HEAD)
49
+ is_merge_commit=$(git log -1 --pretty=%P "${last_commit}" | wc -w)
50
+ if [ ${is_merge_commit} -gt 1 ]; then
51
+ parent_commits=$(git log -1 --pretty=%P "${last_commit}")
52
+ changed_files=$(git diff-tree --no-commit-id --name-only -r ${parent_commits})
53
+ else
54
+ changed_files=$(git diff-tree --no-commit-id --name-only -r "${last_commit}")
55
+ fi
56
+ echo "Changed files : ${changed_files}"
57
+ echo "Force build : ${FORCE:-false}"
58
+ if [ "${FORCE}" = "true" ]; then
59
+ continue="true"
60
+ else
61
+ ignore_patterns=$(grep -v '^#' .dockerignore | grep -v '^$')
62
+ continue="false"
63
+ # Check if any of the changed files is not ignored in .dockerignore.
64
+ for file in ${changed_files}; do
65
+ matched="false"
66
+ for pattern in ${ignore_patterns}; do
67
+ if echo "${file}" | grep -qE "^${pattern}(/|$)"; then
68
+ matched="true"
69
+ break
70
+ fi
71
+ done
72
+ if [ "${matched}" = "false" ]; then
73
+ continue="true"
74
+ break
75
+ fi
76
+ done
77
+ fi
78
+ echo "continue=${continue}" >> ${GITHUB_ENV}
79
+ echo "continue=${continue}"
37
80
38
- # Install the cosign tool except on PR
81
+ # Install the cosign tool
39
82
# https://github.com/sigstore/cosign-installer
40
- - name : Install cosign
41
- if : github.event_name != 'pull_request '
42
- uses : sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 # v3.1.1
83
+ - name : Install cosign.
84
+ if : env.continue == 'true '
85
+ uses : sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 # v3.1.1 # Install cosign tool except on pull requests
43
86
with :
44
- cosign-release : ' v2.1.1'
45
-
87
+ cosign-release : ' v2.1.1' # Specify cosign version
88
+
46
89
# Set up BuildKit Docker container builder to be able to build
47
90
# multi-platform images and export cache
48
91
# https://github.com/docker/setup-buildx-action
49
- - name : Set up Docker Buildx
50
- uses : docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
92
+ - name : Set up Docker Buildx.
93
+ if : env.continue == 'true'
94
+ uses : docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 # Set up Docker Buildx for building multi-platform images
51
95
52
- # Login against a Docker registry except on PR
96
+ # Login against a Docker registry
53
97
# https://github.com/docker/login-action
54
- - name : Log into registry ${{ env.REGISTRY }}
55
- if : github.event_name != 'pull_request '
56
- uses : docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
98
+ - name : Log into registry ${{ env.REGISTRY }}.
99
+ if : env.continue == 'true '
100
+ uses : docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 # Log into Docker registry except on pull requests
57
101
with :
58
102
registry : ${{ env.REGISTRY }}
59
103
username : ${{ github.actor }}
60
104
password : ${{ secrets.GITHUB_TOKEN }}
61
-
105
+
62
106
# Extract metadata (tags, labels) for Docker
63
107
# https://github.com/docker/metadata-action
64
- - name : Extract Docker metadata
108
+ - name : Extract Docker metadata.
109
+ if : env.continue == 'true'
65
110
id : meta
66
- uses : docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
111
+ uses : docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 # Extract metadata for Docker images
67
112
with :
68
113
images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
114
+ tags : |
115
+ ${{ github.ref == 'refs/heads/main' && 'latest' || '' }}
116
+ ${{ env.BRANCH_TAG }}
69
117
70
- # Build and push Docker image with Buildx (don't push on PR)
118
+ # Build and push Docker image with Buildx
71
119
# https://github.com/docker/build-push-action
72
- - name : Build and push Docker image
120
+ - name : Build and push Docker image.
121
+ if : env.continue == 'true'
73
122
id : build-and-push
74
- uses : docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
123
+ uses : docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 # Build and push Docker image with Buildx
75
124
with :
76
125
context : .
77
126
push : ${{ github.event_name != 'pull_request' }}
78
127
tags : ${{ steps.meta.outputs.tags }}
79
128
labels : ${{ steps.meta.outputs.labels }}
80
- cache-from : type=gha
81
- cache-to : type=gha,mode=max
129
+ cache-from : type=gha # Use GitHub Actions cache for Docker layers
130
+ platforms : linux/amd64,linux/arm64
131
+ cache-to : type=gha,mode=max # Use GitHub Actions cache for Docker layers
0 commit comments