name: Build container and publish to docker hub

on:
  create:
    tags: ["v*"]

env:
  GO_VERSION: '1.22'

jobs:
  generate-souin-traefik-docker:
    name: Generate embedded Souin as traefik middleware
    runs-on: ubuntu-latest
    steps:
      -
        name: Get tag name
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      -
        name: Checkout code
        uses: actions/checkout@v4
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build & push Docker image containing only binary
        id: docker_build
        uses: docker/build-push-action@v3
        with:
          push: true
          file: ./plugins/traefik/Dockerfile.deploy
          platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
          tags: darkweak/souin-traefik:${{ env.RELEASE_VERSION }}
  generate-souin-docker:
    name: Generate souin docker
    runs-on: ubuntu-latest
    steps:
      -
        name: Get tag name
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      -
        name: Checkout code
        uses: actions/checkout@v4
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build & push Docker image containing only binary
        id: docker_build
        uses: docker/build-push-action@v4
        with:
          push: true
          file: ./Dockerfile-prod
          platforms: linux/arm64,linux/amd64,linux/ppc64le,linux/s390x,linux/arm/v7,linux/arm/v6
          build-args: |
            "GO_VERSION=${{ env.GO_VERSION }}"
          tags: |
            darkweak/souin:latest
            darkweak/souin:${{ env.RELEASE_VERSION }}
  generate-artifacts:
    name: Deploy to goreleaser
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      -
        name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v3
        with:
          version: latest
          args: release --clean
          workdir: ./plugins/souin
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GO_VERSION: ${{ secrets.GITHUB_TOKEN }}
  generate-tyk-versions:
    name: Generate Tyk plugin binaries
    runs-on: ubuntu-latest
    env:
      LATEST_VERSION: v5.0
      PREVIOUS_VERSION: v4.3
      SECOND_TO_LAST_VERSION: v4.2
    steps:
      -
        name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      -
        name: Checkout code
        uses: actions/checkout@v4
      -
        name: Generate Tyk amd64 artifacts
        run: cd plugins/tyk && make vendor && docker compose -f docker-compose.yml.artifacts up
      - 
        name: Upload Tyk amd64 artifacts
        uses: actions/upload-artifact@v3
        with: 
          path: plugins/tyk/*.so
