R diff of squares #519
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
--- | |
# | |
# https://docs.docker.com/build/ci/github-actions/ | |
# https://github.com/docker/build-push-action | |
# https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action | |
# | |
name: Docker Build Workflow | |
on: # yamllint disable-line rule:truthy | |
schedule: | |
# ┌───────────── minute (0 - 59) | |
# │ ┌───────────── hour (0 - 23) | |
# │ │ ┌───────────── day of the month (1 - 31) | |
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) | |
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) | |
# │ │ │ │ │ | |
# │ │ │ │ │ | |
# │ │ │ │ │ | |
# * * * * * | |
- cron: '15 8 1 * *' | |
workflow_dispatch: | |
pull_request: | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
stage1: | |
name: Change Check | |
runs-on: 'ubuntu-latest' | |
outputs: | |
docs_changed: ${{ steps.check_file_changed.outputs.docs_changed }} | |
matrix_exercise: ${{ steps.check_file_changed.outputs.matrix_exercise }} | |
env: | |
re_change: '^(Dockerfile.*|.github/docker/.*|.github/workflows/docker-build.yml)$' | |
steps: | |
- name: Check GitHub Vars | |
id: github-vars | |
run: set | grep -e ^CI -e ^GITHUB_ -e ^RUNNER_ | |
- name: Checkout Repo | |
id: checkout-repo | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
ref: ${{ github.ref }} | |
submodules: recursive | |
- name: Get Change List | |
id: check_file_changed | |
run: |- | |
{ | |
printf "Workflow: %s\n\n" "${GITHUB_WORKFLOW}" | |
printf "Runner: name[%s] arch[%s]\n" "${RUNNER_NAME}" "${RUNNER_ARCH}" | |
printf "Repo: %s\n" "${GITHUB_REPOSITORY}" | |
printf "User: %s\n" "${GITHUB_TRIGGERING_ACTOR}" | |
printf "\n" | |
} | tee -a "${GITHUB_STEP_SUMMARY}" | |
# Diff HEAD with the previous commit then output to stdout. | |
declare -a GIT_DIFF | |
mapfile -t GIT_DIFF < <(git diff --name-only HEAD^ HEAD | tee /tmp/changed_files.txt) | |
{ | |
printf "=== Which files changed? ===\n" | |
printf "\n" | |
printf "\`\`\`text\n" | |
printf "%s\n" "${GIT_DIFF[@]}" | |
printf "\`\`\`\n" | |
printf "\n" | |
} | tee -a "${GITHUB_STEP_SUMMARY}" | |
# Get changed exercise list. | |
if [[ ${GITHUB_EVENT_NAME} != pull_request ]]; then | |
printf "Manual run, pretending we have a diff.\n" | |
HAS_DIFF=true | |
fi | |
printf "\n" | |
# Check if the files are present in the changed file list (added, modified, deleted) then output to stdout. | |
HAS_DIFF="${HAS_DIFF:-false}" | |
printf "=== Which Docker files changed? ===\n" | |
if printf "%s\n" "${GIT_DIFF[@]}" | grep -E "${{ env.re_change }}"; then | |
HAS_DIFF=true | |
fi | |
printf "\n" | |
# Did Docker files change? | |
{ | |
printf "=== Did Docker files change? ===\n" | |
printf "Changes Detected: %s\n" "${HAS_DIFF}" | |
printf "\n" | |
printf "\`\`\`text\n" | |
printf "%s\n" "${GIT_DIFF[@]}" | grep -E "${{ env.re_change }}" || true | |
printf "\`\`\`\n" | |
printf "\n" | |
} | tee -a "${GITHUB_STEP_SUMMARY}" | |
# Set the output named "docs_changed" | |
printf "%s=%s\n" "docs_changed" "${HAS_DIFF}" >> "${GITHUB_OUTPUT}" | |
stage2a: | |
name: Docker Build Base Image | |
strategy: | |
matrix: | |
os: ["ubuntu-latest"] | |
image: | |
- ci-base-debian | |
runs-on: "${{ matrix.os }}" | |
needs: [stage1] | |
if: needs.stage1.outputs.docs_changed == 'True' | |
outputs: | |
tag_date: ${{ steps.setup_image_info.outputs.tag_date }} | |
steps: | |
- name: Setup Env [${{ matrix.os }} - ${{ matrix.image }}] | |
id: setup_image_info | |
run: |- | |
tag_date="$(date +%Y%m%d)" | |
printf "%s=%s\n" "tag_date" "${tag_date}" >> "${GITHUB_OUTPUT}" | |
- name: Docker Build Checkout [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: actions/checkout@v3 | |
- name: Login to Docker Hub [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker Buildx [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/setup-buildx-action@v2 | |
- name: Build and push [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}:latest,${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}:${{ steps.setup_image_info.outputs.tag_date }} | |
target: ${{ matrix.image }} | |
cache-to: type=gha | |
stage2b: | |
name: Docker Build CI Images | |
strategy: | |
matrix: | |
os: ["ubuntu-latest"] | |
image: | |
- ci-generic-debian | |
- ci-anaconda-debian | |
runs-on: "${{ matrix.os }}" | |
needs: [stage1, stage2a] | |
if: needs.stage1.outputs.docs_changed == 'True' | |
outputs: | |
tag_date: ${{ steps.setup_image_info.outputs.tag_date }} | |
steps: | |
- name: Setup Env [${{ matrix.os }} - ${{ matrix.image }}] | |
id: setup_image_info | |
run: |- | |
tag_date="$(date +%Y%m%d)" | |
printf "%s=%s\n" "tag_date" "${tag_date}" >> "${GITHUB_OUTPUT}" | |
- name: Docker Build Checkout [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: actions/checkout@v3 | |
- name: Login to Docker Hub [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker Buildx [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/setup-buildx-action@v2 | |
- name: Build and push [${{ matrix.os }} - ${{ matrix.image }}] | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}:latest,${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}:${{ steps.setup_image_info.outputs.tag_date }} | |
target: ${{ matrix.image }} | |
cache-from: type=gha | |
stage3: | |
name: Docker Build Check | |
env: | |
ORG: ${{ secrets.DOCKERHUB_USERNAME }} | |
strategy: | |
matrix: | |
os: | |
- vpayno/ci-generic-debian:latest | |
- vpayno/ci-generic-debian:${{ needs.stage2b.outputs.tag_date }} | |
- vpayno/ci-anaconda-debian:latest | |
- vpayno/ci-anaconda-debian:${{ needs.stage2b.outputs.tag_date }} | |
runs-on: ubuntu-latest | |
container: ${{ matrix.os }} | |
needs: [stage2b] | |
steps: | |
- name: Test New Container [${{ matrix.os }}] | |
run: |- | |
printf "%s\n" "Hello World!" | |
- name: Docker Build Summary [${{ matrix.os }}] | |
run: |- | |
IMAGE_NAME="${{ matrix.os }}" | |
IMAGE_NAME="${IMAGE_NAME##*/}" | |
IMAGE_NAME="${IMAGE_NAME%%:*}" | |
ORG_IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/${IMAGE_NAME}" | |
TOKEN="$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${ORG_IMAGE}:pull" | jq -r .access_token)" | |
{ | |
printf "Docker Hub Image Info\n" | |
curl -sS --header "Authorization: Bearer ${TOKEN}" "https://index.docker.io/v2/${ORG_IMAGE}/tags/list" | jq . | |
} | tee -a "${GITHUB_STEP_SUMMARY}" |