Skip to content

Build multi-arch images #9

Build multi-arch images

Build multi-arch images #9

Workflow file for this run

name: "Build multi-arch images"
on:
push:
tags:
- "v*.*.*"
jobs:
start-runner:
name: "Start self-hosted EC2 runner"
runs-on: "ubuntu-latest"
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: "Configure AWS credentials"
uses: "aws-actions/configure-aws-credentials@v1"
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: "Start EC2 runner"
id: "start-ec2-runner"
uses: "xJonathanLEI/ec2-github-runner@main"
with:
mode: "start"
github-token: ${{ secrets.GH_PAT }}
ec2-image-id: "ami-01a786eb497a27d2a"
ec2-instance-type: "c6g.8xlarge"
subnet-id: "subnet-0f178a06b3c5e09dd"
security-group-id: "sg-0db4b2850585ebe80"
storage-size: 256
image-info:
name: "Extract crate info"
runs-on: "ubuntu-latest"
outputs:
repo: ${{ steps.derive.outputs.repo }}
tag: ${{ steps.derive.outputs.tag }}
env:
DOCKER_REPOSITORY: "starknet/juno-firehose"
steps:
- id: "derive"
name: "Derive image info from Git tag"
run: |
echo "repo=${DOCKER_REPOSITORY}" >> $GITHUB_OUTPUT
FULL_REF="${{ github.ref }}"
REGEX="^refs\/tags\/v(.*)$"
[[ $FULL_REF =~ $REGEX ]];
echo "tag=${DOCKER_REPOSITORY}:${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT
build-amd64:
name: "Build for linux/amd64"
runs-on: "ubuntu-larger"
needs:
- "image-info"
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
- name: "Build Docker image"
run: |
docker build \
-t ${{ needs.image-info.outputs.tag }}-amd64 -f ./Dockerfile .
- name: "Export Docker image"
run: |
docker save ${{ needs.image-info.outputs.tag }}-amd64 | gzip > /tmp/amd64.tar.gz
- name: "Upload Docker image artifact"
uses: "actions/upload-artifact@v3"
with:
name: "amd64.tar.gz"
path: "/tmp/amd64.tar.gz"
build-arm64:
name: "Build for linux/arm64"
runs-on: "${{ needs.start-runner.outputs.label }}"
needs:
- "image-info"
- "start-runner"
steps:
- name: "Checkout"
uses: "actions/checkout@v3"
- name: "Install Docker"
run: |
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# Install the latest version
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- name: "Build Docker image"
run: |
docker build \
-t ${{ needs.image-info.outputs.tag }}-arm64 -f ./Dockerfile .
- name: "Export Docker image"
run: |
docker save ${{ needs.image-info.outputs.tag }}-arm64 | gzip > /tmp/arm64.tar.gz
- name: "Upload Docker image artifact"
uses: "actions/upload-artifact@v3"
with:
name: "arm64.tar.gz"
path: "/tmp/arm64.tar.gz"
push:
name: "Push multi-arch manifest"
runs-on: "ubuntu-latest"
needs:
- "image-info"
- "build-amd64"
- "build-arm64"
steps:
- name: "Download linux/amd64 image"
uses: "actions/download-artifact@v3"
with:
name: "amd64.tar.gz"
path: "/tmp/"
- name: "Download linux/arm64/v8 image"
uses: "actions/download-artifact@v3"
with:
name: "arm64.tar.gz"
path: "/tmp/"
- name: "Load Docker images"
run: |
docker load < /tmp/amd64.tar.gz
docker load < /tmp/arm64.tar.gz
- name: "Login to Docker Hub"
uses: "docker/[email protected]"
with:
username: "${{ secrets.DOCKER_HUB_USERNAME }}"
password: "${{ secrets.DOCKER_HUB_PASSWORD }}"
- name: "Push Docker image"
run: |
docker push ${{ needs.image-info.outputs.tag }}-amd64
docker push ${{ needs.image-info.outputs.tag }}-arm64
docker manifest create ${{ needs.image-info.outputs.tag }} \
${{ needs.image-info.outputs.tag }}-amd64 \
${{ needs.image-info.outputs.tag }}-arm64
docker manifest create ${{ needs.image-info.outputs.repo }}:latest \
${{ needs.image-info.outputs.tag }}-amd64 \
${{ needs.image-info.outputs.tag }}-arm64
docker manifest push ${{ needs.image-info.outputs.tag }}
docker manifest push ${{ needs.image-info.outputs.repo }}:latest
stop-runner:
name: "Stop self-hosted EC2 runner"
runs-on: "ubuntu-latest"
needs:
- "start-runner"
- "build-arm64"
if: ${{ always() }}
steps:
- name: "Configure AWS credentials"
uses: "aws-actions/configure-aws-credentials@v1"
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: "Stop EC2 runner"
uses: "xJonathanLEI/ec2-github-runner@main"
with:
mode: "stop"
github-token: ${{ secrets.GH_PAT }}
label: "${{ needs.start-runner.outputs.label }}"
ec2-instance-id: "${{ needs.start-runner.outputs.ec2-instance-id }}"