forked from fedibtc/fedi-alpha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Create or Update Release | ||
|
||
on: | ||
workflow_dispatch: | ||
repository_dispatch: | ||
types: [publish-apk] | ||
|
||
jobs: | ||
create-or-update-release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout script in repo | ||
uses: actions/checkout@v3 | ||
|
||
- name: Run publish-release | ||
uses: actions/github-script@v7 | ||
id: gh-script | ||
env: | ||
RELEASE_ID: ${{ github.event.client_payload.release_id }} | ||
with: | ||
github-token: ${{ secrets.SOURCE_GH_TOKEN }} | ||
result-encoding: string | ||
script: | | ||
const script = require('./publish-release.js') | ||
await script({github, context, core}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
module.exports = async ({ github, context, core }) => { | ||
const { RELEASE_ID } = process.env; | ||
|
||
const publish = async () => { | ||
// Fetch release assets from the source repo | ||
const assets = await github.rest.repos.listReleaseAssets({ | ||
owner: context.repo.owner, | ||
repo: 'fedi', | ||
release_id: RELEASE_ID, | ||
}); | ||
|
||
console.log(`assets: ${JSON.stringify(assets)}`); | ||
|
||
// Find the APK file | ||
const apkAsset = assets.data.find((asset) => asset.name.endsWith('.apk')); | ||
if (!apkAsset) { | ||
throw new Error('APK file not found'); | ||
} | ||
|
||
// Extract version and commit hash from the APK filename | ||
const regex = /app-production-release-(\d+\.\d+\.\d+)-([0-9a-f]+)\.apk/; | ||
const match = apkAsset.name.match(regex); | ||
if (!match) { | ||
throw new Error('Invalid APK filename format'); | ||
} | ||
|
||
const [fullMatch, version, commitHash] = match; | ||
|
||
// Prepare new release details | ||
const newTagName = `v${version}`; | ||
const newTitle = `Fedi Alpha v${version.split('.').slice(0, 2).join('.')} - APK Download`; | ||
const truncatedCommitHash = commitHash.substring(0, 6); | ||
const newFileName = `app-production-release-${version}-${truncatedCommitHash}.apk`; | ||
const newDescription = `Download & Test Fedi Alpha <br><br> Download: [${newFileName}](https://github.com/${context.repo.owner}/fedi-alpha/releases/download/${newTagName}/${newFileName})`; | ||
|
||
// Check if a release with the same title exists in the target repo | ||
const releases = await github.rest.repos.listReleases({ | ||
owner: context.repo.owner, | ||
repo: 'fedi-alpha', | ||
}); | ||
|
||
// Function to upload a new APK to a release | ||
async function uploadNewApk(releaseId) { | ||
// Download the APK from the source repository | ||
const apkBuffer = await github.rest.repos.getReleaseAsset({ | ||
owner: context.repo.owner, | ||
repo: 'fedi', | ||
asset_id: apkAsset.id, | ||
headers: { | ||
Accept: 'application/octet-stream', | ||
}, | ||
}); | ||
|
||
// Upload the APK to the target repository | ||
await github.rest.repos.uploadReleaseAsset({ | ||
url: `https://uploads.github.com/repos/${ | ||
context.repo.owner | ||
}/fedi-alpha/releases/${releaseId}/assets?name=${encodeURIComponent(newFileName)}`, | ||
headers: { | ||
'content-type': 'application/vnd.android.package-archive', | ||
'content-length': apkBuffer.data.length, | ||
}, | ||
data: apkBuffer.data, | ||
name: newFileName, | ||
}); | ||
} | ||
|
||
// Function to delete old APK from a release | ||
async function deleteOldApk(releaseId) { | ||
const assets = await github.rest.repos.listReleaseAssets({ | ||
owner: context.repo.owner, | ||
repo: 'fedi-alpha', | ||
release_id: releaseId, | ||
}); | ||
|
||
const oldApkAsset = assets.data.find((asset) => asset.name.endsWith('.apk')); | ||
if (oldApkAsset) { | ||
await github.rest.repos.deleteReleaseAsset({ | ||
owner: context.repo.owner, | ||
repo: 'fedi-alpha', | ||
asset_id: oldApkAsset.id, | ||
}); | ||
} | ||
} | ||
|
||
const existingRelease = releases.data.find((release) => release.name === newTitle); | ||
|
||
if (existingRelease) { | ||
// Update existing release | ||
await github.rest.repos.updateRelease({ | ||
owner: context.repo.owner, | ||
repo: 'fedi-alpha', | ||
release_id: existingRelease.id, | ||
tag_name: newTagName, | ||
name: newTitle, | ||
body: newDescription, | ||
}); | ||
// Upload new APK and delete old APK | ||
await deleteOldApk(existingRelease.id); | ||
await uploadNewApk(existingRelease.id); | ||
} else { | ||
// Create a new release | ||
const newRelease = await github.rest.repos.createRelease({ | ||
owner: context.repo.owner, | ||
repo: 'fedi-alpha', | ||
tag_name: newTagName, | ||
name: newTitle, | ||
body: newDescription, | ||
}); | ||
// Upload APK | ||
await uploadNewApk(newRelease.data.id); | ||
} | ||
}; | ||
|
||
try { | ||
publish(); | ||
} catch (error) { | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
}; |