name: Container

on:
  schedule:
    - cron: '22 2 */6 * *' # every 6 days to avoid gha cache being evicted
  push:
    paths-ignore:
    - '.devcontainer/**'
    - 'examples/**'
    - 'lib/**'
    - 'man/**'
    - 'priv/**'
    - '**.md'

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  container:
    name: Container
    runs-on: ubuntu-24.04
    permissions:
      packages: write
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Cache build directory
        uses: actions/cache@v4
        with:
          path: ~/build/
          key: ${{runner.os}}-ctr-ct-ng-1.26.0

      - name: Get erlang/OTP version for bootstrapping
        run: |
          echo "OTP_VSN=$(awk '/^otp_vsn=/ {{gsub(/[^0-9.rc-]/, ""); print}}' tools/make-binaries)" >> $GITHUB_ENV
          echo "ELIXIR_VSN=$(awk '/^elixir_vsn=/ {{gsub(/[^0-9.]/, ""); print}}' tools/make-binaries)" >> $GITHUB_ENV

      - name: Install prerequisites
        run: |
          sudo apt-get -qq update
          sudo apt-get -qq install makeself
          # https://github.com/crosstool-ng/crosstool-ng/blob/master/testing/docker/ubuntu21.10/Dockerfile
          sudo apt-get -qq install build-essential autoconf bison flex gawk
          sudo apt-get -qq install help2man libncurses5-dev libtool libtool-bin
          sudo apt-get -qq install python3-dev texinfo unzip

      - name: Install erlang/OTP
        uses: erlef/setup-beam@v1
        with:
          otp-version: ${{ env.OTP_VSN }}
          elixir-version: ${{ env.ELIXIR_VSN }}
          version-type: strict

      - name: Remove Elixir Matchers
        run: |
          echo "::remove-matcher owner=elixir-mixCompileWarning::"
          echo "::remove-matcher owner=elixir-credoOutputDefault::"
          echo "::remove-matcher owner=elixir-mixCompileError::"
          echo "::remove-matcher owner=elixir-mixTestFailure::"
          echo "::remove-matcher owner=elixir-dialyzerOutputDefault::"

      - name: Build musl-libc based binary archives
        run: |
          sed -i "s|targets='.*'|targets='x86_64-linux-musl aarch64-linux-musl'|" tools/make-binaries
          mv .github/container/ejabberdctl.template .
          CHECK_DEPS=false tools/make-binaries

      - name: Collect packages
        run: |
          mkdir tarballs
          mv ejabberd-*.tar.gz tarballs

      - name: Checkout ejabberd-contrib
        uses: actions/checkout@v4
        with:
          repository: processone/ejabberd-contrib
          path: .ejabberd-modules/sources/ejabberd-contrib

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Get git describe
        id: gitdescribe
        run: echo "ver=$(git describe --tags)" >> $GITHUB_OUTPUT

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          labels: |
            org.opencontainers.image.revision=${{ steps.gitdescribe.outputs.ver }}
            org.opencontainers.image.licenses=GPL-2.0
            org.opencontainers.image.vendor=ProcessOne

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

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

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          build-args: |
            METHOD=package
            VERSION=${{ steps.gitdescribe.outputs.ver }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          context: .
          file: .github/container/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
