Skip to content

fix: Enabled image push only from master and wf dispatch #9

fix: Enabled image push only from master and wf dispatch

fix: Enabled image push only from master and wf dispatch #9

name: Build and push Docker image 'target-test-base_jammy-py'
on:
push:
branches:
- master
paths:
- 'target-test-base_jammy-py/**'
- '.github/workflows/build_image_target-test-base_jammy-py.yml'
pull_request:
paths:
- 'target-test-base_jammy-py/**'
- '.github/workflows/build_image_target-test-base_jammy-py.yml'
workflow_dispatch:
inputs:
python_3_8_17:
description: 'Build Ubuntu 22.04 (Jammy) with Python 3.8.17'
required: false
type: boolean
default: true
python_3_9_17:
description: 'Build Ubuntu 22.04 (Jammy) with Python 3.9.17'
required: false
type: boolean
default: false
python_3_10_12:
description: 'Build Ubuntu 22.04 (Jammy) with Python 3.10.12'
required: false
type: boolean
default: false
python_3_11_4:
description: 'Build Ubuntu 22.04 (Jammy) with Python 3.11.4'
required: false
type: boolean
default: false
# Kill running actions pipeline (for PR) when new changes are pushed to PR
concurrency:
group: pr-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
IMAGE_DIR: ./target-test-base_jammy-py
IMAGE_BASE_NAME: ghcr.io/${{ github.repository }}/target-test-base-jammy
IMAGE_ARCHS: linux/amd64,linux/arm
jobs:
setup-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Set Matrix
id: set-matrix
run: |
matrix='["3.8.17"]'
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
if [ "${{ github.event.inputs.python_3_9_17 }}" = "true" ]; then
matrix=$(echo $matrix | jq ' . + ["3.9.17"] ')
fi
if [ "${{ github.event.inputs.python_3_10_12 }}" = "true" ]; then
matrix=$(echo $matrix | jq ' . + ["3.10.12"] ')
fi
if [ "${{ github.event.inputs.python_3_11_4 }}" = "true" ]; then
matrix=$(echo $matrix | jq ' . + ["3.11.4"] ')
fi
fi
echo $matrix
echo "::set-output name=matrix::$matrix"
shell: bash
build-and-push-docker-image:
needs: setup-matrix
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ${{fromJson(needs.setup-matrix.outputs.matrix)}}
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Extract Major/Minor Version from Full Version
id: extract_version
run: |
echo "PYTHON_VERSION=${{ matrix.python_version }}" >> $GITHUB_ENV
echo "PYTHON_VERSION_MM=$(echo "${{ matrix.python_version }}" | cut -d'.' -f1,2)" >> $GITHUB_ENV
- name: Setup QEMU for Multi-Arch Builds
uses: docker/setup-qemu-action@v1
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GitHub Container Registry (GHCR)
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Pull Docker image to use as cache (for master)
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
run: |
docker pull ${{ env.IMAGE_BASE_NAME }}:py${{ matrix.python_version }} || true
- name: Cache Docker layers (for PRs)
if: github.event_name == 'pull_request'
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: buildx-${{ matrix.python_version }}-${{ github.sha }}
restore-keys: |
buildx-${{ matrix.python_version }}-
- name: Build and Push Docker Image
uses: docker/build-push-action@v2
with:
context: ${{ env.IMAGE_DIR }}
platforms: ${{env.IMAGE_ARCHS}}
tags: ${{ env.IMAGE_BASE_NAME }}:py${{ matrix.python_version }}
build-args: |
PYTHON_VERSION=${{ env.PYTHON_VERSION }}
PYTHON_VERSION_MM=${{ env.PYTHON_VERSION_MM }}
push: ${{ github.event_name != 'pull_request' }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache