name: cicd

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: write
  pull-requests: write
  packages: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      - run: npm ci
      - run: npm run build

      - uses: azure/setup-helm@v4
        with:
          version: 'latest'
      - name: Validate Helm chart
        run: |
          helm lint chart
          helm template test-release chart

  release:
    needs: [test]
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    outputs:
      released: ${{ steps.release-please.outputs.release_created }}
      tag: ${{ steps.release-please.outputs.tag_name }}
    steps:
      - uses: googleapis/release-please-action@v4
        id: release-please
        with:
          config-file: .github/release-please-config.json
          manifest-file: .github/release-please-manifest.json

  publish-docker:
    needs: [release]
    if: ${{ needs.release.outputs.released }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.release.outputs.tag }}

      - uses: docker/setup-buildx-action@v3

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

      - name: Extract version
        id: version
        run: |
          VERSION=${{ needs.release.outputs.tag }}
          echo "version=${VERSION#v}" >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:${{ steps.version.outputs.version }}
            ghcr.io/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

  publish-helm:
    needs: [release]
    if: ${{ needs.release.outputs.released }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.release.outputs.tag }}

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

      - name: Extract version
        id: version
        run: |
          VERSION=${{ needs.release.outputs.tag }}
          echo "version=${VERSION#v}" >> $GITHUB_OUTPUT

      - name: Package and push Helm chart
        run: |
          helm package ./chart
          helm push shellwright-${{ steps.version.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}/charts

  publish-npm:
    needs: [release]
    if: ${{ needs.release.outputs.released }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.release.outputs.tag }}

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - run: npm ci
      - run: npm run build
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
