Merge pull request #41 from frankframework/dependabot/docker/build/jd… #70
This file contains hidden or 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: Build and Publish Docker Images | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
workflow_dispatch: | |
jobs: | |
find-dockerfiles: | |
name: Find Dockerfiles | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v3 | |
- name: Find all Dockerfiles | |
id: find-dockerfiles | |
run: | | |
DOCKERFILES=$(find . -name "Dockerfile" -not -path "*/\.*" | sort) | |
echo "Found Dockerfiles:" | |
echo "$DOCKERFILES" | |
# Convert to JSON array with path and image name | |
JSON_ARRAY="[" | |
FIRST=true | |
for dockerfile in $DOCKERFILES; do | |
DIR=$(dirname "$dockerfile") | |
# Remove leading ./ from path | |
DIR=${DIR#./} | |
# Use directory path as image name, replacing / with - | |
IMAGE_NAME=${DIR} | |
if [ "$FIRST" = true ]; then | |
FIRST=false | |
else | |
JSON_ARRAY+="," | |
fi | |
JSON_ARRAY+="{\"dockerfile\":\"$dockerfile\",\"context\":\"$DIR\",\"image_name\":\"$IMAGE_NAME\"}" | |
done | |
JSON_ARRAY+="]" | |
echo "matrix=$JSON_ARRAY" >> $GITHUB_OUTPUT | |
- name: Set Matrix Output | |
id: set-matrix | |
run: | | |
MATRIX=$(cat << EOF | |
${{ steps.find-dockerfiles.outputs.matrix }} | |
EOF | |
) | |
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT | |
build-and-push: | |
needs: find-dockerfiles | |
name: Build and Push Docker Images | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
dockerfile: ${{ fromJson(needs.find-dockerfiles.outputs.matrix) }} | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Needed for paths-filter to work properly | |
# Check if files in specific directory have changed | |
- uses: dorny/paths-filter@v2 | |
id: changes | |
with: | |
filters: | | |
dockerfile_dir: | |
- '${{ matrix.dockerfile.context }}/**' | |
# Skip remaining steps if no changes in this directory and not manually triggered | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
if: steps.changes.outputs.dockerfile_dir == 'true' || github.event_name == 'workflow_dispatch' | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
if: (steps.changes.outputs.dockerfile_dir == 'true' || github.event_name == 'workflow_dispatch') && github.event_name != 'pull_request' | |
- name: Extract metadata for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: | | |
ghcr.io/${{ github.repository_owner }}/${{ matrix.dockerfile.image_name }} | |
tags: | | |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} | |
type=sha,prefix= | |
type=ref,event=branch | |
type=ref,event=tag | |
if: steps.changes.outputs.dockerfile_dir == 'true' || github.event_name == 'workflow_dispatch' | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: ${{ matrix.dockerfile.context }} | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
# Required for Oracle DBMS | |
build-args: | | |
BUILDKIT_SANDBOX_HOSTNAME=localhost | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
if: steps.changes.outputs.dockerfile_dir == 'true' || github.event_name == 'workflow_dispatch' |