Build Multiarch Images #144
This file contains 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 Multiarch Images | |
on: | |
workflow_dispatch: | |
inputs: | |
be-tag: | |
description: "The Lemmy Backend tag to build" | |
required: false | |
type: string | |
fe-tag: | |
description: "The Lemmy Frontend tag to build" | |
required: false | |
type: string | |
env: | |
LEMMY_ORG: LemmyNet | |
BE_REPO: lemmy | |
FE_REPO: lemmy-ui | |
DST_REGISTRY: ghcr.io | |
BE_TAG: ${{ github.event.inputs.be-tag }} | |
FE_TAG: ${{ github.event.inputs.fe-tag }} | |
jobs: | |
launch-runners: | |
runs-on: ubuntu-latest | |
if: github.event.inputs.fe-tag != '' || github.event.inputs.be-tag != '' | |
strategy: | |
matrix: | |
include: | |
- runner-arch: "x64" | |
docker-target: "linux/amd64" | |
target-short: "amd64" | |
ec2-ami: "ami-05fb0b8c1424f266b" | |
ec2-instance-type: "c5d.large" | |
- runner-arch: "arm64" | |
docker-target: "linux/arm/v7" | |
target-short: "arm" | |
ec2-ami: "ami-0748d13ffbc370c2b" | |
ec2-instance-type: "c6gn.large" | |
- runner-arch: "arm64" | |
docker-target: "linux/arm64" | |
target-short: "arm64" | |
ec2-ami: "ami-0748d13ffbc370c2b" | |
ec2-instance-type: "c6gn.large" | |
steps: | |
- name: Launch Spot Runner (${{ matrix.runner-arch }}, ${{ matrix.docker-target }}) | |
id: launch | |
uses: ubergeek77/aws-ec2-spot-runner@v1 | |
with: | |
ec2-ami: ${{ matrix.ec2-ami }} | |
ec2-instance-type: ${{ matrix.ec2-instance-type }} | |
runner-arch: ${{ matrix.runner-arch }} | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
ec2-keypair-name: ${{ secrets.EC2_KEYPAIR }} | |
ec2-security-group-id: ${{ secrets.EC2_SG }} | |
github-repo: ${{ github.repository }} | |
github-token: ${{ secrets.RUNNER_TOKEN }} | |
- name: Export Runner Label | |
id: save-labels | |
run: echo "${{ matrix.target-short }}_label=${{ steps.launch.outputs.label }}" >> $GITHUB_OUTPUT | |
outputs: | |
amd64_label: ${{ steps.save-labels.outputs.amd64_label }} | |
arm_label: ${{ steps.save-labels.outputs.arm_label }} | |
arm64_label: ${{ steps.save-labels.outputs.arm64_label }} | |
build-images: | |
needs: launch-runners | |
if: github.event.inputs.fe-tag != '' || github.event.inputs.be-tag != '' | |
strategy: | |
matrix: | |
include: | |
- docker-target: "linux/amd64" | |
target-short: "amd64" | |
runs-on: ${{ needs.launch-runners.outputs.amd64_label }} | |
- docker-target: "linux/arm/v7" | |
target-short: "arm" | |
runs-on: ${{ needs.launch-runners.outputs.arm_label }} | |
- docker-target: "linux/arm64" | |
target-short: "arm64" | |
runs-on: ${{ needs.launch-runners.outputs.arm64_label }} | |
runs-on: ${{ matrix.runs-on }} | |
steps: | |
- name: Checkout Build Files | |
uses: actions/checkout@v3 | |
- name: Checkout Lemmy Frontend Source (${{ env.FE_TAG }}) | |
uses: actions/checkout@v3 | |
if: env.FE_TAG != '' | |
with: | |
repository: ${{ env.LEMMY_ORG }}/${{ env.FE_REPO }} | |
ref: ${{ env.FE_TAG }} | |
path: ${{ env.FE_REPO }} | |
submodules: recursive | |
- name: Checkout Lemmy Backend Source (${{ env.BE_TAG }}) | |
uses: actions/checkout@v3 | |
if: github.event.inputs.be-tag != '' | |
with: | |
repository: ${{ env.LEMMY_ORG }}/${{ env.BE_REPO }} | |
ref: ${{ env.BE_TAG }} | |
path: ${{ env.BE_REPO }} | |
submodules: recursive | |
- name: Override Frontend Dockerfile | |
if: env.FE_TAG != '' | |
run: cp -f ./Dockerfile-frontend ./${{ env.FE_REPO }}/Dockerfile | |
- name: Override Backend Dockerfile | |
if: github.event.inputs.be-tag != '' | |
run: cp -f ./Dockerfile-backend ./${{ env.BE_REPO }}/Dockerfile | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.DST_REGISTRY }} | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Frontend (${{ env.FE_TAG }}, ${{ matrix.docker-target }}) | |
if: env.FE_TAG != '' | |
uses: docker/build-push-action@v4 | |
with: | |
context: ./${{ env.FE_REPO }} | |
file: ./${{ env.FE_REPO }}/Dockerfile | |
platforms: ${{ matrix.docker-target }} | |
push: true | |
tags: ${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.FE_REPO }}:${{ env.FE_TAG }}-${{ matrix.target-short }} | |
- name: Build and push Backend (${{ env.BE_TAG }}, ${{ matrix.docker-target }}) | |
if: github.event.inputs.be-tag != '' | |
uses: docker/build-push-action@v4 | |
with: | |
context: ./${{ env.BE_REPO }} | |
file: ./${{ env.BE_REPO }}/Dockerfile | |
platforms: ${{ matrix.docker-target }} | |
push: true | |
tags: ${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.BE_REPO }}:${{ env.BE_TAG }}-${{ matrix.target-short }} | |
stop-runners: | |
needs: [launch-runners,build-images] | |
runs-on: ubuntu-latest | |
if: (github.event.inputs.fe-tag != '' || github.event.inputs.be-tag != '') && always() | |
strategy: | |
matrix: | |
include: | |
- shutdown-label: ${{ needs.launch-runners.outputs.amd64_label }} | |
docker-target: "linux/amd64" | |
runner-arch: "x64" | |
- shutdown-label: ${{ needs.launch-runners.outputs.arm_label }} | |
docker-target: "linux/arm/v7" | |
runner-arch: "arm64" | |
- shutdown-label: ${{ needs.launch-runners.outputs.arm64_label }} | |
docker-target: "linux/arm64" | |
runner-arch: "arm64" | |
steps: | |
- name: Terminate Runner (${{ matrix.runner-arch }}, ${{ matrix.docker-target }}) | |
uses: ubergeek77/aws-ec2-spot-runner@v1 | |
with: | |
shutdown-label: ${{ matrix.shutdown-label }} | |
github-token: ${{ secrets.RUNNER_TOKEN }} | |
github-repo: ${{ github.repository }} | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
squash-images: | |
needs: build-images | |
if: github.event.inputs.fe-tag != '' || github.event.inputs.be-tag != '' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.DST_REGISTRY }} | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Squash Frontend Multiarch Tags | |
if: env.FE_TAG != '' | |
uses: ubergeek77/squash-docker-tags@v1 | |
with: | |
source-tags: | | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.FE_REPO }}:${{ env.FE_TAG }}-amd64 | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.FE_REPO }}:${{ env.FE_TAG }}-arm | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.FE_REPO }}:${{ env.FE_TAG }}-arm64 | |
destination-tag: ${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.FE_REPO }}:${{ env.FE_TAG }} | |
- name: Squash Backend Multiarch Tags | |
if: github.event.inputs.be-tag != '' | |
uses: ubergeek77/squash-docker-tags@v1 | |
with: | |
source-tags: | | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.BE_REPO }}:${{ env.BE_TAG }}-amd64 | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.BE_REPO }}:${{ env.BE_TAG }}-arm | |
${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.BE_REPO }}:${{ env.BE_TAG }}-arm64 | |
destination-tag: ${{ env.DST_REGISTRY }}/${{ github.repository_owner }}/${{ env.BE_REPO }}:${{ env.BE_TAG }} |