generated from actions/hello-world-docker-action
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/ci as a state of the art part 4 (#30)
* use go_test as separate workflow * add exit_code for conclusion * make build workflow only build * pack to docker only if we build it * dont retag if changes in go.mod or common libs * skip tests if no changes in code nor in tests
- Loading branch information
1 parent
e01b5cd
commit 719d57b
Showing
8 changed files
with
388 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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}" |
Oops, something went wrong.