Build and Push Docker Image #141
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Image | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
on: | |
workflow_dispatch: | |
inputs: | |
shardeum_branch: | |
description: 'Shardeum repository branch' | |
required: true | |
default: 'mainnet-launch' | |
relayer_collector_branch: | |
description: 'Relayer collector repository branch' | |
required: true | |
default: 'mainnet-launch' | |
json_rpc_server_branch: | |
description: 'JSON RPC server repository branch' | |
required: true | |
default: 'mainnet-launch' | |
image_tag: | |
description: 'Docker image tag (will be combined with network name)' | |
required: true | |
push_latest: | |
description: 'Push latest tag' | |
type: boolean | |
required: true | |
default: false | |
network: | |
type: choice | |
description: 'Network. Will use the config for the network in scripts/config if it exists. Custom if you are doing a custom build and will use environment variables passed to Docker run.' | |
required: true | |
options: | |
- mainnet | |
- stagenet | |
- testnet | |
- custom | |
pull_request: | |
branches: [ main ] | |
permissions: | |
contents: read | |
packages: write | |
jobs: | |
build: | |
strategy: | |
matrix: | |
include: | |
- arch: amd64 | |
runner: [self-hosted, Linux, X64] | |
- arch: arm64 | |
runner: [self-hosted, Linux, ARM64] | |
runs-on: ${{ matrix.runner }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set build args and tags | |
id: build_config | |
run: | | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
echo "SHARDEUM_BRANCH=dev" >> $GITHUB_OUTPUT | |
echo "RELAYER_COLLECTOR_BRANCH=dev" >> $GITHUB_OUTPUT | |
echo "JSON_RPC_SERVER_BRANCH=dev" >> $GITHUB_OUTPUT | |
echo "IMAGE_TAG=pr-preview-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT | |
echo "NETWORK=custom" >> $GITHUB_OUTPUT | |
else | |
echo "SHARDEUM_BRANCH=${{ inputs.shardeum_branch }}" >> $GITHUB_OUTPUT | |
echo "RELAYER_COLLECTOR_BRANCH=${{ inputs.relayer_collector_branch }}" >> $GITHUB_OUTPUT | |
echo "JSON_RPC_SERVER_BRANCH=${{ inputs.json_rpc_server_branch }}" >> $GITHUB_OUTPUT | |
# Only add network prefix for non-custom networks | |
if [ "${{ inputs.network }}" != "custom" ]; then | |
echo "IMAGE_TAG=${{ inputs.network }}-${{ inputs.image_tag }}" >> $GITHUB_OUTPUT | |
else | |
echo "IMAGE_TAG=${{ inputs.image_tag }}" >> $GITHUB_OUTPUT | |
fi | |
echo "NETWORK=${{ inputs.network }}" >> $GITHUB_OUTPUT | |
fi | |
- name: Build Docker image | |
run: | | |
docker build \ | |
--no-cache \ | |
--platform linux/${{ matrix.arch }} \ | |
--build-arg SHARDEUM_BRANCH=${{ steps.build_config.outputs.SHARDEUM_BRANCH }} \ | |
--build-arg RELAYER_COLLECTOR_BRANCH=${{ steps.build_config.outputs.RELAYER_COLLECTOR_BRANCH }} \ | |
--build-arg JSON_RPC_SERVER_BRANCH=${{ steps.build_config.outputs.JSON_RPC_SERVER_BRANCH }} \ | |
--build-arg NETWORK=${{ steps.build_config.outputs.NETWORK }} \ | |
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:${{ steps.build_config.outputs.IMAGE_TAG }} \ | |
. | |
- name: Push Docker image and cleanup | |
run: | | |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:${{ steps.build_config.outputs.IMAGE_TAG }} | |
if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ "${{ inputs.push_latest }}" == "true" ]; then | |
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:${{ steps.build_config.outputs.IMAGE_TAG }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:latest | |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:latest | |
docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:latest | |
fi | |
# Remove the image after pushing | |
docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.arch }}:${{ steps.build_config.outputs.IMAGE_TAG }} | |
create-manifest: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set image tag | |
id: build_config | |
run: | | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
echo "IMAGE_TAG=pr-preview-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT | |
else | |
# Only add network prefix for non-custom networks | |
if [ "${{ inputs.network }}" != "custom" ]; then | |
echo "IMAGE_TAG=${{ inputs.network }}-${{ inputs.image_tag }}" >> $GITHUB_OUTPUT | |
else | |
echo "IMAGE_TAG=${{ inputs.image_tag }}" >> $GITHUB_OUTPUT | |
fi | |
fi | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create and push manifest | |
run: | | |
docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.build_config.outputs.IMAGE_TAG }} \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-amd64:${{ steps.build_config.outputs.IMAGE_TAG }} \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-arm64:${{ steps.build_config.outputs.IMAGE_TAG }} | |
docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.build_config.outputs.IMAGE_TAG }} | |
if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ "${{ inputs.push_latest }}" == "true" ]; then | |
docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-amd64:latest \ | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-arm64:latest | |
docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
fi |