Skip to content

Build docker image [dry_run=, ghcr.io/controllan/docker-state-exporter] #114

Build docker image [dry_run=, ghcr.io/controllan/docker-state-exporter]

Build docker image [dry_run=, ghcr.io/controllan/docker-state-exporter] #114

name: Docker build & push
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
on:
push:
branches: [ main ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ main ]
workflow_dispatch:
inputs:
dry_run:
type: boolean
default: false
description: "enable dry run"
required: false
tags:
type: string
default: ""
description: "The list of tags to be added to the image"
required: false
labels:
type: string
default: ""
description: "The list of labels to be added to the image"
required: false
force_docker_push:
type: boolean
default: false
description: "force docker push"
required: false
run-name: "Build docker image [dry_run=${{ inputs.dry_run }}, ${{ vars.CONTAINER_REGISTRY_URL }}/${{ vars.CONTAINER_REGISTRY_REPOSITORY }}]"
env:
TEST_TAG: controllan/docker-state-exporter:test
jobs:
build:
runs-on: [ self-hosted, ubuntu-latest, '${{ vars.RUNNER_LABEL_NAME }}' ]
name: "Build docker image [dry_run=${{ inputs.dry_run }}, ${{ vars.CONTAINER_REGISTRY_URL }}/${{ vars.CONTAINER_REGISTRY_REPOSITORY }}]"
permissions:
contents: read
packages: write
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Checkout repository
uses: actions/checkout@v4.2.1
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ vars.CONTAINER_REGISTRY_URL }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3.3.0
with:
registry: ${{ vars.CONTAINER_REGISTRY_URL }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5.5.1
with:
images: ${{ vars.CONTAINER_REGISTRY_URL }}/${{ vars.CONTAINER_REGISTRY_REPOSITORY }}
# Build test docker image
# https://github.com/docker/build-push-action
- name: Build test image [${{ env.TEST_TAG }}]
uses: docker/build-push-action@v6.9.0
with:
context: .
load: true
tags: ${{ env.TEST_TAG }}
- name: Run docker image and verify if it is running
env:
IMAGE_TAG: ${{ env.TEST_TAG }}
PORT: ${{ vars.DOCKER_TEST_PORT }}
WAIT_TIME: ${{ vars.DOCKER_TEST_WAIT_TIME }}
run: |
docker compose up -d
sleep $WAIT_TIME
HEALTH_STATUS=$(docker inspect --format "{{json .State.Health }}" docker_state_exporter | jq -r ".Status")
docker compose down -v
if [ "$HEALTH_STATUS" != "healthy" ]
then
echo "Docker state exporter has an unhealthy status and therefore seems not to work."
exit 1
fi
shell: bash
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image [${{ vars.CONTAINER_REGISTRY_URL }}/${{ vars.CONTAINER_REGISTRY_REPOSITORY }}]
uses: docker/build-push-action@v6.9.0
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ (github.event_name != 'pull_request' || inputs.force_docker_push == true ) && inputs.dry_run != true }}
tags: '${{ steps.meta.outputs.tags || inputs.tags }}'
labels: ${{ steps.meta.outputs.labels || inputs.labels }}