This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
CI/CD #49
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
name: build-release-binaries | |
run-name: CI/CD | |
on: | |
pull_request: | |
push: | |
branches: | |
- master | |
tags: | |
- '*' | |
jobs: | |
# Job to run always when CI is triggered. | |
check_code: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Dump env | |
run: env | sort | |
- name: Dump GitHub context | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github) }} | |
run: echo "GITHUB_CONTEXT=$GITHUB_CONTEXT" | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Setup Go environment | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '^1.20.6' | |
- name: Get dependencies | |
run: go get -v -t -d | |
- name: Lint | |
run: go vet ./... | |
- name: Test | |
run: go test ./... | |
# Job to build artifacts only if tag was pushed. | |
build: | |
needs: check_code | |
runs-on: ubuntu-latest | |
if: startsWith(github.ref, 'refs/tags/') | |
# Run concurrently for all major OS-es and architectures. | |
strategy: | |
matrix: | |
os: [windows, darwin, linux] | |
arch: [amd64, arm64] | |
steps: | |
# 3 steps below are lazily copy-pasted from the job above. See https://stackoverflow.com/a/71570847/1535127. | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Get all tags, needed to get git log. | |
- name: Setup Go environment | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '^1.20.6' | |
- name: Get dependencies | |
run: go get -v -t -d ./... # Will use cached dependencies. | |
- name: Assemble version and release notes from commit messages | |
id: vars | |
run: | | |
version=`date +'%Y/%m/%d.%H:%M:%S'`".${GITHUB_REF_NAME}" | |
curtag=${GITHUB_REF_NAME} | |
prevtag=`git describe --tags --abbrev=0 HEAD^1` | |
git log --format=%B -n 1 > body.log | |
if git tag | grep ${prevtag} ; then | |
git log -q ${curtag}...${prevtag} --pretty="- %s" -q --no-color >> body.log | |
else | |
git log --pretty="- %s" -q --no-color >> body.log | |
fi | |
echo "version=${version}" >> $GITHUB_OUTPUT | |
- name: Print resulting information | |
run: | | |
echo version is ${{ steps.vars.outputs.version }} | |
cat body.log | |
- name: Build Binaries | |
run: | | |
GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build \ | |
-o aggregate-inecobank-statements-${{ matrix.os }}-${{ matrix.arch }} \ | |
-ldflags "-X main.Version=${{ steps.vars.outputs.version }}" | |
chmod a+x aggregate-inecobank-statements-* | |
- name: Save Version and Binaries | |
id: save_info | |
run: | | |
echo "${{ steps.vars.outputs.version }}" > version.txt | |
# Idea here that the last `ls` would overwrite content with all files list. | |
ls aggregate-inecobank-statements-* # For debugging. | |
echo "binaries=$(ls aggregate-inecobank-statements-*)" >> $GITHUB_ENV | |
# For debugging. | |
echo "GITHUB_ENV=$GITHUB_ENV" | |
cat ${GITHUB_ENV} || true | |
- name: Upload Binaries and Release data to artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: data | |
# Note that each job "thread" would write it's own files here. | |
path: | | |
version.txt | |
body.log | |
${{ env.binaries }} | |
# Job to prepare artifacts only if they were built. | |
release: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download Info Artifacts | |
uses: actions/download-artifact@v3 | |
with: | |
name: data | |
- name: Read Version and Binaries | |
run: | | |
# Print what we have in the result. | |
ls -la | |
echo "--- version.txt ---" | |
cat version.txt | |
echo "--- body.log ---" | |
cat body.log | |
echo "version=$(cat version.txt)" >> $GITHUB_ENV | |
- name: Make Release in repo | |
uses: ncipollo/release-action@v1 | |
with: | |
name: ${{ env.version }} | |
bodyFile: "body.log" | |
token: ${{ secrets.GHACTIONS_PUBLIC_REPO_RW }} # Note that it is custom PAT which expires. | |
artifacts: aggregate-inecobank-statements-* |