Merge branch 'main' of https://github.com/dhslab/dhslab-docker-images #240
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 Push Changed Containers | |
| on: | |
| push: | |
| branches: | |
| - main # Or your default branch | |
| jobs: | |
| # This job discovers which service directories have changed. | |
| # This job discovers which service directories have changed. | |
| determine-build-matrix: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.create-matrix.outputs.matrix }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get changed Dockerfiles | |
| id: changed-files | |
| uses: tj-actions/changed-files@v44 | |
| with: | |
| # Only report changes to files named Dockerfile anywhere in the repo | |
| files: '**/Dockerfile' | |
| - name: Create build matrix for changed services | |
| id: create-matrix | |
| run: | | |
| # Get the list of changed Dockerfiles into a variable | |
| changed_dockerfiles='${{ steps.changed-files.outputs.all_changed_files }}' | |
| # Check if the list of changed files is empty | |
| if [[ -z "$changed_dockerfiles" ]]; then | |
| # If it's empty, create an empty matrix and finish | |
| matrix='{"include":[]}' | |
| else | |
| # If files were changed, get their directory paths and create the matrix | |
| paths=$(echo "$changed_dockerfiles" | xargs -n1 dirname | sort -u) | |
| matrix=$(echo "$paths" | jq -R '{"path": .}' | jq -cs '{"include": .}') | |
| fi | |
| echo "matrix=${matrix}" >> $GITHUB_OUTPUT | |
| # This job builds and pushes images based on the matrix from the previous job. | |
| build-and-push: | |
| needs: determine-build-matrix | |
| # Only run this job if the matrix is not empty | |
| if: ${{ needs.determine-build-matrix.outputs.matrix != '{"include":[]}' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write # Required to push to GitHub Container Registry (ghcr.io) | |
| strategy: | |
| fail-fast: false | |
| # Use the JSON output from the 'determine-build-matrix' job | |
| matrix: ${{ fromJSON(needs.determine-build-matrix.outputs.matrix) }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata (tags, labels) for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| # Creates an image name based on the directory path | |
| images: ghcr.io/${{ github.repository_owner }}/${{ matrix.path }} | |
| tags: | | |
| type=raw,value=latest,enable=true | |
| type=raw,value={{date 'YYMMDD'}},enable=true | |
| - name: 🚀 Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| # The context is the specific service directory from the matrix | |
| context: ./${{ matrix.path }} | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |