Files
zurg/.github/workflows/build.yml
2024-05-28 10:00:21 +02:00

184 lines
5.8 KiB
YAML

name: Build and Release
on:
push:
tags:
- '*'
jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Cache Go Modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Check out code
uses: actions/checkout@v4
- name: Generate build variables
id: prep
run: |
echo "version=$(echo ${GITHUB_REF#refs/tags/})" >> $GITHUB_OUTPUT
echo "built_at=$(date +%Y-%m-%dT%H:%M:%S)" >> $GITHUB_OUTPUT
echo "git_commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Cross-compile with xgo
uses: crazy-max/ghaction-xgo@v3
with:
xgo_version: latest
go_version: 1.21
pkg: cmd/zurg
targets: linux/amd64,windows/amd64,darwin/amd64
dest: artifacts
prefix: zurg
v: false
x: false
race: false
ldflags: >
-s -w
-X 'github.com/debridmediamanager/zurg/internal/version.BuiltAt=${{ steps.prep.outputs.built_at }}'
-X 'github.com/debridmediamanager/zurg/internal/version.GitCommit=${{ steps.prep.outputs.git_commit }}'
-X 'github.com/debridmediamanager/zurg/internal/version.Version=${{ steps.prep.outputs.version }}'
buildmode: default
trimpath: true
- name: Compress artifacts
run: |
mkdir -p compressed_artifacts
for file in artifacts/*; do
# Extract platform from the filename
platform=$(basename $file | sed 's/zurg-//; s/.exe//')
# Determine if it's a Windows executable
extension=""
if [[ $file == *".exe" ]]; then
extension=".exe"
fi
# Copy and zip the file
cp -fR "$file" "artifacts/zurg$extension"
(cd artifacts && zip -r "../compressed_artifacts/zurg-${{ steps.prep.outputs.version }}-$platform.zip" "zurg$extension")
done
- name: Upload artifacts to workflow
uses: actions/upload-artifact@v3
with:
name: compiled-binaries
path: compressed_artifacts/
release:
name: Release
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Set version
id: version
run: echo "::set-output name=version::$(echo ${GITHUB_REF#refs/tags/})"
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: compiled-binaries
path: compressed_artifacts
- name: Publish to zurg or zurg-testing
run: |
git config --global user.name "Zurg"
git config --global user.email "zurg@debridmediamanager.com"
git lfs install
if [[ "${{ steps.version.outputs.version }}" == *"-rc."* ]]; then
git remote set-url origin git@github.com:debridmediamanager/zurg.git
else
git remote set-url origin git@github.com:debridmediamanager/zurg-testing.git
fi
- name: Create Release in Target Repository
uses: actions/github-script@v7
with:
github-token: ${{ secrets.TOKEN }}
script: |
const ownerName = 'debridmediamanager';
const repoName = '${{ github.ref.includes("-rc.") ? "zurg" : "zurg-testing" }}';
const tagName = '${{ steps.version.outputs.version }}';
const releaseName = `${tagName}`;
let lastReleaseTime;
let hoursSinceLastRelease = 'N/A';
try {
const latestRelease = await github.rest.repos.getLatestRelease({
owner: ownerName,
repo: repoName,
});
lastReleaseTime = new Date(latestRelease.data.published_at);
const currentTime = new Date();
hoursSinceLastRelease = ((currentTime - lastReleaseTime) / 3600000).toFixed(2);
} catch (error) {
console.log("No previous releases found or error fetching latest release");
}
const sponsorshipDetails = `
Support [Debrid Media Manager](https://debridmediamanager.com/), zurg and other projects' development by becoming a sponsor:
- Patreon: https://www.patreon.com/debridmediamanager
- GitHub Sponsors: https://github.com/sponsors/debridmediamanager
- PayPal: https://paypal.me/yowmamasita
`;
const releaseBody = `
Version: ${tagName}
Commit: [${{ github.sha }}](https://github.com/debridmediamanager/zurg/commit/${{ github.sha }})
Hours since last release: ${hoursSinceLastRelease} hours
${sponsorshipDetails}
[version history](https://github.com/debridmediamanager/${repoName}/wiki/History)
`;
const response = await github.rest.repos.createRelease({
owner: ownerName,
repo: repoName,
tag_name: tagName,
name: releaseName,
body: releaseBody,
});
const uploadUrl = response.data.upload_url;
const fs = require('fs');
const path = require('path');
const artifactsPath = 'compressed_artifacts';
fs.readdirSync(artifactsPath).forEach(async (file) => {
const filePath = path.join(artifactsPath, file);
const fileStream = fs.createReadStream(filePath);
await github.rest.repos.uploadReleaseAsset({
url: uploadUrl,
headers: {
'content-type': 'application/octet-stream',
'content-length': fs.statSync(filePath).size
},
name: file,
data: fileStream
});
});