Skip to content

Commit

Permalink
ci: add new workflow for multiarch
Browse files Browse the repository at this point in the history
  • Loading branch information
skylenet committed May 15, 2024
1 parent 10c7ae1 commit ccc6cff
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 40 deletions.
78 changes: 78 additions & 0 deletions .github/actions/deploy/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Deploy
description: Build and push a docker image to Docker Hub

outputs:
tag:
description: "Tags for the docker image"
value: ${{ steps.meta.outputs.tags[0] }}

inputs:
platform:
description: The platform to build for
type: string
required: true
build_args:
description: Build arguments to pass to the Docker build
default: ""
type: string
required: false
tag:
description: Docker hub tag to push to
type: string
required: true
# Secrets
DOCKER_USERNAME:
required: true
DOCKER_PASSWORD:
required: true
MACOS_PASSWORD:
required: true

runs:
using: composite
steps:
- name: Checkout this repo
uses: actions/checkout@v4
- name: Set up Docker Context for Buildx
shell: bash
id: buildx-context
run: |
docker context use builders || docker context create builders
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
endpoint: builders
- name: Unlock MacOS keychain for Docker Hub login
shell: bash
if: runner.os == 'macOS'
run: |
security -v unlock-keychain -p ${{ inputs.MACOS_PASSWORD }} ~/Library/Keychains/login.keychain-db
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ inputs.DOCKER_USERNAME }}
password: ${{ inputs.DOCKER_PASSWORD }}
- name: Generate slug
id: slzug
shell: bash
run: |
echo "slug=$(echo "$platform" | tr '/' '-')
- name: Docker build & push
id: docker_build
uses: docker/build-push-action@v5
with:
context: '.'
file: Dockerfile
tags: ${{ steps.meta.outputs.tags[0] }}-${{ steps.vars.outputs.slug }}
push: true
platforms: ${{ inputs.platform }}
build-args: ${{ inputs.build_args }}
- name: Image digest & tags
shell: bash
if: ${{ inputs.build_script == '' }}
run: |
cat << EOF
digest: ${{ steps.docker_build.outputs.digest }}
tags:
${{ steps.meta.outputs.tags[0] }}
EOF
72 changes: 72 additions & 0 deletions .github/actions/manifest/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Manifest
description: Build and push a docker manifest to Docker Hub

inputs:
platforms:
# eg [{"platform":"linux/amd64", "runner": "ARM64", "slug": "something-arm64"},{"platform":"linux/arm64", "runner": "ubuntu-latest", "slug": "something-amd64"}]
description: JSON list of platforms to build for
type: string
required: true
tag:
description: Docker hub tag to push to
type: string
required: true
repository:
description: Docker hub repository to push to
type: string
required: true
DOCKER_USERNAME:
required: true
DOCKER_PASSWORD:
required: true

