name: Containers

on:
  # This Workflow can be triggered manually
  workflow_dispatch:
  # Build new Spack develop containers nightly.
  schedule:
    - cron: '34 0 * * *'
  # Run on pull requests that modify this file
  pull_request:
    branches:
      - develop
    paths:
      - '.github/workflows/build-containers.yml'
      - 'share/spack/docker/*'
      - 'share/spack/templates/container/*'
      - 'lib/spack/spack/container/*'
  # Let's also build & tag Spack containers on releases.
  release:
    types: [published]

concurrency:
  group: build_containers-${{github.ref}}-${{github.event.pull_request.number || github.run_number}}
  cancel-in-progress: true

jobs:
  deploy-images:
    runs-on: ubuntu-latest
    permissions:
      packages: write
    strategy:
      # Even if one container fails to build we still want the others
      # to continue their builds.
      fail-fast: false
      # A matrix of Dockerfile paths, associated tags, and which architectures
      # they support.
      matrix:
        # Meaning of the various items in the matrix list
        # 0: Container name (e.g. ubuntu-bionic)
        # 1: Platforms to build for
        # 2: Base image (e.g. ubuntu:22.04)
        dockerfile: [[amazon-linux, 'linux/amd64,linux/arm64', 'amazonlinux:2'],
                     [centos-stream9, 'linux/amd64,linux/arm64', 'centos:stream9'],
                     [leap15, 'linux/amd64,linux/arm64', 'opensuse/leap:15'],
                     [ubuntu-focal, 'linux/amd64,linux/arm64', 'ubuntu:20.04'],
                     [ubuntu-jammy, 'linux/amd64,linux/arm64', 'ubuntu:22.04'],
                     [ubuntu-noble, 'linux/amd64,linux/arm64', 'ubuntu:24.04'],
                     [almalinux8, 'linux/amd64,linux/arm64', 'almalinux:8'],
                     [almalinux9, 'linux/amd64,linux/arm64', 'almalinux:9'],
                     [rockylinux8, 'linux/amd64,linux/arm64', 'rockylinux:8'],
                     [rockylinux9, 'linux/amd64,linux/arm64', 'rockylinux:9'],
                     [fedora39, 'linux/amd64,linux/arm64', 'fedora:39'],
                     [fedora40, 'linux/amd64,linux/arm64', 'fedora:40']]
    name: Build ${{ matrix.dockerfile[0] }}
    if: github.repository == 'spack/spack'
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

      - name: Determine latest release tag
        id: latest
        run: |
          git fetch --quiet --tags
          echo "tag=$(git tag --list --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)" | tee -a $GITHUB_OUTPUT

      - uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96
        id: docker_meta
        with:
          images: |
              ghcr.io/${{ github.repository_owner }}/${{ matrix.dockerfile[0] }}
              ${{ github.repository_owner }}/${{ matrix.dockerfile[0] }}
          tags: |
              type=schedule,pattern=nightly
              type=schedule,pattern=develop
              type=semver,pattern={{version}}
              type=semver,pattern={{major}}.{{minor}}
              type=semver,pattern={{major}}
              type=ref,event=branch
              type=ref,event=pr
              type=raw,value=latest,enable=${{ github.ref == format('refs/tags/{0}', steps.latest.outputs.tag) }}

      - name: Generate the Dockerfile
        env:
          SPACK_YAML_OS: "${{ matrix.dockerfile[2] }}"
        run: |
          .github/workflows/bin/generate_spack_yaml_containerize.sh
          . share/spack/setup-env.sh
          mkdir -p dockerfiles/${{ matrix.dockerfile[0] }}
          spack containerize --last-stage=bootstrap | tee dockerfiles/${{ matrix.dockerfile[0] }}/Dockerfile
          printf "Preparing to build ${{ env.container }} from dockerfiles/${{ matrix.dockerfile[0] }}/Dockerfile"
          if [ ! -f "dockerfiles/${{ matrix.dockerfile[0] }}/Dockerfile" ]; then
              printf "dockerfiles/${{ matrix.dockerfile[0] }}/Dockerfile does not exist"
              exit 1;
          fi

      - name: Upload Dockerfile
        uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b
        with:
          name: dockerfiles_${{ matrix.dockerfile[0] }}
          path: dockerfiles

      - name: Set up QEMU
        uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Log in to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build & Deploy ${{ matrix.dockerfile[0] }}
        uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355
        with:
          context: dockerfiles/${{ matrix.dockerfile[0] }}
          platforms: ${{ matrix.dockerfile[1] }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}

  merge-dockerfiles:
    runs-on: ubuntu-latest
    needs: deploy-images
    steps:
      - name: Merge Artifacts
        uses: actions/upload-artifact/merge@6f51ac03b9356f520e9adb1b1b7802705f340c2b
        with:
          name: dockerfiles
          pattern: dockerfiles_*
          delete-merged: true
