Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#193 CI part refactoring #212

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
name: Build binary and Docker images

on:
workflow_call:

env:
go-version: '1.22.6'
docker-io-registry: greenmask
docker-ghcr-registry: greenmaskio
app-name: greenmask

jobs:
build-binary:
runs-on: ubuntu-22.04
strategy:
matrix:
platforms:
- 'windows/arm64'
- 'windows/amd64'
- 'darwin/amd64'
- 'darwin/arm64'
- 'linux/amd64'
- 'linux/arm64'
- 'linux/arm/v6'
- 'linux/arm/v7'
- 'linux/ppc64le'
- 'linux/riscv64'
- 'linux/s390x'
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.go-version }}

- name: Build with different arch
run: |
export GOOS=$(echo ${{ matrix.platforms }} | cut -d '/' -f 1)
export GOARCH=$(echo ${{ matrix.platforms }} | cut -d '/' -f 2)
export GOARM=$(echo ${{ matrix.platforms }} | cut -d '/' -f 3 | cut -d 'v' -f 2)
if [[ "$GOOS" == "windows" ]]; then
make build CMD_NAME="builds/${{ env.app-name }}.exe"
else
make build CMD_NAME="builds/${{ env.app-name }}"
fi

- name: Create checksum
if: startsWith(github.ref, 'refs/tags/v')
working-directory: builds
run: |
find . -type f -exec shasum -a 256 -b {} + | sed 's# \*\./# *#' | while read sum file; do echo "$sum $file" > "${file#\*}".sha256; done

- name: Create archive
if: startsWith(github.ref, 'refs/tags/v')
run: |
export GOOS=$(echo ${{ matrix.platforms }} | cut -d '/' -f 1)
export GOARCH=$(echo ${{ matrix.platforms }} | cut -d '/' -f 2)
export GOARM=$(echo ${{ matrix.platforms }} | cut -d '/' -f 3 | cut -d 'v' -f 2)
export ARCHIVE_NAME=$(echo "${{ env.app-name }}-${GOOS}-${GOARCH}$(if [ -n "${GOARM}" ]; then echo v${GOARM}; fi).$(if [ "${GOOS}" = "windows" ]; then echo "zip"; else echo "tar.gz"; fi)")
cp LICENSE builds/
cd builds
if [[ "$GOOS" == "windows" ]]; then
zip "${ARCHIVE_NAME}" *
else
tar -czvf "${ARCHIVE_NAME}" *
fi
find . -maxdepth 1 -type f ! -name ${ARCHIVE_NAME} -exec rm -f {} +

- name: GitHub Release
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
files: builds/*

build-docker-images-and-push:
runs-on: ubuntu-22.04
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get Git tag
run: echo "TAG=$(git tag --points-at HEAD)" >> $GITHUB_ENV

- name: Build docker image and push
uses: docker/build-push-action@v5
with:
file: docker/greenmask/Dockerfile
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ env.docker-io-registry }}/${{ env.app-name }}:${{ env.TAG }}
ghcr.io/${{ env.docker-ghcr-registry }}/${{ env.app-name }}:${{ env.TAG }}

- name: Build docker imaget with latest tag and push
uses: docker/build-push-action@v5
if: |
! contains(github.ref, 'rc') &&
! contains(github.ref, 'dev') &&
! contains(github.ref, 'pre') &&
! contains(github.ref, 'beta') &&
! contains(github.ref, 'b')
with:
file: docker/greenmask/Dockerfile
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ env.docker-io-registry }}/${{ env.app-name }}:latest
ghcr.io/${{ env.docker-ghcr-registry }}/${{ env.app-name }}:latest
Loading