name: Build containers

on:
  push:
    branches:
    - openssl
    - docker
    paths:
    - .github/workflows/docker.yml
    - contrib/docker/**
    - contrib/certificates/**
    - daemon/**
    - i18n/**
    - libi2pd/**
    - libi2pd_client/**
    - Makefile
    - Makefile.linux
    tags:
    - '*'

jobs:
  build:
    name: Building container for ${{ matrix.platform }}
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read

    strategy:
      matrix:
        include: [
          { platform: 'linux/amd64', archname: 'amd64' },
          { platform: 'linux/386', archname: 'i386' },
          { platform: 'linux/arm64', archname: 'arm64' },
          { platform: 'linux/arm/v7', archname: 'armv7' },
        ]

    steps:
    - name: Checkout
      uses: actions/checkout@v4

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

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

    - name: Login to DockerHub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}

    - name: Login to GitHub Container registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Build container for ${{ matrix.archname }}
      uses: docker/build-push-action@v5
      with:
        context: ./contrib/docker
        file: ./contrib/docker/Dockerfile
        platforms: ${{ matrix.platform }}
        push: true
        tags: |
          purplei2p/i2pd:latest-${{ matrix.archname }}
          ghcr.io/purplei2p/i2pd:latest-${{ matrix.archname }}
        provenance: false

  push:
    name: Pushing merged manifest
    runs-on: ubuntu-latest

    permissions:
      packages: write
      contents: read

    needs: build

    steps:
    - name: Checkout
      uses: actions/checkout@v4

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

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

    - name: Login to DockerHub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}

    - name: Login to GitHub Container registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Create and push latest manifest image to Docker Hub
      if: ${{ !startsWith(github.ref, 'refs/tags/') }}
      uses: Noelware/docker-manifest-action@v1
      with:
        tags: purplei2p/i2pd:latest
        inputs: purplei2p/i2pd:latest-amd64,purplei2p/i2pd:latest-i386,purplei2p/i2pd:latest-arm64,purplei2p/i2pd:latest-armv7
        push: true

    - name: Create and push latest manifest image to GHCR
      if: ${{ !startsWith(github.ref, 'refs/tags/') }}
      uses: Noelware/docker-manifest-action@v1
      with:
        tags: ghcr.io/purplei2p/i2pd:latest
        inputs: ghcr.io/purplei2p/i2pd:latest-amd64,ghcr.io/purplei2p/i2pd:latest-i386,ghcr.io/purplei2p/i2pd:latest-arm64,ghcr.io/purplei2p/i2pd:latest-armv7
        push: true

    - name: Store release version to env
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV

    - name: Create and push release manifest to Docker Hub
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      uses: Noelware/docker-manifest-action@v1
      with:
        tags: purplei2p/i2pd:latest,purplei2p/i2pd:latest-release,purplei2p/i2pd:release-${{ env.RELEASE_VERSION }}
        inputs: purplei2p/i2pd:latest-amd64,purplei2p/i2pd:latest-i386,purplei2p/i2pd:latest-arm64,purplei2p/i2pd:latest-armv7
        push: true

    - name: Create and push release manifest to GHCR
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      uses: Noelware/docker-manifest-action@v1
      with:
        tags: ghcr.io/purplei2p/i2pd:latest,ghcr.io/purplei2p/i2pd:latest-release,ghcr.io/purplei2p/i2pd:release-${{ env.RELEASE_VERSION }}
        inputs: ghcr.io/purplei2p/i2pd:latest-amd64,ghcr.io/purplei2p/i2pd:latest-i386,ghcr.io/purplei2p/i2pd:latest-arm64,ghcr.io/purplei2p/i2pd:latest-armv7
        push: true
