name: Release
on:
  push:
    tags:
      - "v*"

jobs:
  release:
    name: Release
    uses: ./.github/workflows/reusable-release.yaml
    with:
      goreleaser_config: goreleaser.yml
      goreleaser_options: '--rm-dist --timeout 90m'
    secrets: inherit

  deploy-packages:
    name: Deploy rpm/dep packages
    needs: release # run this job after 'release' job completes
    runs-on: ubuntu-18.04 # 20.04 doesn't provide createrepo for now
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Restore Trivy binaries from cache
        uses: actions/cache@v3.0.8
        with:
          path: dist/
          key: ${{ runner.os }}-bins-${{github.workflow}}-${{github.sha}}

      - name: Install dependencies
        run: |
          sudo apt-get -y update
          sudo apt-get -y install rpm reprepro createrepo distro-info

      - name: Checkout trivy-repo
        uses: actions/checkout@v3
        with:
          repository: ${{ github.repository_owner }}/trivy-repo
          path: trivy-repo
          fetch-depth: 0
          token: ${{ secrets.ORG_REPO_TOKEN }}

      - name: Setup git settings
        run: |
          git config --global user.email "knqyf263@gmail.com"
          git config --global user.name "Teppei Fukuda"

      - name: Create rpm repository
        run: ci/deploy-rpm.sh

      - name: Import GPG key
        run: echo -e "${{ secrets.GPG_KEY }}" | gpg --import

      - name: Create deb repository
        run: ci/deploy-deb.sh
