Skip to content

Build and publish Docker images #6

Build and publish Docker images

Build and publish Docker images #6

name: Build and publish Docker images
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "env/*/Dockerfile"
- "env/*/environment.yml"
env:
REGISTRY: ghcr.io
INCLUDE_DIR: "env/"
REPOSITORY: ${{ github.repository }}
jobs:
changed-directories:
name: Get last directory name of changed files
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.changed-files.outputs.all_changed_files }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
uses: tj-actions/changed-files@v45
id: changed-files
with:
matrix: true
dir_names: true
path: ${{ env.INCLUDE_DIR }}
dir_names_include_files: ${{ env.INCLUDE_DIR }}
dir_names_exclude_current_dir: true
build-and-push-image:
name: Build and push Docker images
runs-on: ubuntu-latest
needs: changed-directories
strategy:
fail-fast: false
matrix:
name: ${{ fromJSON(needs.changed-directories.outputs.matrix) }}
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}/${{ matrix.name }}
tags: |
type=raw,value=latest,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }}
type=ref,event=branch,enable=${{ !endsWith(github.ref, github.event.repository.default_branch) }}
flavor: |
latest=false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ${{ env.INCLUDE_DIR }}/${{ matrix.name }}
push: true
tags: ${{ steps.meta.outputs.tags }}
platforms: linux/amd64,linux/arm64