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: 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 if: ${{ !startsWith(github.ref, 'refs/tags/v') }} 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: Cross-compile with xgo uses: crazy-max/ghaction-xgo@v3 if: ${{ startsWith(github.ref, 'refs/tags/v') }} with: xgo_version: latest go_version: 1.21 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: 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-testing run: | git config --global user.name "Zurg" git config --global user.email "zurg@debridmediamanager.com" git lfs install - name: Publish to zurg-early-releases if: ${{ !startsWith(github.ref, 'refs/tags/v') }} run: | git clone https://x-access-token:${{ secrets.TOKEN }}@github.com/debridmediamanager/zurg-early-releases.git mkdir -p zurg-early-releases/releases/${{ steps.version.outputs.version }} cp -fR compressed_artifacts/* zurg-early-releases/releases/${{ steps.version.outputs.version }} cd zurg-early-releases/releases/${{ steps.version.outputs.version }} git lfs track "*.zip" git add . git commit -m "Release ${{ steps.version.outputs.version }}" git push - name: Publish to zurg-testing if: ${{ startsWith(github.ref, 'refs/tags/v') }} run: | git clone https://x-access-token:${{ secrets.TOKEN }}@github.com/debridmediamanager/zurg-testing.git mkdir -p zurg-testing/releases/${{ steps.version.outputs.version }} cp -fR compressed_artifacts/* zurg-testing/releases/${{ steps.version.outputs.version }} cd zurg-testing/releases/${{ steps.version.outputs.version }} git lfs track "*.zip" git add . git commit -m "Release ${{ steps.version.outputs.version }}" git push - name: Create Release in Target Repository uses: actions/github-script@v7 with: github-token: ${{ secrets.PAT }} script: | const ownerName = 'debridmediamanager'; const repoName = 'zurg-early-releases'; 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 `; async function getGithubSponsors() { const query = ` query { organization(login: "debridmediamanager") { sponsorshipsAsMaintainer(first: 100) { nodes { sponsorEntity { ... on User { login } ... on Organization { login } } } } } }`; try { const response = await github.graphql(query); const sponsors = response.viewer.sponsorshipsAsMaintainer.nodes .filter(node => node.sponsorEntity) .map(node => '@' + node.sponsorEntity.login); return sponsors.join(' '); } catch (error) { console.error("Error fetching GitHub sponsors: ", error); return ''; } } const githubSponsors = await getGithubSponsors(); const releaseBody = ` Version: ${tagName} Commit: [${{ github.sha }}](https://github.com/debridmediamanager/zurg/commit/${{ github.sha }}) Hours since last release: ${hoursSinceLastRelease} hours ${sponsorshipDetails} Shoutouts to our amazing sponsors! ${githubSponsors} `; 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 }); });