From af8c6f9c4c7f07f07597c90917289539993ee9df Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Sun, 24 Mar 2024 22:47:44 +0200 Subject: [PATCH 1/2] container build pipeline --- .github/workflows/cicd.yml | 123 +++++++++++++++++++++++++++ .github/workflows/release-event.json | 9 ++ run.sh | 31 +++++++ 3 files changed, 163 insertions(+) create mode 100644 .github/workflows/cicd.yml create mode 100644 .github/workflows/release-event.json create mode 100755 run.sh diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..d1013b6 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,123 @@ +name: CICD + +on: + push: + branches: ["**"] + tags: ["*.*.*"] + pull_request: + release: + types: [created] + +env: + REGISTRY: ghcr.io + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + strategy: + fail-fast: false + matrix: + platform: + - linux/amd64 + - linux/arm/v6 + - linux/arm/v7 + - linux/arm64 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # replaces slashes with dashes in platform name + - name: Convert platform pairs + run: | + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ github.repository }} + + - name: Build and push by digest + id: build + uses: docker/build-push-action@v5 + with: + context: . + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=${{ env.REGISTRY }}/${{ github.repository }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ env.PLATFORM_PAIR }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + runs-on: ubuntu-latest + if: ${{ github.event_name != 'pull_request' }} + needs: + - build + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ github.repository }} + tags: | + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + type=sha + type=semver,pattern={{version}} + + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) + + - name: Inspect image + run: | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} diff --git a/.github/workflows/release-event.json b/.github/workflows/release-event.json new file mode 100644 index 0000000..4010c3a --- /dev/null +++ b/.github/workflows/release-event.json @@ -0,0 +1,9 @@ +{ + "act": true, + "action": "created", + "ref": "refs/tags/v0.0.10", + "sha": "b7e12928f13caf61af40d3e8788649a1a8f24c22", + "release": { + "tag_name": "0.0.10" + } +} \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..af3e7f3 --- /dev/null +++ b/run.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Copyright 2023 Nokia +# Licensed under the BSD 3-Clause License. +# SPDX-License-Identifier: BSD-3-Clause + + +set -o errexit +set -o pipefail + + +# testing release-triggered workflow +function test-on-push { + gh act release -W '.github/workflows/cicd.yml' -e .github/workflows/release-event.json -s GITHUB_TOKEN="$(gh auth token)" --matrix platform:linux/amd64 +} + +function test-on-release { + gh act release -W '.github/workflows/cicd.yml' -e .github/workflows/release-event.json -s GITHUB_TOKEN="$(gh auth token)" --matrix platform:linux/amd64 +} + +# ----------------------------------------------------------------------------- +# Bash runner functions. +# ----------------------------------------------------------------------------- +function help { + printf "%s [args]\n\nTasks:\n" "${0}" + + compgen -A function | grep -v "^_" | cat -n + + printf "\nExtended help:\n Each task has comments for general usage\n" +} + +"${@:-help}" \ No newline at end of file From ad6498ad992af414161204c5a22293c19f3a31ba Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Sun, 24 Mar 2024 22:55:50 +0200 Subject: [PATCH 2/2] remove pull request target and fix wrong image name env var --- .github/workflows/cicd.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index d1013b6..d3fc90c 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -4,7 +4,6 @@ on: push: branches: ["**"] tags: ["*.*.*"] - pull_request: release: types: [created] @@ -116,8 +115,8 @@ jobs: working-directory: /tmp/digests run: | docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ - $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) + $(printf '${{ env.REGISTRY }}/${{ github.repository }}@sha256:%s ' *) - name: Inspect image run: | - docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} + docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ github.repository }}:${{ steps.meta.outputs.version }}