Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docker building workflow #165

Merged
merged 3 commits into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 47 additions & 37 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,34 @@ on:
workflow_dispatch:

jobs:
push-docker-image:
test-docker-image:
# Ubuntu-20.04 required until this is fixed: https://github.com/actions/runner-images/discussions/9074
runs-on: ubuntu-20.04
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@v4.1.1

- name: 'Login to GitHub Container Registry'
uses: docker/login-action@v3.0.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout GitHub Action
uses: actions/checkout@v4.1.5

- name: 'Generate downcase repository name'
run: |
echo "REPO=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}

- name: 'Set up Docker Buildx'
uses: docker/setup-buildx-action@v3.1.0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.3.0

- name: 'Cache Docker layers'
uses: actions/cache@v4.0.1
- name: Build amd64 image for testing
uses: docker/build-push-action@v5.3.0
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

- name: 'Build image for testing'
uses: docker/build-push-action@v5.1.0
with:
tags: ghcr.io/${{ env.REPO }}:test
context: .
tags: birdnet-go:test
load: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
platforms: linux/amd64
cache-from: type=gha,scope=linux/amd64
cache-to: type=gha,mode=max,scope=linux/amd64

- name: 'Install alsa dummy module for testing built image'
- name: Install alsa dummy module for testing built image
run: |
sudo apt-get install linux-modules-extra-$(uname -r)
sudo modprobe snd-dummy

- name: 'Test built image'
- name: Test built amd64 image
run: |
docker run --rm -p 8080:8080 --device /dev/snd --env ALSA_CARD=0 ghcr.io/${{ env.REPO }}:test realtime --source "Dummy" & p1=$!
docker run --rm -p 8080:8080 --device /dev/snd --env ALSA_CARD=0 birdnet-go:test realtime --source "Dummy" & p1=$!
if ! wget --retry-connrefused --waitretry=1 --tries=5 -q -O /dev/null http://localhost:8080; then
echo "Failed to reach container after 5 sec"
kill "$p1"
Expand All @@ -61,13 +44,40 @@ jobs:
kill "$p1"
fi

- name: 'Build and push images for all platforms'
uses: docker/build-push-action@v5.1.0
push-docker-image:
needs: test-docker-image
runs-on: ubuntu-20.04
strategy:
matrix:
include:
- platform: linux/arm64
- platform: linux/amd64
steps:
- name: Checkout GitHub Action
uses: actions/checkout@v4.1.5

- name: Login to GitHub Container Registry
uses: docker/login-action@v3.1.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Generate downcase repository name
run: |
echo "REPO=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.3.0

- name: Build and push docker image
uses: docker/build-push-action@v5.3.0
with:
context: .
push: true
tags: |
ghcr.io/${{ env.REPO }}:dev
ghcr.io/${{ env.REPO }}:${{ github.sha }}
platforms: linux/amd64,linux/arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
platforms: ${{ matrix.platform }}
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
Loading