GitHub Action to set up Docker Buildx.
If you are interested, check out my other GitHub Actions!
- Usage
- Projects using this action
- Customizing
- Keep up-to-date with GitHub Dependabot
- Limitation
- How can I help?
- License
Here is a simple example to build a Docker image with buildx (BuildKit)
name: buildx
on:
pull_request:
branches: master
push:
branches: master
tags:
jobs:
buildx:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Docker Buildx
id: buildx
uses: crazy-max/ghaction-docker-buildx@v3
with:
buildx-version: latest
qemu-version: latest
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
-
name: Run Buildx
run: |
docker buildx build \
--platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x \
--output "type=image,push=false" \
--file ./test/Dockerfile ./test
Another example to build and push Diun Docker image on DockerHub.
- On
push
event, Docker imagecrazymax/diun:edge
is built and pushed on DockerHub. - On
pull_request
event, Docker imagecrazymax/diun:edge
is built. - On
schedule
event, Docker imagecrazymax/diun:nightly
is built and pushed on DockerHub. - On
push tags
event, Docker imagecrazymax/diun:<version>
andcrazymax/diun:latest
is built and pushed on DockerHub.
name: buildx
on:
schedule:
- cron: '0 10 * * *' # everyday at 10am
pull_request:
branches: master
push:
branches: master
tags:
- v*
jobs:
buildx:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Prepare
id: prepare
run: |
DOCKER_IMAGE=crazymax/diun
DOCKER_PLATFORMS=linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le,linux/s390x
VERSION=edge
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/v}
fi
if [ "${{ github.event_name }}" = "schedule" ]; then
VERSION=nightly
fi
TAGS="--tag ${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
TAGS="$TAGS --tag ${DOCKER_IMAGE}:latest"
fi
echo ::set-output name=docker_image::${DOCKER_IMAGE}
echo ::set-output name=version::${VERSION}
echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} \
--build-arg VERSION=${VERSION} \
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
--build-arg VCS_REF=${GITHUB_SHA::8} \
${TAGS} --file ./test/Dockerfile ./test
-
name: Set up Docker Buildx
uses: crazy-max/ghaction-docker-buildx@v3
-
name: Docker Buildx (build)
run: |
docker buildx build --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }}
-
name: Login to DockerHub
if: success() && github.event_name != 'pull_request'
uses: crazy-max/ghaction-docker-login@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker Buildx (push)
if: success() && github.event_name != 'pull_request'
run: |
docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }}
-
name: Inspect image
if: always() && github.event_name != 'pull_request'
run: |
docker buildx imagetools inspect ${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.version }}
You can leverage cache using @actions/cache with this action.
name: buildx
on:
pull_request:
branches: master
push:
branches: master
jobs:
buildx:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Docker Buildx
uses: crazy-max/ghaction-docker-buildx@v3
-
name: Cache Docker layers
uses: actions/cache@v2
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Docker Buildx (build)
run: |
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache" \
--platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x \
--output "type=image,push=false" \
--tag crazymax/diun:latest \
--file ./Dockerfile-diun ./
-
name: Login to DockerHub
uses: crazy-max/ghaction-docker-login@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker Buildx (push)
run: |
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x \
--output "type=image,push=true" \
--tag crazymax/diun:latest \
--file ./Dockerfile-diun ./
-
name: Inspect image
run: |
docker buildx imagetools inspect crazymax/diun:latest
Following inputs can be used as step.with
keys
Name | Type | Default | Description |
---|---|---|---|
buildx-version |
String | latest |
Buildx version. Example: v0.3.0 |
qemu-version |
String | latest |
qemu-user-static version (Docker tag). Example: 4.2.0-7 |
Following outputs are available
Name | Type | Description |
---|---|---|
platforms |
String | Available platforms (comma separated) |
The following official docker environment variables are supported:
Name | Type | Default | Description |
---|---|---|---|
DOCKER_CONFIG |
String | ~/.docker |
The location of your client configuration files |
Since Dependabot
has native GitHub Actions support,
to enable it on your GitHub repo all you need to do is add the .github/dependabot.yml
file:
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
This action is only available for Linux virtual environments.
All kinds of contributions are welcome π! The most basic way to show your support is to star π the project, or to raise issues π¬ You can also support this project by becoming a sponsor on GitHub π or by making a Paypal donation to ensure this journey continues indefinitely! π
Thanks again for your support, it is much appreciated! π
MIT. See LICENSE
for more details.