Test, Build and Deploy Images #174
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: Test, Build and Deploy Images | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
on: | |
workflow_dispatch: | |
pull_request: | |
push: | |
schedule: | |
# Run every 6 days to keep our caches alive | |
- cron: '0 0 */6 * *' | |
jobs: | |
deploy-docker-images: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
docker: [base] | |
project: [companion] | |
new_project: [blueos] | |
platforms: ["linux/arm/v7,linux/arm/v8,linux/amd64"] | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Prepare | |
id: prepare | |
run: | | |
# Deploy image with the name of the branch, if the build is a git tag, replace tag with the tag name. | |
# If git tag matches semver, append latest tag to the push. | |
DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${{ matrix.project }}-${{ matrix.docker }} | |
VERSION=${GITHUB_REF##*/} | |
if [[ $GITHUB_REF == refs/tags/* ]]; then | |
VERSION=${GITHUB_REF#refs/tags/} | |
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 | |
# Add temporary tag for the new project name | |
TAGS="$TAGS --tag ${DOCKER_USERNAME:-bluerobotics}/${{ matrix.new_project }}-${{ matrix.docker }}:${VERSION}" | |
echo ::set-output name=docker_image::${DOCKER_IMAGE} | |
echo ::set-output name=version::${VERSION} | |
echo ::set-output name=buildx_args:: \ | |
--build-arg VUE_APP_GIT_DESCRIBE=$(git describe --long --always --dirty --all) \ | |
--cache-from "type=local,src=/tmp/.buildx-cache" \ | |
--cache-to "type=local,dest=/tmp/.buildx-cache" \ | |
${TAGS} \ | |
--file Dockerfile . | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v1 | |
with: | |
platforms: all | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
with: | |
version: latest | |
- name: Cache Docker layers | |
uses: actions/cache@v2 | |
id: cache | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ matrix.docker }}-${{ hashFiles('Dockerfile') }} | |
restore-keys: | | |
${{ runner.os }}-buildx-${{ matrix.docker }}-${{ hashFiles('Dockerfile') }} | |
${{ runner.os }}-buildx-${{ matrix.docker }} | |
- name: Docker Buildx (build) | |
run: | | |
# Pull latest version of image to help with build speed | |
IFS=',' read -ra platforms <<< "${{ matrix.platforms }}" | |
for platform in "${platforms[@]}"; do | |
docker pull --platform ${platform} ${DOCKER_USERNAME:-bluerobotics}/${{ matrix.project }}-${{ matrix.docker }}:master || true | |
done | |
docker buildx build \ | |
--output "type=image,push=false" \ | |
--platform ${{ matrix.platforms }} \ | |
${{ steps.prepare.outputs.buildx_args }} | |
- name: Check size | |
run: | | |
# Check if the image size is lower than our limit | |
docker image list | |
IMAGE_ID=$(docker images -q ${DOCKER_USERNAME:-bluerobotics}/${{ matrix.project }} | head -n 1) | |
LIMIT_SIZE_MB=600 | |
IMAGE_SIZE_MB=$(( $(docker inspect $IMAGE_ID --format {{.Size}})/(2**20) )) | |
echo "Core size is: $IMAGE_SIZE_MB MB" | |
((IMAGE_SIZE_MB < LIMIT_SIZE_MB)) | |
- 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" \ | |
--platform ${{ matrix.platforms }} \ | |
${{ 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 }} | |
- name: Create image artifact | |
# We are serializing each export here because "Currently, multi-platform images cannot be | |
# exported with the docker export type." (https://docs.docker.com/engine/reference/commandline/buildx_build/#output) | |
run: | | |
DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${{ matrix.project }}-${{ matrix.docker }} | |
GIT_HASH_SHORT=$(git rev-parse --short "$GITHUB_SHA") | |
IFS=',' read -ra platforms <<< "${{ matrix.platforms }}" | |
for platform in "${platforms[@]}"; do | |
docker buildx build \ | |
--platform $platform \ | |
${{ steps.prepare.outputs.buildx_args }} \ | |
--tag ${DOCKER_IMAGE}:${GIT_HASH_SHORT} \ | |
--output "type=docker,dest=BlueOS-base-${GIT_HASH_SHORT}.tar" | |
done | |
- name: Upload artifact | |
uses: actions/upload-artifact@v2 | |
with: | |
name: BlueOS-base-docker-image.zip | |
path: '*.tar' |