-
Notifications
You must be signed in to change notification settings - Fork 0
182 lines (161 loc) · 7.56 KB
/
build_docker_images.yaml
File metadata and controls
182 lines (161 loc) · 7.56 KB
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
176
177
178
179
180
181
182
# GitHub Actions Example for Multiarch Docker Images
# Source: https://github.com/Tob1as/docker-build-example
# Documentation:
# * Docker: https://docs.docker.com/build/ci/github-actions/
# * GitHub: https://docs.github.com/en/actions (.github/workflows/*.yaml)
# * Gitea: https://docs.gitea.com/usage/actions/overview (.gitea/workflows/*.yaml)
# * Forgejo: https://forgejo.org/docs/latest/user/actions/ (.forgejo/workflows/*.yaml)
# Actions:
# * https://github.com/actions/checkout
# * https://github.com/docker/login-action
# * https://github.com/docker/setup-qemu-action
# * https://github.com/docker/setup-buildx-action
# * https://github.com/docker/build-push-action
# * https://github.com/hadolint/hadolint-action
# * https://github.com/peter-evans/dockerhub-description
name: 'build docker images: example'
on:
#push:
# branches:
# - 'main'
# - 'master'
# paths:
# - 'Dockerfile'
# - '.github/workflows/build_docker_images.yaml'
workflow_dispatch:
#schedule:
# - cron: '30 5 28 * *' # At 05:30 on day-of-month 28.
defaults:
run:
shell: bash
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Shell-Script
shell: bash
id: script
run: |
BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
BUILD_DATE_NUMERIC="${BUILD_DATE//[^[:digit:]]/}"
#COMMIT_HASH=$(git rev-parse --short "$GITHUB_SHA")
COMMIT_HASH=${GITHUB_SHA::7}
GIT_URL=$(echo "${GITHUB_SERVER_URL}" | awk -F/ '{print $3}' | sed 's/\/*$//')
GIT_URL=$(echo "$GIT_URL" | sed 's/github\.com/ghcr\.io/g') # GIT_URL switch to ghcr.io registry for GitHub
GIT_REPO=${GITHUB_REPOSITORY,,}
GIT_REPO_SHORT=${GIT_REPO#*/}
GIT_REPO_SHORT=${GIT_REPO_SHORT#"docker-"}
DOCKER_REPO=${{ vars.DOCKER_USERNAME }}/${GIT_REPO_SHORT}
REDHAT_QUAY_REPO=${{ vars.REDHAT_QUAY_USERNAME }}/${GIT_REPO_SHORT}
echo "ENVs: BUILD_DATE=${BUILD_DATE}, BUILD_DATE_NUMERIC=${BUILD_DATE_NUMERIC}, COMMIT_HASH=${COMMIT_HASH}, GIT_URL=${GIT_URL}, GIT_REPO=${GIT_REPO}"
#echo "ENVs (by Git) for Owner: GITHUB_REPOSITORY_OWNER=${GITHUB_REPOSITORY_OWNER} and github.repository_owner=${{ github.repository_owner }} are the same!"
# Set output to action <https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/passing-information-between-jobs>
echo "build_date=${BUILD_DATE}" >> "$GITHUB_OUTPUT"
echo "build_date_numeric=${BUILD_DATE_NUMERIC}" >> "$GITHUB_OUTPUT"
echo "commit_hash=${COMMIT_HASH}" >> "$GITHUB_OUTPUT"
echo "git_url=${GIT_URL}" >> "$GITHUB_OUTPUT"
echo "git_repo=${GIT_REPO}" >> "$GITHUB_OUTPUT"
echo "docker_repo=${DOCKER_REPO}" >> "$GITHUB_OUTPUT"
echo "redhat_quay_repo=${REDHAT_QUAY_REPO}" >> "$GITHUB_OUTPUT"
# Set ENVs to action <https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#using-the-env-context-to-access-environment-variable-values>
echo "commit_hash=${COMMIT_HASH}" >> "$GITHUB_ENV"
# TOKEN workarround for Gitea/Forgejo
if [[ $GIT_URL == "github.com" || $GIT_URL == "ghcr.io" ]]; then GIT_REGISTRY_TOKEN="${{ secrets.GITHUB_TOKEN }}"; else GIT_REGISTRY_TOKEN="${{ secrets.GIT_USER_PASSWD }}"; fi
echo "git_registry_token=${GIT_REGISTRY_TOKEN}" >> "$GITHUB_OUTPUT"
# add "GIT_USER_PASSWD" to Settings->Actions->Secrets in Repository,
# because ${{ secrets.GITHUB_TOKEN }}, ${GITHUB_TOKEN}, ${{ github.token }} or ${{ env.GITHUB_TOKEN }} not working :-(
# see: https://github.com/go-gitea/gitea/issues/23642 for ${{ secrets.GITEA_TOKEN}} and other.
#- name: Shell-Script-echo-outputs
# id: script-echo
# run: |
# echo ">> GITHUB_OUTPUT"
# echo ${{steps.script.outputs.build_date}}
# echo ${{steps.script.outputs.build_date_numeric}}
# echo ${{steps.script.outputs.commit_hash}}
# echo ${{steps.script.outputs.git_url}}
# echo ${{steps.script.outputs.git_repo}}
# echo ${{steps.script.outputs.docker_repo}}
# echo ">> GITHUB_ENV"
# echo "${{ env.commit_hash }}"
# echo "$commit_hash"
#- name: hadolint
# uses: hadolint/hadolint-action@master
# with:
# dockerfile: ./Dockerfile
- name: Install Docker
run: |
if ! command -v docker &> /dev/null; then
curl -fsSL https://get.docker.com | sh
else
echo "skip -> Docker is already installed!"
fi
- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v3
with:
image: tonistiigi/binfmt:latest
platforms: all
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
#with:
# buildkitd-config-inline: |
# [registry."${{steps.script.outputs.git_url}}"]
# #insecure = true
# ca=["/etc/ssl/certs/ca-chain.crt"]
- name: Login to GIT Container Registry
uses: docker/login-action@v3
with:
registry: ${{ steps.script.outputs.git_url }}
username: ${{ github.repository_owner }}
password: ${{ steps.script.outputs.git_registry_token }} # ${{ secrets.GITHUB_TOKEN }}
#- name: Login to DockerHub Container Registry
# uses: docker/login-action@v3
# with:
# registry: docker.io
# username: ${{ vars.DOCKER_USERNAME }}
# password: ${{ secrets.DOCKER_PASSWORD }}
#- name: Login to Docker Hardened Images Registry
# uses: docker/login-action@v3
# with:
# registry: dhi.io
# username: ${{ vars.DOCKER_USERNAME }}
# password: ${{ secrets.DOCKER_PASSWORD }}
#- name: Login to RED HAT Quay.io Container Registry
# uses: docker/login-action@v3
# with:
# registry: quay.io
# username: ${{ vars.REDHAT_QUAY_USERNAME }}
# password: ${{ secrets.REDHAT_QUAY_PASSWORD }}
- name: Build
uses: docker/build-push-action@v6
#env:
# APP_VERSION: 1.0.0
with:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/ppc64le,linux/riscv64,linux/s390x,linux/386
#pull: true
push: true
#target: production
build-args: |
BUILD_DATE=${{steps.script.outputs.build_date}}
VCS_REF=${{steps.script.outputs.commit_hash}}
# APP_VERSION=${{ env.APP_VERSION }}
tags: |
${{steps.script.outputs.git_url}}/${{steps.script.outputs.git_repo}}:latest
${{steps.script.outputs.git_url}}/${{steps.script.outputs.git_repo}}:${{steps.script.outputs.commit_hash}}
# docker.io/${{steps.script.outputs.docker_repo}}:latest
# quay.io/${{steps.script.outputs.redhat_quay_repo}}:latest
# outputs: type=image,name=target,annotation-index.org.opencontainers.image.description=My multi-arch image
#- name: Docker Hub Description
# uses: peter-evans/dockerhub-description@v5
# with:
# username: ${{ vars.DOCKER_USERNAME }}
# password: ${{ secrets.DOCKER_PASSWORD }}
# repository: ${{steps.script.outputs.docker_repo}}
# short-description: ${{ github.event.repository.description }}
# readme-filepath: README.md