generated from actions/hello-world-docker-action
-
Notifications
You must be signed in to change notification settings - Fork 1
195 lines (191 loc) · 8.14 KB
/
50_go_build-component.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
name: Build Go cmd
on:
workflow_call:
inputs:
cmd-path:
required: true
type: string
go-build-cache-restore-or-update:
required: false
type: string
default: 'restore'
go-mod-path:
required: false
type: string
default: 'go.mod'
go-mod-cache-restore-or-update:
required: false
type: string
default: 'restore'
goarch:
required: false
type: string
default: '' # default is to take it from ${{ runner.arch }}
goos:
required: false
type: string
default: '' # default is to take it from ${{ runner.os }}
docker-image-base:
required: false
type: string
default: ''
retag-base-image:
required: false
type: boolean
default: false
run-build:
required: false
type: boolean
default: false
run-tests:
required: false
type: boolean
default: false
pack-to-docker-image:
required: false
type: boolean
default: false
base-sha:
required: false
type: string
default: 'anysha'
head-sha:
required: false
type: string
default: 'anysha'
jobs:
prepare-running-env:
name: Build ${{ inputs.cmd-path}} for "${{ inputs.goos }}/${{ inputs.goarch }}"
runs-on: [ self-hosted, small ]
outputs:
goarch: ${{ steps.goarch-goos.outputs.goarch }}
goos: ${{ steps.goarch-goos.outputs.goos }}
go-mod-cache-key: ${{ steps.set-cache-keys.outputs.go-mod-cache-key }}
go-build-cache-key-cmd: ${{ steps.set-cache-keys.outputs.go-build-cache-key-cmd }}
go-build-cache-key-test: ${{ steps.set-cache-keys.outputs.go-build-cache-key-test }}
prepare-runner-os: ${{ runner.os }}
prepare-runner-arch: ${{ runner.arch }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
id: setup-go
with:
cache: false
go-version-file: ${{ inputs.go-mod-path }}
- name: Set GOARCH and GOOS
id: goarch-goos
run: |
GOARCH_INPUT="${{ inputs.goarch }}"
GOOS_INPUT="${{ inputs.goos }}"
declare -A runner_to_go_os=( ["Linux"]="linux" ["Windows"]="windows" ["macOS"]="darwin" )
declare -A runner_to_go_arch=( ["X86"]="386" ["X64"]="amd64" ["ARM"]="arm" ["ARM64"]="arm64" )
ARCH_FROM_RUNNER="${runner_to_go_arch[${{ runner.arch }}]}"
OS_FROM_RUNNER="${runner_to_go_os[${{ runner.os }}]}"
echo "goarch=${GOARCH_INPUT:-$ARCH_FROM_RUNNER}" >> "$GITHUB_OUTPUT"
echo "goos=${GOOS_INPUT:-$OS_FROM_RUNNER}" >> "$GITHUB_OUTPUT"
- id: set-cache-keys
name: Set keys needed for caching
env:
OS: "${{ steps.goarch-goos.outputs.goos }}"
ARCH: "${{ steps.goarch-goos.outputs.goarch }}"
GO_VER: "${{ steps.setup-go.outputs.go-version }}"
GO_SUM_HASH: "${{ hashFiles('**/go.sum') }}"
GH_REPO: "${{ github.event.repository.full_name }}"
BASE_SHA: "${{ inputs.base-sha }}"
HEAD_SHA: ${{ inputs.head-sha }}
CMD_PATH: ${{ inputs.cmd-path }}
run: |
GH_REPO="${GH_REPO//\//_}" # '//\//_' wat?
# here how it works:
# ${var//pattern/replace}, -- to replace all the occurencise of 'pattern' with 'replace'
# and pattern is '/', so escape it: '\/'.
# and replace is just '_', easy.
CMD_PATH="${CMD_PATH//\//_}" # well, now you know what '//\//_' means and how it works, right? :)
echo "go-mod-cache-key=go-mod-cache-${OS}-${ARCH}-go-${GO_VER}-${GO_SUM_HASH}" >> "$GITHUB_OUTPUT"
echo "go-build-cache-key-base=go-build-cache-${OS}-${ARCH}-go-${GO_VER}-gh-${GH_REPO}-git-${BASE_SHA}-build-${CMD_PATH}" >> "$GITHUB_OUTPUT"
echo "go-build-cache-key-head=go-build-cache-${OS}-${ARCH}-go-${GO_VER}-gh-${GH_REPO}-git-${HEAD_SHA}-build-${CMD_PATH}" >> "$GITHUB_OUTPUT"
- id: docker-login
if: ${{ inputs.retag-base-image || inputs.pack-to-docker-image }}
uses: docker/login-action@v3
with:
username: ${{ secrets.NEON_DOCKERHUB_USERNAME }}
password: ${{ secrets.NEON_DOCKERHUB_PASSWORD }}
- id: docker-retag
if: ${{ inputs.retag-base-image }}
env:
DOCKER_IMAGE_BASE: ${{ inputs.docker-image-base }}
BASE_SHA: ${{ inputs.base-sha }}
HEAD_SHA: ${{ inputs.head-sha }}
BASE_TAG: arch-${{ steps.goarch-goos.outputs.goarch }}-commit-${{ inputs.base-sha }}
HEAD_TAG: arch-${{ steps.goarch-goos.outputs.goarch }}-commit-${{ inputs.head-sha }}
PLATFORM: ${{ steps.goarch-goos.outputs.goarch }}
continue-on-error: true
run: |
docker buildx imagetools create \
--tag "${DOCKER_IMAGE_BASE}:${HEAD_TAG}" \
"${DOCKER_IMAGE_BASE}:${BASE_TAG}"
- name: Go Mod Cache
uses: tespkg/actions-cache@v1
if: ${{ (inputs.go-build-cache-restore-or-update == 'update') && (steps.docker-retag.outcome != 'success') }}
with:
endpoint: ${{ vars.HETZNER_CACHE_REGION }}.${{ vars.HETZNER_CACHE_ENDPOINT }}
bucket: ${{ vars.HETZNER_CACHE_BUCKET }}
accessKey: ${{ secrets.HETZNER_CACHE_ACCESS_KEY }}
secretKey: ${{ secrets.HETZNER_CACHE_SECRET_KEY }}
use-fallback: false
path: |
${{ steps.set-cache-keys.outputs.go-mod-cache }}
key: ${{ steps.set-cache-keys.outputs.go-mod-cache-key }}
- name: Go Mod Cache Restore
uses: tespkg/actions-cache/restore@v1
if: ${{ (inputs.go-build-cache-restore-or-update == 'restore') && (steps.docker-retag.outcome != 'success') }}
with:
endpoint: ${{ vars.HETZNER_CACHE_REGION }}.${{ vars.HETZNER_CACHE_ENDPOINT }}
bucket: ${{ vars.HETZNER_CACHE_BUCKET }}
accessKey: ${{ secrets.HETZNER_CACHE_ACCESS_KEY }}
secretKey: ${{ secrets.HETZNER_CACHE_SECRET_KEY }}
use-fallback: false
path: |
${{ steps.set-cache-keys.outputs.go-mod-cache }}
key: ${{ steps.set-cache-keys.outputs.go-mod-cache-key }}
- name: Go Build Cache
uses: tespkg/actions-cache@v1
if: ${{ (inputs.go-build-cache-restore-or-update == 'update') && (steps.docker-retag.outcome != 'success') }}
with:
endpoint: ${{ vars.HETZNER_CACHE_REGION }}.${{ vars.HETZNER_CACHE_ENDPOINT }}
bucket: ${{ vars.HETZNER_CACHE_BUCKET }}
accessKey: ${{ secrets.HETZNER_CACHE_ACCESS_KEY }}
secretKey: ${{ secrets.HETZNER_CACHE_SECRET_KEY }}
use-fallback: false
path: |
${{ steps.set-cache-keys.outputs.go-build }}
key: ${{ steps.set-cache-keys.outputs.go-build-cache-key-head }}
- name: Go Build
if: ${{ steps.docker-retag.outcome != 'success' }}
env:
GOARCH: ${{ steps.goarch-goos.outputs.goarch }}
GOOS: ${{ steps.goarch-goos.outputs.goos }}
CMD_PATH: ${{ inputs.cmd-path }}
run: |
go build -v -o "build/$(basename "${CMD_PATH}")" "${CMD_PATH}"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Pack as Docker image
if: ${{ steps.docker-retag.outcome != 'success' }}
env:
DOCKER_IMAGE_BASE: ${{ inputs.docker-image-base }}
BASE_TAG: arch-${{ steps.goarch-goos.outputs.goarch }}-commit-${{ inputs.base-sha }}
HEAD_TAG: arch-${{ steps.goarch-goos.outputs.goarch }}-commit-${{ inputs.head-sha }}
PLATFORM: ${{ steps.goarch-goos.outputs.goarch }}
CMD_PATH: ${{ inputs.cmd-path }}
run: |
echo "+++ BUILD:"
ls -laht build/
docker build \
--tag "${DOCKER_IMAGE_BASE}:${HEAD_TAG}" \
--platform "${PLATFORM}" \
--build-arg BINARY_TO_ADD="$(basename "${CMD_PATH}")" \
--file Dockerfiles/go-app-common.Dockerfile \
build/
docker push \
"${DOCKER_IMAGE_BASE}:${HEAD_TAG}"