Skip to content

Build

Build #45

Workflow file for this run

name: Build
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *'
jobs:
shellcheck:
name: Test
uses: ./.github/workflows/check.yml
build:
name: Build
needs: shellcheck
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
-
name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Get remote package data
uses: wei/curl@master
with:
args: --silent "https://pkgs.alpinelinux.org/package/edge/community/x86_64/munin" --output ./package_data.html
-
name: Get tag from package data
run: |
#!/bin/bash
latest_tag=$(grep -A 3 -i version ./package_data.html | sed -E 's#.*>([0-9\.]+)-r[0-9]+</a>#\1#g' | tail -1 | sed 's/^[ \t]*//;s/[ \t]*$//')
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
-
name: Check if the new tag exists in DockerHub
id: check-tag
run: |
#!/bin/bash
dockerhub_tags=$(curl -s https://hub.docker.com/v2/repositories/${{ secrets.DOCKERHUB_REPO }}/tags | jq -r '.results[].name')
if echo "${dockerhub_tags}" | grep -q "${{ env.latest_tag }}"; then
echo "exists=true" >> $GITHUB_ENV
else
echo "exists=false" >> $GITHUB_ENV
fi
-
name: Set up Docker Buildx
if: env.exists == 'false'
uses: docker/setup-buildx-action@v3
-
name: Prepare Docker build
if: env.exists == 'false'
id: prepare
run: |
PLATFORMS="linux/amd64,linux/arm64"
TAGS=()
TAGS=("${{ secrets.DOCKERHUB_REPO }}:latest")
TAGS+=("${{ secrets.DOCKERHUB_REPO }}:${{ env.latest_tag }}")
TAGS+=("ghcr.io/${{ github.repository }}:latest")
TAGS+=("ghcr.io/${{ github.repository }}:${{ env.latest_tag }}")
echo "tags=${TAGS[@]}" >> $GITHUB_OUTPUT
echo "version=${{ env.latest_tag }}" >> $GITHUB_OUTPUT
echo "docker_platforms=${PLATFORMS}" >> $GITHUB_OUTPUT
echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
-
name: Login into Docker Hub
if: env.exists == 'false'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login to GitHub Container Registry
if: env.exists == 'false'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build Docker image
if: env.exists == 'false'
run: |
TAGS=(${{ steps.prepare.outputs.tags }})
echo "Build date: ${{ steps.prepare.outputs.build_date }}"
echo "Docker platform: ${{ steps.prepare.outputs.docker_platforms }}"
echo "Tags: ${TAGS}"
docker buildx build --platform ${{ steps.prepare.outputs.docker_platforms }} \
--output "type=image,push=true" \
--progress=plain \
--build-arg "BUILD_ARG=${GITHUB_RUN_ID}" \
--build-arg "VERSION_ARG=${{ steps.prepare.outputs.version }}" \
--build-arg "DATE_ARG=${{ steps.prepare.outputs.build_date }}" \
--build-arg "VCS_REF=${GITHUB_SHA::8}" \
$(printf "%s" "${TAGS[@]/#/ --tag }" ) .
-
name: Clear Docker credentials
if: env.exists == 'false'
run: |
rm -f ${HOME}/.docker/config.json