add unit test execution step to GitHub Actions workflow for improved CI #11
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: Docker Image CI | |
on: | |
push: | |
branches: [ "main" ] | |
tags: | |
- 'v*' # Trigger on any tag starting with 'v' | |
pull_request: | |
branches: [ "main" ] | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Fetch all history for proper versioning | |
- name: Extract version info | |
id: version_info | |
run: | | |
# Get version from tag or generate from commit | |
if [[ $GITHUB_REF == refs/tags/v* ]]; then | |
echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV | |
else | |
echo "VERSION=dev-$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
fi | |
echo "VERSION=${{ env.VERSION }}" >> $GITHUB_ENV | |
echo "COMMIT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
echo "TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> $GITHUB_ENV | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '1.24' | |
- name: Run unit tests | |
run: | | |
go mod download | |
go test -v ./... | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to the Container registry | |
# Only login when we'll be pushing (main branch or tags, never on PR) | |
if: (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=ref,event=branch | |
type=ref,event=pr | |
type=ref,event=tag | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=sha,prefix=,suffix=,format=short | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64,linux/arm/v7 | |
# Only push for main branch or version tags, never on PR | |
push: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
build-args: | | |
VERSION=${{ env.VERSION }} | |
GIT_COMMIT=${{ env.COMMIT }} | |
BUILD_TIME=${{ env.TIMESTAMP }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |