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

Feat(github-actions): add multi-platform build (raspberry pi support) #245

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.git
tests
# Use whitelisting instead of blacklisting
*
! files
135 changes: 107 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,81 @@ jobs:
uses: ludeeus/action-shellcheck@master
with:
ignore: tests/shunit2

# get the date for the docker labels
- name: Prepare environment variables
id: rfc_date
run: |
echo "rfc_date=$(date --rfc-3339=seconds)" >> $GITHUB_ENV
echo "gh_server_url=$GITHUB_SERVER_URL" >> $GITHUB_ENV # because I can't seem to get it with ${{ env.GITHUB_SERVER_URL }}

# QEMU is needeed for multi-arch build
- name: Set up QEMU
uses: docker/setup-qemu-action@v1

# Buildx is the tool used for multi-arch build
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

# note: you should use ghcr.io instead of docker.pkg.github.com because the latest is in depreciation
# however i'm not able to get it to work reliably and it's still in beta
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: docker.pkg.github.com
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Build docker image single platform for the tests
- name: Build debian image
run: |
docker build . \
--pull=true \
--file=Dockerfile \
--tag="$IMAGE_NAME:latest" \
--tag="$IMAGE_NAME:debian" \
--label="org.opencontainers.image.source=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
--label="org.opencontainers.image.revision=$GITHUB_SHA" \
--label="org.opencontainers.image.created=$(date --rfc-3339=seconds)"
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be omitted, so it will use underlying runner architecture

push: false # don't push to registry
load: true # load the final result in the docker machine. note this does'nt (yet) work with multiple platforms defined.
cache-from: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:debian
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:debian
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:debian

labels: |
org.opencontainers.image.source=${{ env.gh_server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ env.rfc_date }}

- name: Test debian image
run: tests/run $IMAGE_NAME:debian

# Build docker image single platform for the tests
- name: Build alpine image
run: |
docker build . \
--pull=true \
--file=Dockerfile-alpine \
--tag="$IMAGE_NAME:alpine" \
--label="org.opencontainers.image.source=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
--label="org.opencontainers.image.revision=$GITHUB_SHA" \
--label="org.opencontainers.image.created=$(date --rfc-3339=seconds)"
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile-alpine
platforms: linux/amd64
push: false # don't push to registry
load: true # load the final result in the docker machine. note this does'nt (yet) work with multiple platforms defined.
cache-from: |
${{ env.IMAGE_NAME }}:alpine
tags: |
${{ env.IMAGE_NAME }}:alpine
ghcr.io/${{ github.repository }}:alpine
labels: |
org.opencontainers.image.source=${{ env.gh_server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ env.rfc_date }}

- name: Test alpine image
run: tests/run $IMAGE_NAME:alpine
Expand All @@ -61,26 +112,54 @@ jobs:
with:
import-github-users: atmoz

- name: Push images to Docker Hub registry
# Build & push docker image multi-platform
# note: the linux/amd64 image is not rebuilt, the cache is still present from the previous steps.
- name: Build & Push debian image to dockerhub
if: github.ref == 'refs/heads/master'
run: |
echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login \
-u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be specified as a newline separated list, which is a bit more readable.

platforms: |
  linux/386
  linux/amd64
  linux/arm/v6
  linux/arm/v7
  linux/arm64
  linux/ppc64le
  linux/s390x

push: true # push to registry
cache-from: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:debian
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:debian

labels: |
org.opencontainers.image.source=${{ env.gh_server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ env.rfc_date }}

docker push $IMAGE_NAME # no tags specified to include all tags
docker logout
# Build & push docker image multi-platform
- name: Build & Push alpine image to dockerhub
if: github.ref == 'refs/heads/master'
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile-alpine
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as previous

push: true # push to registry
cache-from: |
${{ env.IMAGE_NAME }}:alpine
tags: |
${{ env.IMAGE_NAME }}:alpine

labels: |
org.opencontainers.image.source=${{ env.gh_server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ env.rfc_date }}

# Push to github registry
- name: Push images to GitHub registry
if: github.ref == 'refs/heads/master'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com \
-u ${{ github.actor }} --password-stdin

TAG_DEBIAN=docker.pkg.github.com/$GITHUB_REPOSITORY/debian
TAG_ALPINE=docker.pkg.github.com/$GITHUB_REPOSITORY/alpine
docker tag $IMAGE_NAME:debian $TAG_DEBIAN
docker tag $IMAGE_NAME:alpine $TAG_ALPINE
docker push $TAG_DEBIAN
docker push $TAG_ALPINE
docker logout docker.pkg.github.com

3 changes: 1 addition & 2 deletions Dockerfile-alpine
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ MAINTAINER Adrian Dvergsdal [atmoz.net]
# - Fix default group (1000 does not exist)
# - OpenSSH needs /var/run/sshd to run
# - Remove generic host keys, entrypoint generates unique keys
RUN echo "@community http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
apk add --no-cache bash shadow@community openssh openssh-sftp-server && \
RUN apk add --no-cache bash shadow openssh openssh-sftp-server && \
sed -i 's/GROUP=1000/GROUP=100/' /etc/default/useradd && \
mkdir -p /var/run/sshd && \
rm -f /etc/ssh/ssh_host_*key*
Expand Down