Skip to content

Build and Push Docker Images to GHCR #87

Build and Push Docker Images to GHCR

Build and Push Docker Images to GHCR #87

name: Build and Push Docker Images to GHCR
on:
workflow_dispatch:
inputs:
tag:
description: "Comma-separated list of image tags for manual builds (e.g., 'preprod' or 'latest,v2,v2.1,v2.1.2')."
required: true
type: string
pull_request:
types: [opened, synchronize]
branches:
- '!misc/**'
paths:
- "**.go"
- "**/Dockerfile"
jobs:
setup:
name: Setup workflow
runs-on: ubuntu-latest
environment: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
outputs:
short_sha: ${{ steps.set_outputs.outputs.short_sha }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v6
- name: Set outputs
id: set_outputs
shell: bash
run: |
echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
test:
name: Run Go tests
runs-on: ubuntu-latest
environment: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
steps:
- name: Checkout code
id: checkout
uses: actions/checkout@v6
- name: Set up Go
id: setup_go
uses: actions/setup-go@v6
with:
go-version: "1.25"
- name: Run tests
id: run_tests
run: |
go test ./...
parse_tags:
name: Parse tags
runs-on: ubuntu-latest
environment: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
outputs:
tags: ${{ steps.parse_tags.outputs.tags }}
has_custom_tags: ${{ steps.parse_tags.outputs.has_custom_tags }}
steps:
- name: Parse tags
id: parse_tags
shell: bash
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ -n "${{ github.event.inputs.tag }}" ]; then
echo "Parsing tags from input: '${{ github.event.inputs.tag }}'"
# Parse comma-separated tags and output as multiline string for docker/metadata-action
IFS=',' read -ra TAGS <<< "${{ github.event.inputs.tag }}"
TAGS_OUTPUT=""
for tag in "${TAGS[@]}"; do
tag=$(echo "$tag" | xargs) # trim whitespace
if [ -n "$tag" ]; then
TAGS_OUTPUT="${TAGS_OUTPUT}type=raw,value=${tag}"$'\n'
echo "Added tag: ${tag}"
fi
done
echo "tags<<EOF" >> $GITHUB_OUTPUT
echo "$TAGS_OUTPUT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "has_custom_tags=true" >> $GITHUB_OUTPUT
echo "Parsed tags successfully"
else
echo "tags=" >> $GITHUB_OUTPUT
echo "has_custom_tags=false" >> $GITHUB_OUTPUT
echo "No custom tags provided (event: ${{ github.event_name }}, has tag input: $([ -n '${{ github.event.inputs.tag }}' ] && echo 'yes' || echo 'no'))"
fi
health-checker:
name: Build health-checker image
runs-on: ubuntu-latest
# Run for all configured events, but skip pull requests from forks
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
environment: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
needs:
- setup
- test
- parse_tags
steps:
- name: Checkout code
id: checkout
uses: actions/checkout@v6
- name: Set up Go
id: setup_go
uses: actions/setup-go@v6
with:
go-version: "1.25"
- name: Log in to GitHub Container Registry
id: login_ghcr
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.AETHERLAY_GITHUB_TOKEN }}
- name: Docker meta for health-checker
id: meta_hc
uses: docker/metadata-action@v5
with:
images: ghcr.io/project-aethermesh/aetherlay/aetherlay-hc
tags: |
type=raw,value=latest,enable=${{ github.ref_name == github.event.repository.default_branch && github.event_name != 'pull_request' && needs.parse_tags.outputs.has_custom_tags != 'true' }}
type=sha,format=short,enable=${{ github.ref_name == github.event.repository.default_branch && github.event_name != 'pull_request' }}
type=raw,value=pr-${{ github.event.pull_request.number }},enable=${{ github.event_name == 'pull_request' }}
${{ needs.parse_tags.outputs.tags }}
labels: |
org.opencontainers.image.vendor=Project Aethermesh
org.opencontainers.image.licenses=AGPL-3.0
- name: Debug - Show final tags for health-checker
run: |
echo "Final tags that will be applied:"
echo "${{ steps.meta_hc.outputs.tags }}"
- name: Build and push health-checker image
id: container_image_hc
uses: docker/build-push-action@v6
with:
context: .
file: ./services/health-checker/Dockerfile
push: true
tags: ${{ steps.meta_hc.outputs.tags }}
labels: ${{ steps.meta_hc.outputs.labels }}
load-balancer:
name: Build load-balancer image
runs-on: ubuntu-latest
# Run for all configured events, but skip pull requests from forks
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
environment: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
needs:
- setup
- test
- parse_tags
steps:
- name: Checkout code
id: checkout_lb
uses: actions/checkout@v6
- name: Set up Go
id: setup_go
uses: actions/setup-go@v6
with:
go-version: "1.25"
- name: Log in to GitHub Container Registry
id: login_ghcr_lb
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.AETHERLAY_GITHUB_TOKEN }}
- name: Docker meta for load-balancer
id: meta_lb
uses: docker/metadata-action@v5
with:
images: ghcr.io/project-aethermesh/aetherlay/aetherlay-lb
tags: |
type=raw,value=latest,enable=${{ github.ref_name == github.event.repository.default_branch && github.event_name != 'pull_request' && needs.parse_tags.outputs.has_custom_tags != 'true' }}
type=sha,format=short,enable=${{ github.ref_name == github.event.repository.default_branch && github.event_name != 'pull_request' }}
type=raw,value=pr-${{ github.event.pull_request.number }},enable=${{ github.event_name == 'pull_request' }}
${{ needs.parse_tags.outputs.tags }}
labels: |
org.opencontainers.image.vendor=Project Aethermesh
org.opencontainers.image.licenses=AGPL-3.0
- name: Debug - Show final tags for load-balancer
run: |
echo "Final tags that will be applied:"
echo "${{ steps.meta_lb.outputs.tags }}"
- name: Build and push load-balancer image
id: container_image_lb
uses: docker/build-push-action@v6
with:
context: .
file: ./services/load-balancer/Dockerfile
push: true
tags: ${{ steps.meta_lb.outputs.tags }}
labels: ${{ steps.meta_lb.outputs.labels }}