Files
zurg/.github/workflows/build.yml
2024-07-21 04:43:20 +02:00

202 lines
6.3 KiB
YAML

name: Build and Release
on:
push:
tags:
- 'v**'
schedule:
- cron: '45 1 * * *'
jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Cache Go Modules
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Check out code
uses: actions/checkout@v4
- name: Check for new commits (nightly only)
id: check
if: github.event_name == 'schedule'
continue-on-error: false
run: |
# 3AM CET
since="$(date -d 'yesterday' +%Y-%m-%d) 01:00:00"
count=$(git rev-list --count --since="$since" --until=now HEAD)
if [ "$count" -gt 0 ]; then
echo "new_commits=true" >> $GITHUB_OUTPUT
else
echo "no new commits found for nightly build"
exit 1
fi
- name: Generate build variables
id: prep
run: |
if [ "${{ github.event_name }}" = "schedule" ]; then
echo "version=$(date +'%Y.%m.%d-nightly')" >> $GITHUB_OUTPUT
else
echo "version=$(echo ${GITHUB_REF#refs/tags/})" >> $GITHUB_OUTPUT
fi
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.22
pkg: cmd/zurg
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: |
if [ "${{ github.event_name }}" = "schedule" ]; then
echo "version=$(date +'%Y.%m.%d-nightly')" >> $GITHUB_OUTPUT
else
echo "version=$(echo ${GITHUB_REF#refs/tags/})" >> $GITHUB_OUTPUT
fi
- 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"
- name: Create Release in Target Repository
uses: actions/github-script@v7
with:
github-token: ${{ secrets.TOKEN }}
script: |
const ownerName = 'debridmediamanager';
const tagName = '${{ steps.version.outputs.version }}';
const repoName = `zurg${tagName.includes('-rc') || tagName.includes('-nightly') ? '' : '-testing'}`;
const prerelease = tagName.includes('-nightly');
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,
body: releaseBody,
prerelease: prerelease,
});
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
});
});