Added a new sync script for moving releases to ultimate-releases

This commit is contained in:
Matthew Rathbone
2024-12-11 14:18:05 -06:00
parent 4c7c165d83
commit 2686a2ba84
3 changed files with 79 additions and 92 deletions

View File

@@ -1,61 +0,0 @@
module.exports = async (octo, sourceRepo, destRepo, sourceReleaseId, destReleaseId) => {
const gitHubKey = process.env.GITHUB_TOKEN;
if (!gitHubKey) {
throw new Error("GITHUB_TOKEN is not set in environment variables.");
}
// Get the source release details
const [sourceOwner, sourceRepoName] = sourceRepo.split("/");
const { data: sourceRelease } = await octo.rest.repos.getRelease({
owner: sourceOwner,
repo: sourceRepoName,
release_id: sourceReleaseId,
});
// Prepare to copy assets
const [destOwner, destRepoName] = destRepo.split("/");
const assetPromises = sourceRelease.assets.map(async (asset) => {
try {
console.log(`Processing asset: ${asset.name}`);
// Use `browser_download_url` for downloading assets
const response = await fetch(asset.browser_download_url, {
headers: {
Accept: "application/octet-stream",
Authorization: `Bearer ${gitHubKey}`,
},
});
if (!response.ok) {
throw new Error(`Failed to download asset ${asset.name}: ${response.statusText}`);
}
const assetData = await response.arrayBuffer();
// Upload the asset to the destination release
await octo.rest.repos.uploadReleaseAsset({
owner: destOwner,
repo: destRepoName,
release_id: destReleaseId,
name: asset.name,
label: asset.label || undefined, // Label is optional
data: Buffer.from(assetData), // Convert ArrayBuffer to Buffer
headers: {
"content-length": asset.size,
"content-type": asset.content_type || "application/octet-stream",
},
});
console.log(`Successfully uploaded asset: ${asset.name}`);
} catch (error) {
console.error(`Failed to process asset ${asset.name}: ${error.message}`);
}
});
// Wait for all assets to be processed
await Promise.all(assetPromises);
console.log("All assets processed.");
};

View File

@@ -403,38 +403,11 @@ jobs:
steps:
- name: Check out Git repository
uses: actions/checkout@v1
# this way we can fix this script without a full redeploy
with:
ref: master
# Requires us to use a github token with more power (to create drafts on another repo)
- name: Create or check draft release (ultimate)
id: create_release
uses: actions/github-script@v7
env:
TAG_NAME: ${{ github.ref_name }}
OWNER: 'beekeeper-studio'
REPO: 'ultimate-releases'
with:
github-token: ${{ secrets.GH_DEPLOY_TOKEN }}
script: |
const script = require('./.github/scripts/create_draft_release.js')
await script({github, context, core}, process.env.OWNER, process.env.REPO, process.env.TAG_NAME)
- name: Copy community release to ultimate
uses: actions/github-script@v7
env:
TAG_NAME: ${{ github.ref_name }}
SOURCE: ${{ needs.create_draft_release.outputs.id }}
DEST: ${{ steps.create_release.outputs.id }}
GITHUB_TOKEN: ${{ secrets.GH_DEPLOY_TOKEN }}
with:
script: |
const script = require('./.github/scripts/duplicate_release.js')
await script(
github,
'beekeeper-studio/beekeeper-studio',
'beekeeper-studio/ultimate-releases',
process.env.SOURCE,
process.env.DEST
)
GH_TOKEN: ${{ secrets.GH_DEPLOY_TOKEN }}
SOURCE: beekeeper-studio/beekeeper-studio
DEST: beekeeper-studio/ultimate-releases
run: 'bin/sync-release.sh "$SOURCE" "$DEST" "$TAG_NAME"'

75
bin/sync-release.sh Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
# Function to print usage
print_usage() {
echo "Usage: $0 <source-repo> <target-repo> <release-tag>"
echo "Example: $0 user/source-repo user/target-repo v1.0.0"
exit 1
}
# Check for required arguments
if [ $# -ne 3 ]; then
print_usage
fi
# Assign arguments to variables
SOURCE_REPO=$1
TARGET_REPO=$2
RELEASE_TAG=$3
# Check if gh is installed
if ! command -v gh &> /dev/null; then
echo "gh (GitHub CLI) could not be found. Please install it first."
exit 1
fi
# Fetch the release info from the source repo
echo "Fetching release information for tag $RELEASE_TAG from $SOURCE_REPO..."
RELEASE_JSON=$(gh release view $RELEASE_TAG --repo $SOURCE_REPO --json tagName,body,name)
if [ -z "$RELEASE_JSON" ]; then
echo "Failed to fetch release information. Please check if the release tag and source repository are correct."
exit 1
fi
# Extract the release name and body from the JSON response
RELEASE_NAME=$(echo $RELEASE_JSON | jq -r '.name')
RELEASE_BODY=$(echo $RELEASE_JSON | jq -r '.body')
# Create the release in the target repo
echo "Creating release $RELEASE_TAG in $TARGET_REPO..."
gh release create $RELEASE_TAG --repo $TARGET_REPO --title "$RELEASE_NAME" --notes "$RELEASE_BODY"
if [ $? -ne 0 ]; then
echo "Failed to create release in $TARGET_REPO."
exit 1
fi
# List assets from the source release
echo "Listing assets for release $RELEASE_TAG in $SOURCE_REPO..."
ASSETS=$(gh release view $RELEASE_TAG --repo $SOURCE_REPO --json assets --jq '.assets[].url')
# Download and upload each asset
for ASSET_URL in $ASSETS; do
ASSET_NAME=$(basename $ASSET_URL)
echo "Downloading asset $ASSET_NAME from $SOURCE_REPO..."
curl -L -o $ASSET_NAME $ASSET_URL
if [ $? -ne 0 ]; then
echo "Failed to download asset $ASSET_NAME."
exit 1
fi
echo "Uploading asset $ASSET_NAME to $TARGET_REPO..."
gh release upload $RELEASE_TAG $ASSET_NAME --repo $TARGET_REPO
if [ $? -ne 0 ]; then
echo "Failed to upload asset $ASSET_NAME."
exit 1
fi
# Clean up downloaded asset
rm $ASSET_NAME
done
echo "Release $RELEASE_TAG successfully copied from $SOURCE_REPO to $TARGET_REPO."