Skip to content

Build, Test & Push #821

Build, Test & Push

Build, Test & Push #821

name: Build, Test & Push
on:
push:
branches:
- '**' # all branches
schedule:
- cron: '22 2 * * 1,3,5' # every monday, wednesday, friday - at 02:00h
workflow_dispatch:
inputs:
build:
description: "Run build steps? (yes/no)"
type: boolean
required: true
default: true
jobs:
flagTestBuild:
# Generate condition: if file/s changed involve changes on the image, or trigger is cron: test & build jobs must run
name: "Conditional: run test & build jobs"
runs-on: ubuntu-latest
outputs:
flag: ${{ env.flag }}
steps:
# Setup
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 0
# Perform validation
- name: Verify if any of the image-involved files changed
id: changed-files
uses: tj-actions/changed-files@v11.5
with:
files: |
.dockerignore
Dockerfile
images.json
tools/**
scripts/**
- name: Set flag for triggering test & build jobs
if: ${{ steps.changed-files.outputs.any_changed == 'true' || github.event_name == 'schedule' || github.event.inputs.build == 'true' }}
run: echo "flag=true" >> $GITHUB_ENV
testBuild:
# Run "build" tests
name: Test (build)
runs-on: ubuntu-latest
if: ${{ needs.flagTestBuild.outputs.flag == 'true' }}
needs:
- flagTestBuild
steps:
# Setup
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Setup Python
uses: actions/setup-python@master
with:
python-version: "3.8"
architecture: "x64"
- name: Install test requirements
run: make test-install-requirements
# Test
- name: Test "build"
run: make test-build
imagesMatrix:
# Parse and set the images matrix to be used by following jobs
name: Parse & Set images matrix
runs-on: ubuntu-latest
if: ${{ needs.flagTestBuild.outputs.flag == 'true' }}
needs:
- flagTestBuild
outputs:
matrix: ${{ steps.set_matrix.outputs.matrix }}
steps:
# Setup
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Load & Set matrix variable
id: set_matrix
run: |
echo "::set-output name=matrix::$(echo $(cat images.json) | sed 's/ //g' )"
testNoBuild:
# Run "no-build" tests (pre-building image for linux/amd64)
name: "[${{ matrix.image.fromImage }}] Test (no-build)"
runs-on: ubuntu-latest
if: ${{ needs.flagTestBuild.outputs.flag == 'true' }}
needs:
- flagTestBuild
- testBuild
- imagesMatrix
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.imagesMatrix.outputs.matrix) }}
steps:
# Setup
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Setup Python
uses: actions/setup-python@master
with:
python-version: "3.8"
architecture: "x64"
- name: Install test requirements
run: make test-install-requirements
- name: Set environment variables
run: |
echo "fromImage=${{ matrix.image.fromImage }}" >> $GITHUB_ENV
echo "toImage=python-git-app:${{ matrix.image.toTag }}" >> $GITHUB_ENV
# Build
- name: Docker cache
uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
with:
key: "DockerPythonGitApp-docker-cache-${{ env.fromImage }}-{hash}"
restore-keys: "DockerPythonGitApp-docker-cache-${{ env.fromImage }}"
- name: Build
run: make build FROM_IMAGE=${{ env.fromImage }} TO_IMAGE=${{ env.toImage }}
- name: Test "no-build" (arch=default)
run: make test-nobuild FROM_IMAGE=${{ env.toImage }}
buildPush:
# Build and push to DockerHub if current branch is "master"
name: "[${{ matrix.image.fromImage }} -> ${{ matrix.image.toTag }}] Build & Push (multiarch)"
runs-on: ubuntu-latest
if: ${{ needs.flagTestBuild.outputs.flag == 'true' }}
needs:
- flagTestBuild
- testBuild
- testNoBuild
- imagesMatrix
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.imagesMatrix.outputs.matrix) }}
steps:
# Setup
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Install QEMU
uses: docker/setup-qemu-action@v1
- name: Install Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Set environment variables
run: |
echo "fromImage=${{ matrix.image.fromImage }}" >> $GITHUB_ENV
echo "toTag=${{ matrix.image.toTag }}" >> $GITHUB_ENV
echo "arch=${{ matrix.image.arch }}" >> $GITHUB_ENV
echo "buildxPush=false" >> $GITHUB_ENV
- name: Set buildx push flag
if: github.ref == 'refs/heads/master'
run: |
echo "buildxPush=true" >> $GITHUB_ENV
# Docker login
- name: Login to DockerHub
if: ${{ env.buildxPush == 'true' }}
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }}
# Build & push for all archs
- name: Docker cache
uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
with:
key: "DockerPythonGitApp-docker-cache-${{ matrix.base_tag }}-{hash}"
restore-keys: "DockerPythonGitApp-docker-cache-${{ matrix.base_tag }}"
- name: Build & Push for all archs
run: make buildx FROM_IMAGE=${{ env.fromImage }} TO_TAG=${{ env.toTag }} BUILDX_PUSH=${{ env.buildxPush }} ARCH=${{ env.arch }}