|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v[0-9]*.[0-9]*.[0-9]*' # matches "v<number...>.<number...>.<number>..." |
| 7 | + |
| 8 | +jobs: |
| 9 | + # Check prerequisites for the workflow |
| 10 | + prereqs: |
| 11 | + name: Prerequisites |
| 12 | + runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + tag_name: ${{ steps.tag.outputs.name }} # The full name of the tag, e.g. v1.0.0 |
| 15 | + tag_version: ${{ steps.tag.outputs.version }} # The version number (without preceding "v"), e.g. 1.0.0 |
| 16 | + steps: |
| 17 | + - name: Determine tag to build |
| 18 | + run: | |
| 19 | + echo "name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT |
| 20 | + echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT |
| 21 | + id: tag |
| 22 | + |
| 23 | + package: |
| 24 | + needs: prereqs |
| 25 | + name: ${{matrix.jobs.jobname}} |
| 26 | + strategy: |
| 27 | + fail-fast: false |
| 28 | + matrix: |
| 29 | + jobs: |
| 30 | + - jobname: Create MacOS .pkg (x86_64) |
| 31 | + goarch: amd64 |
| 32 | + pool: macos-latest |
| 33 | + artifact: _dist/*.pkg |
| 34 | + - jobname: Create MacOS .pkg (ARM64) |
| 35 | + goarch: arm64 |
| 36 | + pool: macos-latest |
| 37 | + artifact: _dist/*.pkg |
| 38 | + - jobname: Create binary Debian package (x86_64) |
| 39 | + goarch: amd64 |
| 40 | + pool: ubuntu-latest |
| 41 | + artifact: _dist/*.deb |
| 42 | + env: |
| 43 | + GOARCH: ${{matrix.jobs.goarch}} |
| 44 | + runs-on: ${{matrix.jobs.pool}} |
| 45 | + steps: |
| 46 | + - name: Setup Go |
| 47 | + uses: actions/setup-go@v3 |
| 48 | + with: |
| 49 | + go-version: '1.19.0' |
| 50 | + - name: Clone repository |
| 51 | + uses: actions/checkout@v3 |
| 52 | + - name: Build the release artifact |
| 53 | + run: make package VERSION=${{ needs.prereqs.outputs.tag_version }} |
| 54 | + - name: Get the release artifact |
| 55 | + shell: bash |
| 56 | + run: | |
| 57 | + artifacts=(${{matrix.jobs.artifact}}) |
| 58 | +
|
| 59 | + # Get path to, and name of, artifact |
| 60 | + artifactPath="${artifacts[0]}" |
| 61 | + artifactName=$(basename "$artifactPath") |
| 62 | +
|
| 63 | + # Export variables to environment |
| 64 | + echo "artifactPath=$artifactPath" >> $GITHUB_ENV |
| 65 | + echo "artifactName=$artifactName" >> $GITHUB_ENV |
| 66 | + - name: Upload release artifact |
| 67 | + uses: actions/upload-artifact@v3 |
| 68 | + with: |
| 69 | + name: ${{env.artifactName}} |
| 70 | + path: ${{github.workspace}}/${{env.artifactPath}} |
| 71 | + if-no-files-found: error |
| 72 | + |
| 73 | + create-github-release: |
| 74 | + needs: [prereqs, package] |
| 75 | + name: Create release with artifacts |
| 76 | + runs-on: ubuntu-latest |
| 77 | + steps: |
| 78 | + - name: Download packages |
| 79 | + uses: actions/download-artifact@v3 |
| 80 | + with: |
| 81 | + path: artifacts-raw |
| 82 | + - name: Consolidate artifact directory |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + # This step is needed to extract the artifacts from their wrapper |
| 86 | + # parent directories. For more details, see |
| 87 | + # https://github.com/actions/download-artifact#download-all-artifacts |
| 88 | + mkdir artifacts |
| 89 | + mv artifacts-raw/*/* artifacts/ |
| 90 | + - name: Create release & attach artifacts |
| 91 | + uses: actions/github-script@v6 |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const fs = require('fs'); |
| 95 | + const path = require('path'); |
| 96 | +
|
| 97 | + var releaseMetadata = { |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo |
| 100 | + }; |
| 101 | +
|
| 102 | + // Create the release |
| 103 | + var tagName = "${{ needs.prereqs.outputs.tag_name }}"; |
| 104 | + var createdRelease = await github.rest.repos.createRelease({ |
| 105 | + ...releaseMetadata, |
| 106 | + draft: true, |
| 107 | + tag_name: tagName, |
| 108 | + name: tagName, |
| 109 | + generate_release_notes: true |
| 110 | + }); |
| 111 | + releaseMetadata.release_id = createdRelease.data.id; |
| 112 | +
|
| 113 | + // Upload contents of directory to the release created above |
| 114 | + async function uploadDirectoryToRelease(directory, includeExtensions=[]) { |
| 115 | + return fs.promises.readdir(directory) |
| 116 | + .then(async(files) => Promise.all( |
| 117 | + files.filter(file => { |
| 118 | + return includeExtensions.length==0 || includeExtensions.includes(path.extname(file).toLowerCase()); |
| 119 | + }) |
| 120 | + .map(async (file) => { |
| 121 | + var filePath = path.join(directory, file); |
| 122 | + return github.rest.repos.uploadReleaseAsset({ |
| 123 | + ...releaseMetadata, |
| 124 | + name: file, |
| 125 | + headers: { |
| 126 | + "content-length": (await fs.promises.stat(filePath)).size |
| 127 | + }, |
| 128 | + data: fs.createReadStream(filePath) |
| 129 | + }); |
| 130 | + })) |
| 131 | + ); |
| 132 | + } |
| 133 | +
|
| 134 | + await Promise.all([ |
| 135 | + // Upload all artifacts |
| 136 | + uploadDirectoryToRelease('artifacts', ['.pkg', '.deb']) |
| 137 | + ]); |
| 138 | +
|
0 commit comments