name: Create Release
on:
  push:
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
jobs:
  build:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: stable
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Build Artifacts
        run: go run build_release.go
      - name: Create Release
        uses: actions/github-script@v2
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const fs = require('fs').promises;
            console.log('environment', process.versions);
            const { repo: { owner, repo }, sha } = context;

            const release = await github.repos.createRelease({
              owner, repo,
              tag_name: process.env.GITHUB_REF,
              name: process.env.GITHUB_REF.split('/')[2],
              draft: true,
              target_commitish: sha
            });
            console.log('created release', { release });

            for (let file of await fs.readdir('./release')) {
              console.log('uploading', file);
              await github.repos.uploadReleaseAsset({
                owner, repo,
                release_id: release.data.id,
                name: file,
                data: await fs.readFile(`./release/${file}`)
              });
            }
