# Generated by tinfoil <%= @tinfoil_version %>. Do not edit by hand.
# Regenerate with: mix tinfoil.generate
name: Release

on:<%= case @trigger do %><% :release_published -> %>
  release:
    types: [published]<% :workflow_call -> %>
  workflow_call:
    inputs:
      tag:
        description: "Tag to build and attach assets to. Defaults to the calling workflow's release event tag."
        required: false
        type: string
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag to build and attach assets to, e.g. v1.2.3"
        required: true
        type: string<% _ -> %>
  push:
    tags: ["v*"]<% end %>

permissions:
  contents: write<%= if @attestations do %>
  id-token: write
  attestations: write<% end %>

env:
  MIX_ENV: prod
  APP_NAME: <%= @app %>

jobs:
  build:
    name: Build ${{ matrix.id }}
    strategy:
      fail-fast: false
      matrix:
        include:<%= for entry <- @build_entries do %>
          - id: <%= entry.id %>
            targets: "<%= entry.targets %>"
            runner: <%= entry.runner %><% end %>
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout
        uses: actions/checkout@v5<%= if @checkout_ref do %>
        with:
          ref: "<%= @checkout_ref %>"<% end %>

      - name: Set up Elixir/OTP
        uses: erlef/setup-beam@v1
        with:
          elixir-version: "<%= @ci.elixir_version %>"
          otp-version: "<%= @ci.otp_version %>"

      - name: Set up Zig
        uses: mlugg/setup-zig@v2
        with:
          version: "<%= @ci.zig_version %>"

      - name: Cache mix deps and build
        uses: actions/cache@v5
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-${{ matrix.id }}-mix-${{ hashFiles('mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.id }}-mix-

      - name: Install dependencies
        run: mix deps.get

      - name: Build and package release
        run: |
          for target in $(echo "${{ matrix.targets }}" | tr ',' ' '); do
            mix tinfoil.build --target "$target"
          done

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: binary-${{ matrix.id }}
          path: _tinfoil/*
          if-no-files-found: error
<%= if @attestations do %>
      - name: Attest build provenance
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: _tinfoil/*.tar.gz, _tinfoil/*.zip
<% end %>

  release:
    name: Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5<%= if @checkout_ref do %>
        with:
          ref: "<%= @checkout_ref %>"<% end %>

      - name: Set up Elixir/OTP
        uses: erlef/setup-beam@v1
        with:
          elixir-version: "<%= @ci.elixir_version %>"
          otp-version: "<%= @ci.otp_version %>"

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Cache mix deps and build
        uses: actions/cache@v5
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-release-mix-${{ hashFiles('mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-release-mix-

      - name: Install dependencies
        run: mix deps.get

      - name: <%= if @attach do %>Attach assets to GitHub Release<% else %>Publish GitHub Release<% end %>
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: mix tinfoil.publish --input-dir artifacts<%= if @attach do %> --attach<% else %><%= if @github.draft do %> --draft<% end %><% end %><%= @tag_flag %>
<%= if @homebrew.enabled do %>
  homebrew:
    name: Update Homebrew tap
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5<%= if @checkout_ref do %>
        with:
          ref: "<%= @checkout_ref %>"<% end %>

      - name: Set up Elixir/OTP
        uses: erlef/setup-beam@v1
        with:
          elixir-version: "<%= @ci.elixir_version %>"
          otp-version: "<%= @ci.otp_version %>"

      - name: Install dependencies
        run: mix deps.get

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true
<%= if @homebrew.auth == :deploy_key do %>
      - name: Set up SSH deploy key
        uses: webfactory/ssh-agent@v0.10.0
        with:
          ssh-private-key: ${{ secrets.<%= @homebrew.deploy_key_secret %> }}
<% end %>
      - name: Update Homebrew formula<%= if @homebrew.auth == :token do %>
        env:
          HOMEBREW_TAP_TOKEN: ${{ secrets.<%= @homebrew.token_secret %> }}<% end %>
        run: mix tinfoil.homebrew --input-dir artifacts<%= @tag_flag %>
<% end %>
<%= if @scoop.enabled do %>
  scoop:
    name: Update Scoop bucket
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5<%= if @checkout_ref do %>
        with:
          ref: "<%= @checkout_ref %>"<% end %>

      - name: Set up Elixir/OTP
        uses: erlef/setup-beam@v1
        with:
          elixir-version: "<%= @ci.elixir_version %>"
          otp-version: "<%= @ci.otp_version %>"

      - name: Install dependencies
        run: mix deps.get

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true
<%= if @scoop.auth == :deploy_key do %>
      - name: Set up SSH deploy key
        uses: webfactory/ssh-agent@v0.10.0
        with:
          ssh-private-key: ${{ secrets.<%= @scoop.deploy_key_secret %> }}
<% end %>
      - name: Update Scoop manifest<%= if @scoop.auth == :token do %>
        env:
          SCOOP_BUCKET_TOKEN: ${{ secrets.<%= @scoop.token_secret %> }}<% end %>
        run: mix tinfoil.scoop --input-dir artifacts<%= @tag_flag %>
<% end %>