runs:
using: composite
steps:
- name: Checkout this repo
uses: actions/checkout@v4
- name: Generate images list
id: generate_images_list
shell: bash
run: |
PLATFORMS='${{ inputs.platforms }}'
# Iterate over the platforms and build the image list
len=$(echo $PLATFORMS | jq '. | length')
for ((i=0; i<$len; i++)); do
slug=$(echo $PLATFORMS | jq -r --argjson i $i '.[$i].slug')
imagetag="${{ inputs.tag }}-$slug"
image="${{ inputs.repository }}:$imagetag"
url="https://hub.docker.com/v2/repositories/${{ inputs.repository }}/tags?page_size=25&page=1&ordering=&name=$imagetag"
exists=$(curl -s $url | jq '.results | length > 0')
if [ "$exists" == "true" ]; then
IMAGES+="${{ inputs.repository }}:${{ inputs.tag }}-$slug "
fi
done
IMAGES=${IMAGES::-1} # Remove the trailing space
echo "IMAGES: $IMAGES"
echo "images=$IMAGES" >> $GITHUB_OUTPUT
- name: Check if there is atleast one image
if: ${{ steps.generate_images_list.outputs.images == '[]' || steps.generate_images_list.outputs.images == '' }}
shell: bash
run: exit 1
- name: Set up Docker Context for Buildx
shell: bash
id: buildx-context
run: |
docker context create builders
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
endpoint: builders
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ inputs.DOCKER_USERNAME }}
password: ${{ inputs.DOCKER_PASSWORD }}
- name: Create and push manifest images
shell: bash
run: |
docker buildx imagetools create --dry-run -t ${{ inputs.repository }}:${{ inputs.tag }} ${{ steps.generate_images_list.outputs.images }}
docker buildx imagetools create -t ${{ inputs.repository }}:${{ inputs.tag }} -t ${{ inputs.repository }}:${{ inputs.tag }} ${{ steps.generate_images_list.outputs.images }}
43 changes: 43 additions & 0 deletions .github/actions/prepare/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: 'Setup'
description: 'Read and parse config files for builds'
outputs:
platforms:
description: "Matrix of platforms and runner to use"
value: ${{ steps.setup_platforms.outputs.platforms }}
tag:
description: "Tags for the docker image"
value: ${{ steps.meta.outputs.tags[0] }}
runs:
using: "composite"
steps:
- name: Checkout repository
uses: actions/checkout@v4
- uses: mikefarah/yq@v4.35.1
- name: Generate platform and runner matrix from config files
id: setup_platforms
shell: bash
run: |
PLATFORMS_JSON="["
# Extract the platforms
platforms=$(yq e ".ethereum-genesis-generator[]" platforms.yaml)
for platform in $platforms; do
slug=$(echo "$platform" | tr '/' '-')
runner=$(yq e ".\"$platform\"" runners.yaml)
PLATFORMS_JSON+="{\"platform\":\"$platform\", \"runner\":\"$runner\", \"slug\":\"$slug\"},"
done
PLATFORMS_JSON="${PLATFORMS_JSON%,}]"
echo "platforms=$PLATFORMS_JSON" >> $GITHUB_OUTPUT
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ethpandaops/ethereum-genesis-generator
flavor: latest=true
tags: |
type=semver,pattern=verkle-gen-{{version}}
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
76 changes: 36 additions & 40 deletions .github/workflows/build-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,48 @@ on:
push:
branches:
- master
- multiarch-build
tags:
- '**'
workflow_dispatch:

jobs:
build-push:
name: build-push
prepare:
runs-on: ubuntu-latest
outputs:
platforms: ${{ steps.setup.outputs.platforms }}
steps:
- name: Checkout this repo
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Context for Buildx
shell: bash
id: buildx-context
run: |
docker context create builders
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
endpoint: builders
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ethpandaops/ethereum-genesis-generator
flavor: latest=true
tags: |
type=semver,pattern=verkle-gen-{{version}}
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
- name: Login to Docker Hub
uses: docker/login-action@v2
- uses: actions/checkout@v4
- name: Prepare Matrix
id: setup
uses: ./.github/actions/prepare
deploy:
needs:
- prepare
runs-on: ${{ matrix.runner }}
continue-on-error: true
strategy:
matrix:
include: ${{fromJson(needs.prepare.outputs.platforms)}}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/deploy
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: docker-build-push
id: docker_build
uses: docker/build-push-action@v4
platform: ${{ matrix.platform }}
tag: ethpandaops/ethereum-genesis-generator:${{ needs.prepare.outputs.tag }}
DOCKER_USERNAME: "${{ vars.DOCKER_USERNAME }}"
DOCKER_PASSWORD: "${{ secrets.DOCKER_PASSWORD }}"
manifest:
needs:
- prepare
- deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/manifest
with:
context: .
file: Dockerfile
tags: ${{ steps.meta.outputs.tags }}
push: true
platforms: linux/amd64,linux/arm64
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
tag: ${{ needs.prepare.outputs.tag }}
repository: ethpandaops/ethereum-genesis-generator
platforms: ${{ needs.prepare.outputs.platforms }}
DOCKER_USERNAME: "${{ vars.DOCKER_USERNAME }}"
DOCKER_PASSWORD: "${{ secrets.DOCKER_PASSWORD }}"

0 comments on commit ccc6cff

Please sign in to comment.