From 64dec294c976ddd2ed90ae094710eb07b521f21a Mon Sep 17 00:00:00 2001 From: pat-git Date: Sat, 21 Sep 2024 12:52:36 +0200 Subject: [PATCH] ci(docker-publish): verify that the image can be run successfully. --- .github/workflows/docker-publish.yml | 33 ++++++++++++++++++++++++++++ README.md | 4 ++-- docker-compose.yaml | 20 +++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 docker-compose.yaml diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 64ed24c..0c47751 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -37,6 +37,9 @@ on: 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 }}' ] @@ -69,6 +72,36 @@ jobs: 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.7.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 }}] diff --git a/README.md b/README.md index 0e129cc..b314c94 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ For Docker compose. ```yaml --- version: '3.8' - services: docker_state_exporter: - image: ghcr.io/controllan/docker-state-exporter + image: ghcr.io/controllan/docker-state-exporter:latest + container_name: docker_state_exporter volumes: - type: bind source: /var/run/docker.sock diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..9255297 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,20 @@ +--- +version: '3.8' +services: + docker_state_exporter: + image: ${IMAGE_TAG:-ghcr.io/controllan/docker-state-exporter:latest} + container_name: docker_state_exporter + volumes: + - type: bind + source: /var/run/docker.sock + target: /var/run/docker.sock + ports: + - "${PORT:-8080}:${PORT:-8080}" + command: + - --no-add-container-labels # Keep this if you do not wand to add container labels. + - --web.listen-address=:${PORT:-8080} # Set the port of the docker state exporter + healthcheck: + test: "wget -q -O - localhost:${PORT:-8080}/metrics | grep -q ',status=\"running\"} 1'" + interval: 3s + timeout: 10s + retries: 3 \ No newline at end of file