name: Container & Helm chart
on: [push]

jobs:
  semrel_dryrun:
    name: Semantic Release (Dry-run)
    runs-on: dind-rootless
    outputs:
      version: ${{ steps.sem_rel.outputs.version }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 20
      - name: Install dependencies
        run: |
          npm install \
            semantic-release \
            @semantic-release/commit-analyzer \
            @semantic-release/exec
      - name: Semantic Release (dry-run)
        id: sem_rel
        run: |
          npx semantic-release \
            --package @semantic-release/exec \
            --package semantic-release \
            --branches ${{ gitea.refname }} \
            --tag-format 'v${version}' \
            --dry-run \
            --plugins @semantic-release/commit-analyzer,@semantic-release/exec \
            --analyzeCommits @semantic-release/commit-analyzer \
            --verifyRelease @semantic-release/exec \
            --verifyReleaseCmd 'echo "version=${nextRelease.version}" >> $GITHUB_OUTPUT'
        env:
          GIT_CREDENTIALS: ${{ secrets.GIT_USERNAME }}:${{ secrets.GIT_APIKEY }}
      - name: Assert semantic release output
        run: |
          [[ -z "${{ steps.sem_rel.outputs.version }}" ]] && {
            echo 'No release tag - exiting'; exit 1
          } || {
            echo 'Release tag set correctly: ${{ steps.sem_rel.outputs.version }}'; exit 0
          }

  build_container:
    name: Container image
    container: catthehacker/ubuntu:act-latest
    runs-on: dind
    needs: semrel_dryrun
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Kaniko build
        uses: aevea/action-kaniko@master
        with:
          registry: code.spamasaurus.com
          image: djpbessems/spamasaurusrex
          username: ${{ secrets.GIT_USERNAME }}
          password: ${{ secrets.GIT_APIKEY }}
          tag: ${{ needs.semrel_dryrun.outputs.version }}

  build_chart:
    name: Helm chart
    runs-on: dind-rootless
    needs: semrel_dryrun
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Inject semantic release version
        run: |
          sed -i 's/{{ chart_version }}/'${{ needs.semrel_dryrun.outputs.version }}'/g' charts/spamasaurusrex/Chart.yaml
          sed -i 's/{{ image_tag }}/'${{ needs.semrel_dryrun.outputs.version }}'/g' charts/spamasaurusrex/values.yaml
      - name: Install helm w/ plugins
        run: |
          curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | tee /usr/share/keyrings/helm.gpg > /dev/null
          apt-get install apt-transport-https --yes
          echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | tee /etc/apt/sources.list.d/helm-stable-debian.list
          apt-get update && apt-get install helm --yes
          helm plugin install https://github.com/chartmuseum/helm-push
      - name: Package helm chart into archive
        run: |
          helm package ./charts/spamasaurusrex
      - name: Add target helm repository
        run: |
          helm repo add \
            --username ${{ secrets.GIT_USERNAME }} \
            --password ${{ secrets.GIT_APIKEY }} \
            spamasaurus \
            https://code.spamasaurus.com/api/packages/${{ secrets.GIT_USERNAME }}/helm
      - name: Push helm chart
        run: |
          helm cm-push \
            spamasaurusrex-*.tgz \
            spamasaurus

  semrel:
    name: Semantic Release
    runs-on: dind-rootless
    needs: [build_container, build_chart]
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 20
      - name: Install dependencies
        run: |
          npm install \
            semantic-release \
            @semantic-release/changelog \
            @semantic-release/commit-analyzer \
            @semantic-release/git \
            @semantic-release/release-notes-generator
      - name: Semantic Release
        run: |
          npx semantic-release \
            --branches ${{ gitea.refname }} \
            --tag-format 'v${version}' \
            --plugins @semantic-release/commit-analyzer,@semantic-release/release-notes-generator,@semantic-release/changelog,@semantic-release/git
        env:
          GIT_CREDENTIALS: ${{ secrets.GIT_USERNAME }}:${{ secrets.GIT_APIKEY }}