Skip to content

Commit

Permalink
Improve and test the CI/CD flows.
Browse files Browse the repository at this point in the history
  • Loading branch information
cvetty committed Dec 13, 2024
1 parent 01ad244 commit c8ee623
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 260 deletions.
26 changes: 25 additions & 1 deletion .github/actions/draft-release/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,37 @@ module.exports = async ({ github, context, inputs }) => {
}
}

console.log("Getting the latest tag");
const majorVersion = inputs.branchName.split(".")[0];
const { data: tags } = await github.rest.repos.listTags({
owner: context.repo.owner,
repo: context.repo.repo,
});
const latestTag = tags.find(
(tag) =>
tag.name.startsWith(`${majorVersion}.`) && !tag.name.includes("-beta"),
);
if (latestTag) {
console.log(`Found latest tag: ${latestTag.name}`);
} else {
console.log(`No matching tags found for major version ${majorVersion}`);
}

console.log(`Creating a new untagged release for ${inputs.branchName}`);
const { data: notes } = await github.rest.repos.generateReleaseNotes({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
target_commitish: inputs.branchName,
previous_tag_name: latestTag?.name,
});
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: `Unreleased [${inputs.branchName}]`,
body: notes.body,
draft: true,
generate_release_notes: true,
target_commitish: inputs.branchName,
});
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Set Release Tag"
description: "Updates the tag of specific release"
name: "Pre-Release"
description: "Pre-release the draft release"

inputs:
branch_name:
Expand All @@ -17,7 +17,7 @@ outputs:
runs:
using: "composite"
steps:
- name: Set release tag
- name: Create/Update the pre-release
uses: actions/github-script@v6
id: set-release-tag
with:
Expand Down
28 changes: 28 additions & 0 deletions .github/actions/pre-release/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = async ({ github, context, inputs }) => {
const draftTagName = `unreleased[${inputs.branchName}]`;

const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
});

const release = releases.data.find(
(release) => release.tag_name == draftTagName && release.draft,
);
if (!release) {
throw new Error("Draft release not found!");
}

console.log("Updating the draft release...");
github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
name: inputs.tagName,
tag_name: inputs.tagName,
draft: false,
prerelease: true,
});

return release.id;
};
37 changes: 37 additions & 0 deletions .github/actions/release/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Release"
description: "Release the pre-release release"

inputs:
release_id:
description: "The id of the target release"
required: true
tag_name:
description: "The release version tag"
required: true
npm_auth_token:
description: "The NPM authentication token"
required: true

runs:
using: "composite"
steps:
- name: Release the package
uses: actions/github-script@v6
with:
script: |
const script = require('${{ github.action_path }}/script.js')
return await script({
github,
context,
inputs: {
releaseId: '${{ inputs.release_id }}',
tagName: '${{ inputs.tag_name }}',
}
})
# - name: Unpublish the beta versions
# shell: bash
# env:
# NODE_AUTH_TOKEN: ${{ inputs.npm_auth_token }}
# MAJOR_VERSION: ${{ inputs.tag_name.split('.')[0] }}
# run: ${{ github.action_path }}/unpublish-beta-versions.sh
59 changes: 59 additions & 0 deletions .github/actions/release/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = async ({ github, context, inputs }) => {
const majorVersion = inputs.tagName.split(".")[0];

// Get current release
const { data: release } = await github.rest.repos.getRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: inputs.releaseId,
});

console.log("Updating release tag...");
await github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
name: inputs.tagName,
tag_name: inputs.tagName,
draft: false,
prerelease: false,
});

// Cleanup beta releases
console.log("Cleaning up beta releases...");
const { data: releases } = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
});

for (const release of releases) {
if (
release.tag_name.startsWith(`${majorVersion}.`) &&
release.tag_name.includes("-beta")
) {
console.log(`Deleting beta release: ${release.tag_name}`);
await github.rest.repos.deleteRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
});
}
}

// Cleanup beta tags
console.log("Cleaning up beta tags...");
const { data: tags } = await github.rest.repos.listTags({
owner: context.repo.owner,
repo: context.repo.repo,
});
for (const tag of tags) {
if (tag.name.startsWith(`${majorVersion}.`) && tag.name.includes("-beta")) {
console.log(`Deleting beta tag: ${tag.name}`);
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tag.name}`,
});
}
}
};
18 changes: 18 additions & 0 deletions .github/actions/release/unpublish-beta-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

if [ -z "$MAJOR_VERSION" ]; then
echo "Error: MAJOR_VERSION environment variable is not set"
exit 1
fi

# Get all versions and store them in an array
versions=($(npm view @mtrdesign/krait-ui versions | tr -d "'[]," | tr " " "\n"))

# Loop through each version
for version in "${versions[@]}"; do
# Check if version starts with provided major version and contains "beta"
if [[ $version =~ ^${MAJOR_VERSION}\. && $version =~ beta ]]; then
echo "Deleting version: $version"
npm unpublish -f "@mtrdesign/krait-ui@$version"
fi
done
38 changes: 0 additions & 38 deletions .github/actions/set-release-tag/script.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ concurrency:
jobs:
draft-release:
name: Draft Github Release
if: github.actor != 'mtrpackageversionsmanager[bot]'
runs-on: ubuntu-22.04
steps:
- name: Checkout
Expand Down
Loading

0 comments on commit c8ee623

Please sign in to comment.