# Generated by `starci compile` from wrapping-release-actions.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script looks at the tag's name. That does not carry over: every tag is
#    treated alike.
#  - Passes are remembered in this repository's Actions cache, which GitHub
#    scopes by branch and evicts when old. starci's own store has neither
#    limit.

name: CI

on:
  push:
    branches: ['**']
    tags: ['**']
  pull_request:
  workflow_dispatch:

jobs:
  test:
    name: "test"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /**/*.go
            /go.mod
            /go.sum
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-test-7226615a5584a071-${{ hashFiles('**/*.go', 'go.mod', 'go.mod/**', 'go.sum', 'go.sum/**') }}
      - name: "test"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            go test ./...
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            golang:1.26 sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  push-image:
    name: "push image"
    runs-on: ubuntu-latest
    needs: [test]
    if: ${{ (github.event_name == 'push' && github.ref_type == 'tag') }}
    steps:
      - uses: actions/checkout@v4
      - name: "push image"
        env:
          STARCI_RUN: |-
            echo "$REGISTRY_TOKEN" | docker login ghcr.io -u "$REGISTRY_USER" --password-stdin && docker build -t ghcr.io/sam/app:0.0.0 -t ghcr.io/sam/app:0.0 -t ghcr.io/sam/app:0 -t ghcr.io/sam/app:latest . && docker push ghcr.io/sam/app:0.0.0 && docker push ghcr.io/sam/app:0.0 && docker push ghcr.io/sam/app:0 && docker push ghcr.io/sam/app:latest
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            -e 'REGISTRY_TOKEN=' \
            -e 'REGISTRY_USER=' \
            docker:27-cli sh -c "$STARCI_RUN"

  goreleaser:
    name: "goreleaser"
    runs-on: ubuntu-latest
    needs: [test]
    if: ${{ (github.event_name == 'push' && github.ref_type == 'tag') }}
    steps:
      - uses: actions/checkout@v4
      - name: "goreleaser"
        env:
          STARCI_RUN: |-
            goreleaser release --clean
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            -e 'GITHUB_TOKEN=' \
            goreleaser/goreleaser:latest sh -c "$STARCI_RUN"
