Publish Beta Release #64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Beta Release | |
on: | |
push: | |
tags: | |
- "*_beta" | |
workflow_dispatch: | |
inputs: | |
version: | |
description: "Version to release" | |
required: true | |
jobs: | |
check-version-change: | |
outputs: | |
changed: ${{ steps.check-version.outputs.result }} | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Check if version has changed | |
id: check-version | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
// Get the version from the workflow input | |
let version = '${{ github.event.inputs.version }}'; | |
if (!version) { | |
fs = require('fs'); | |
let v = ''; | |
try { | |
v = fs.readFileSync('./VERSION', 'utf8'); | |
console.log(`Version found: ${v}`); | |
} | |
catch (err) { | |
return false; | |
} | |
if (!v) { | |
return false; | |
} else { | |
version = v; | |
} | |
} | |
let majorMinorVersion = version.split('.').slice(0, 2).join('.'); | |
version = `${majorMinorVersion}.${{github.run_id}}`; | |
console.log(`Version: ${version}`) | |
// Find a release for that version | |
const release = await github.rest.repos.getReleaseByTag({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
tag: `v${version}-beta`, | |
}).catch(() => null); | |
// If the release exists, the version has not changed | |
if (release) { | |
console.log(`Version ${version} has an existing release`); | |
console.log(release.data.html_url); | |
core.summary.addLink(`Release v${version}`, release.data.html_url); | |
await core.summary.write(); | |
return "false"; | |
} | |
console.log(`Version ${version} does not have a release`); | |
return true; | |
beta-release: | |
name: Release Beta version | |
needs: | |
- check-version-change | |
if: ${{ needs.check-version-change.outputs.changed == 'true' }} | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: read | |
env: | |
EXT_VERSION: "" # will be set in the workflow | |
MAJOR_VERSION: "" # will be set in the workflow | |
outputs: | |
version: ${{ env.EXT_VERSION }} | |
majorVersion: ${{ env.MAJOR_VERSION }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set new version | |
run: | | |
VERSION=${{ github.event.inputs.version }} | |
MAJOR_VERSION=${VERSION%.*} | |
NEW_VERSION=${VERSION%.*}.${{ github.run_id }} | |
echo "Beta Version: $NEW_VERSION" | |
echo "EXT_VERSION=${NEW_VERSION}" >> "$GITHUB_ENV" | |
echo "MAJOR_VERSION=${MAJOR_VERSION}" >> "$GITHUB_ENV" | |
- name: Generate release notes | |
run: | | |
./.github/workflow_scripts/get-latest-changelog.sh --output-to-file | |
cat release_notes.md | |
- name: Create release and upload release asset | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require("fs"); | |
const release = await github.rest.repos.createRelease({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: fs.readFileSync("release_notes.md", "utf8"), | |
tag_name: "v${{ env.EXT_VERSION }}-beta", | |
name: "v${{ env.EXT_VERSION }}-beta", | |
draft: false, | |
prerelease: true | |
}); | |
beta-releases-matrix: | |
needs: | |
- check-version-change | |
- beta-release | |
name: Release Go Binary | |
runs-on: ubuntu-latest | |
env: | |
EXT_VERSION: ${{ needs.beta-release.outputs.version }} | |
AmplitudeApiKey: ${{ secrets.AMPLITUDE_API_KEY }} | |
strategy: | |
fail-fast: false | |
matrix: | |
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 | |
goos: [linux, windows, darwin] | |
goarch: ["386", amd64, arm64] | |
exclude: | |
- goarch: "386" | |
goos: darwin | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Add Inbuilt Variables | |
run: | | |
sed -i "s/var AmplitudeApiKey = \"\"/var AmplitudeApiKey = \"${{ env.AmplitudeApiKey }}\"/g" ./src/constants/amplitude.go | |
sed -i "/^\tver =/c\\\tver = \"${{ env.EXT_VERSION }}\"" ./src/main.go | |
sed -i "/^\/\/ @version/c\\// @version ${{ env.EXT_VERSION }}" ./src/main.go | |
- uses: wangyoucao577/go-release-action@v1 | |
timeout-minutes: 10 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
goos: ${{ matrix.goos }} | |
goarch: ${{ matrix.goarch }} | |
goversion: "https://dl.google.com/go/go1.21.1.linux-amd64.tar.gz" | |
project_path: "./src" | |
binary_name: "prldevops" | |
release_name: "v${{ env.EXT_VERSION }}-beta" | |
build-containers: | |
needs: | |
- check-version-change | |
- beta-release | |
env: | |
EXT_VERSION: ${{ needs.beta-release.outputs.version }} | |
AmplitudeApiKey: ${{ secrets.AMPLITUDE_API_KEY }} | |
name: Build Docker Images | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Add Inbuilt Variables | |
run: | | |
sed -i "s/var AmplitudeApiKey = \"\"/var AmplitudeApiKey = \"${{ env.AmplitudeApiKey }}\"/g" ./src/constants/amplitude.go | |
sed -i "/^\tver =/c\\\tver = \"${{ env.EXT_VERSION }}\"" ./src/main.go | |
sed -i "/^\/\/ @version/c\\// @version ${{ env.EXT_VERSION }}" ./src/main.go | |
- uses: docker/setup-buildx-action@v1 | |
- uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- uses: docker/build-push-action@v2 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: | | |
${{ secrets.DOCKER_USERNAME }}/prl-devops-service:latest-beta | |
${{ secrets.DOCKER_USERNAME }}/prl-devops-service:${{ env.EXT_VERSION }}-beta | |
remove-old-beta-release: | |
name: Remove old beta release | |
needs: | |
- check-version-change | |
- beta-release | |
- beta-releases-matrix | |
- build-containers | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: read | |
env: | |
EXT_VERSION: ${{ needs.beta-release.outputs.version }} | |
MAJOR_VERSION: ${{ needs.beta-release.outputs.version }} | |
steps: | |
- name: Remove old beta release | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require("fs"); | |
let version ='${{ github.event.inputs.version }}'.trim().split('.').slice(0, 2).join('.'); | |
let currentVersion = `${version}.${{github.run_id}}-beta`; | |
console.log(`Current Version: ${currentVersion}`); | |
const releases = await github.rest.repos.listReleases({ | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
for(const idx in releases.data) { | |
const release = releases.data[idx]; | |
if (release.tag_name.includes("-beta") && release.tag_name !== `v${currentVersion}`) { | |
for(const assetIdx in release.assets) { | |
const asset = release.assets[assetIdx]; | |
console.log(`Deleting asset: ${asset.name}`); | |
await github.rest.repos.deleteReleaseAsset({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
asset_id: asset.id | |
}); | |
} | |
console.log(`Deleting release: ${release.tag_name}`); | |
await github.rest.repos.deleteRelease({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
release_id: release.id | |
}); | |
console.log(`Deleting tag: tags/${release.tag_name}`); | |
await github.rest.git.deleteRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `tags/${release.tag_name}` | |
}); | |
} | |
} |