From 5c9bd1b63100597a2f37590103b5b78d5b3076ac Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Mon, 2 Dec 2024 16:25:00 +0100 Subject: [PATCH 1/2] allow to run docker ci manually by entering a branch name date time will be used so multiple images can be built and publish per branch. --- .github/workflows/docker-custom.yaml | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 .github/workflows/docker-custom.yaml diff --git a/.github/workflows/docker-custom.yaml b/.github/workflows/docker-custom.yaml new file mode 100644 index 00000000000..289dcbe7ea4 --- /dev/null +++ b/.github/workflows/docker-custom.yaml @@ -0,0 +1,80 @@ +# This workflow builds docker images on given custom 'branch name'. +name: Custom Docker + +# Limit concurrent runs of this workflow within a single PR +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +on: + workflow_dispatch: + inputs: + branch_name: + type: string + description: 'Used to publish a hydra-node-{datetime}-{branch_name} docker image' + required: true + default: "master" + +permissions: + packages: write + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: 📥 Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event.inputs.branch_name }} + + - name: 🐳 Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: ❄ Prepare nix + uses: cachix/install-nix-action@v30 + with: + extra_nix_config: | + accept-flake-config = true + log-lines = 1000 + + - name: ❄ Cachix cache of nix derivations + uses: cachix/cachix-action@v15 + with: + name: cardano-scaling + authToken: '${{ secrets.CACHIX_CARDANO_SCALING_AUTH_TOKEN }}' + + - name: 🔨 Build image using nix + run: | + IMAGE_NAME=ghcr.io/${{github.repository_owner}}/hydra-node + echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV + nix build .#docker-hydra-node + ./result | docker load + + VERSION_NAME=${{github.ref_name}} + + BRANCH_NAME="${{ github.event.inputs.branch_name }}" + [[ -z "${BRANCH_NAME}" ]] && { echo "branch_name is required"; exit 1; } + + DATETIME=$(date '+%Y%m%d%H%M') + CUSTOM_TAG="${DATETIME}-${BRANCH_NAME}" + echo "CUSTOM_TAG=${CUSTOM_TAG}" >> $GITHUB_ENV + + # Use 'FROM' instruction to use docker build with --label + echo "FROM hydra-node" | docker build \ + --label org.opencontainers.image.source=https://github.com/cardano-scaling/hydra \ + --label org.opencontainers.image.licenses=Apache-2.0 \ + --label org.opencontainers.image.created=$(date -Is) \ + --label org.opencontainers.image.revision=${{github.sha}} \ + --label org.opencontainers.image.version=${VERSION_NAME:-unstable} \ + --tag ${IMAGE_NAME}:${CUSTOM_TAG} - + + docker images + docker inspect ${IMAGE_NAME}:${CUSTOM_TAG} + + - name: 📤 Push to registry + run: | + docker push "${IMAGE_NAME}:${CUSTOM_TAG}" From 491260e092bdc308994c24c1bb15003410565445 Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Tue, 3 Dec 2024 12:50:09 +0100 Subject: [PATCH 2/2] replace custom workflow by enhancing the existing docker one --- .github/workflows/docker-custom.yaml | 80 ---------------------------- .github/workflows/docker.yaml | 37 ++++++++++--- 2 files changed, 31 insertions(+), 86 deletions(-) delete mode 100644 .github/workflows/docker-custom.yaml diff --git a/.github/workflows/docker-custom.yaml b/.github/workflows/docker-custom.yaml deleted file mode 100644 index 289dcbe7ea4..00000000000 --- a/.github/workflows/docker-custom.yaml +++ /dev/null @@ -1,80 +0,0 @@ -# This workflow builds docker images on given custom 'branch name'. -name: Custom Docker - -# Limit concurrent runs of this workflow within a single PR -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -on: - workflow_dispatch: - inputs: - branch_name: - type: string - description: 'Used to publish a hydra-node-{datetime}-{branch_name} docker image' - required: true - default: "master" - -permissions: - packages: write - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: 📥 Checkout repository - uses: actions/checkout@v4 - with: - ref: ${{ github.event.inputs.branch_name }} - - - name: 🐳 Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: ❄ Prepare nix - uses: cachix/install-nix-action@v30 - with: - extra_nix_config: | - accept-flake-config = true - log-lines = 1000 - - - name: ❄ Cachix cache of nix derivations - uses: cachix/cachix-action@v15 - with: - name: cardano-scaling - authToken: '${{ secrets.CACHIX_CARDANO_SCALING_AUTH_TOKEN }}' - - - name: 🔨 Build image using nix - run: | - IMAGE_NAME=ghcr.io/${{github.repository_owner}}/hydra-node - echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV - nix build .#docker-hydra-node - ./result | docker load - - VERSION_NAME=${{github.ref_name}} - - BRANCH_NAME="${{ github.event.inputs.branch_name }}" - [[ -z "${BRANCH_NAME}" ]] && { echo "branch_name is required"; exit 1; } - - DATETIME=$(date '+%Y%m%d%H%M') - CUSTOM_TAG="${DATETIME}-${BRANCH_NAME}" - echo "CUSTOM_TAG=${CUSTOM_TAG}" >> $GITHUB_ENV - - # Use 'FROM' instruction to use docker build with --label - echo "FROM hydra-node" | docker build \ - --label org.opencontainers.image.source=https://github.com/cardano-scaling/hydra \ - --label org.opencontainers.image.licenses=Apache-2.0 \ - --label org.opencontainers.image.created=$(date -Is) \ - --label org.opencontainers.image.revision=${{github.sha}} \ - --label org.opencontainers.image.version=${VERSION_NAME:-unstable} \ - --tag ${IMAGE_NAME}:${CUSTOM_TAG} - - - docker images - docker inspect ${IMAGE_NAME}:${CUSTOM_TAG} - - - name: 📤 Push to registry - run: | - docker push "${IMAGE_NAME}:${CUSTOM_TAG}" diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index d736c529545..4f4af26a693 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -13,6 +13,13 @@ on: push: branches: [ "master" ] tags: [ "*.*.*" ] + workflow_dispatch: + inputs: + ref_name: + type: string + description: 'Point-in-time to build the custom docker images' + required: true + default: "master" permissions: packages: write @@ -27,6 +34,8 @@ jobs: steps: - name: 📥 Checkout repository uses: actions/checkout@v4 + with: + ref: ${{ github.event.inputs.ref_name || '' }} - name: 🐳 Login to GitHub Container Registry uses: docker/login-action@v3 @@ -54,25 +63,41 @@ jobs: echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV nix build .#docker-${{ matrix.target }} ./result | docker load + + IMAGE_LABEL=unstable + BUILDING_WORKFLOW_DISPATCH=${{github.event_name == 'workflow_dispatch'}} + [[ ${BUILDING_WORKFLOW_DISPATCH} = true ]] && \ + IMAGE_LABEL=workflow_dispatch-${{github.event.inputs.ref_name}} + + IS_TAG=${{github.ref_type == 'tag'}} + + # Only build say we are building a tag if it's a tag _and_ not part of a + # workflow-dispatch task + [[ ${IS_TAG} = true && ${BUILDING_WORKFLOW_DISPATCH} = false ]] && \ + BUILDING_TAG=true || + BUILDING_TAG=false + # Determine whether we are building a tag and if yes, set a VERSION_NAME - BUILDING_TAG=${{github.ref_type == 'tag'}} [[ ${BUILDING_TAG} = true ]] && \ VERSION_NAME=${{github.ref_name}} + # Use 'FROM' instruction to use docker build with --label echo "FROM ${{matrix.target}}" | docker build \ --label org.opencontainers.image.source=https://github.com/cardano-scaling/hydra \ --label org.opencontainers.image.licenses=Apache-2.0 \ --label org.opencontainers.image.created=$(date -Is) \ --label org.opencontainers.image.revision=${{github.sha}} \ - --label org.opencontainers.image.version=${VERSION_NAME:-unstable} \ - --tag ${IMAGE_NAME}:unstable - + --label org.opencontainers.image.version=${VERSION_NAME:-${IMAGE_LABEL}} \ + --tag ${IMAGE_NAME}:${IMAGE_LABEL} - + # Also tag with semver and 'latest' if we are building a tag [[ ${BUILDING_TAG} = true && ${{matrix.target}} != "hydraw" ]] && \ - docker tag ${IMAGE_NAME}:unstable ${IMAGE_NAME}:${{github.ref_name}} + docker tag ${IMAGE_NAME}:${IMAGE_LABEL} ${IMAGE_NAME}:${{github.ref_name}} [[ ${BUILDING_TAG} = true ]] && \ - docker tag ${IMAGE_NAME}:unstable ${IMAGE_NAME}:latest + docker tag ${IMAGE_NAME}:${IMAGE_LABEL} ${IMAGE_NAME}:latest + docker images - docker inspect ${IMAGE_NAME}:unstable + docker inspect ${IMAGE_NAME}:${IMAGE_LABEL} - name: 📤 Push to registry run: |