Refactor Dockerfile and GitHub Actions for multi-architecture support #74
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 flAPI | |
on: | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: [ main ] | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
cancel-in-progress: true | |
jobs: | |
# ------------------------------------------------------------------------------------------------- | |
# Linux AMD64 | |
# ------------------------------------------------------------------------------------------------- | |
linux-amd64: | |
runs-on: ubuntu-latest | |
env: | |
BUILD_ARCH: amd64 | |
BUILD_IMAGE: flapi_build_amd64 | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
submodules: recursive | |
fetch-depth: 0 | |
- name: Build Docker image | |
shell: bash | |
run: | | |
docker build \ | |
-t ${BUILD_IMAGE} \ | |
-f /`pwd`/.github/docker/linux_${BUILD_ARCH}/Dockerfile . | |
- name: Run build in docker | |
shell: bash | |
run: | | |
docker run \ | |
-v `pwd`:/build_dir \ | |
${BUILD_IMAGE} \ | |
bash -c 'make clean && make release' | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: flapi-linux-${{ env.BUILD_ARCH }} | |
path: build/release/flapi | |
if-no-files-found: error | |
# ------------------------------------------------------------------------------------------------- | |
# Linux ARM64 | |
# ------------------------------------------------------------------------------------------------- | |
linux-aarch64: | |
runs-on: ubuntu-latest | |
env: | |
FLAPI_CROSS_COMPILE: arm64 | |
BUILD_ARCH: arm64 | |
BUILD_IMAGE: flapi_build_arm64 | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
submodules: recursive | |
fetch-depth: 0 | |
- name: Build Docker image | |
shell: bash | |
run: | | |
docker build \ | |
-t ${BUILD_IMAGE} \ | |
-f /`pwd`/.github/docker/linux_${BUILD_ARCH}/Dockerfile . | |
- name: Run build in docker | |
shell: bash | |
run: | | |
docker run \ | |
-e FLAPI_CROSS_COMPILE="${BUILD_ARCH}" \ | |
-v `pwd`:/build_dir \ | |
${BUILD_IMAGE} \ | |
bash -c 'make clean && make release' | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: flapi-linux-${{ env.BUILD_ARCH }} | |
path: build/release/flapi | |
if-no-files-found: error | |
# ------------------------------------------------------------------------------------------------- | |
# macOS Universal | |
# ------------------------------------------------------------------------------------------------- | |
osx-universal: | |
runs-on: macos-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
submodules: recursive | |
- name: Setup Ccache | |
uses: hendrikmuhs/ccache-action@main | |
with: | |
key: ${{ github.job }} | |
save: ${{ github.ref == 'refs/heads/main' || github.repository != 'datazoode/flapi' }} | |
- name: Install ninja | |
shell: bash | |
run: brew install ninja autoconf make libtool pkg-config automake autoconf-archive | |
- name: Build | |
run: make release | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: flapi-macos-universal | |
path: build/universal/flapi | |
if-no-files-found: error | |
retention-days: 90 | |
# ------------------------------------------------------------------------------------------------- | |
# Docker | |
# ------------------------------------------------------------------------------------------------- | |
docker: | |
needs: [linux-amd64, linux-aarch64] | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' | |
strategy: | |
matrix: | |
platform: [linux/amd64, linux/arm64] | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Download artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
path: artifacts | |
- name: Set repository owner lowercase | |
run: | | |
echo "REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
- name: Build and push multi-platform Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: Dockerfile | |
platforms: ${{ matrix.platform }} | |
push: true | |
tags: | | |
ghcr.io/${{ env.REPO_OWNER }}/flapi:latest | |
ghcr.io/${{ env.REPO_OWNER }}/flapi:${{ github.sha }} | |
provenance: false | |
build-contexts: | | |
build=./artifacts/flapi-linux-${{ endsWith(matrix.platform, 'amd64') && 'amd64' || 'arm64' }} |