Skip to content

Commit

Permalink
feat(web): Release action
Browse files Browse the repository at this point in the history
  • Loading branch information
RayRedGoose committed Sep 21, 2023
1 parent 46c4043 commit 1fa4aa5
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"changelog": "./my-changelog-config.js",
"commit": false,
"fixed": [],
"linked": [],
Expand Down
10 changes: 10 additions & 0 deletions .changeset/my-changelog-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function getDependencyReleaseLine() {}

async function getReleaseLine(changeset) {
return changeset.summary;
}

module.exports = {
getReleaseLine,
getDependencyReleaseLine,
};
93 changes: 93 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Release

on:
push:
branches:
- main
workflow_dispatch: # Allow manual triggering of this job in case of failures
inputs:
version:
default: 'patch'
description:
'The version override. Example: "1.1.2". Leave blank if you want conventional commits to
decide which version'
required: false
package:
type: choice
description: The package name to release
options:
- canvas-tokens-web
- canvas-tokens-docs

jobs:
release:
# Only run if:
# - The commit message does not contain `[skip release]`
# - OR the workflow was manually triggered and has a `version` string
if: "(contains(github.event.head_commit.message, '):') && !contains(github.event.head_commit.message, '[skip release]')) || inputs.version"
runs-on: ubuntu-latest

steps:
## First, we'll checkout the repository. We don't persist credentials because we need a
## Personal Access Token to push on a branch that is protected. See
## https://github.com/cycjimmy/semantic-release-action#basic-usage
- uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0 # Used for conventional commit ranges

## This step installs node and sets up several matchers (regex matching for Github
## Annotations). See
## https://github.com/actions/setup-node/blob/25316bbc1f10ac9d8798711f44914b1cf3c4e954/src/main.ts#L58-L65
- uses: actions/setup-node@v3
with:
node-version: "18.x"
registry-url: https://registry.npmjs.org

## The caching steps create a cache key based on the OS and hash of the yarn.lock file. A
## cache hit will copy files from Github cache into the `node_modules` and `.cache/cypress`
## folders. A cache hit will skip the cache steps
- name: Cache node modules
id: npm-cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-18.x-node-modules-hash-${{ hashFiles('package-lock.json') }}

## If both `node_modules` and `.cache/cypress` were cache hits, we're going to skip the `yarn
## install` step. This effectively saves up to 3m on a cache hit build.
- name: Install Packages
if: steps.npm-cache.outputs.cache-hit != 'true'
run: npm install --production=false

- name: Get package name
id: package
run: echo "package=$(node scripts/utils/get-package-name.js)" >> $GITHUB_OUTPUT
env:
HEAD_COMMIT: ${{ github.event.head_commit.message }}
PACKAGE: ${{inputs.package}}

- name: Get previous tag
id: previous-tag
run: echo "tag=$(node -p 'require("./packages/${{ steps.package.outputs.package }}/package.json").version')" >> $GITHUB_OUTPUT

- name: Generate Changeset
uses: Workday/canvas-kit-actions/generate-changeset@v1
id: changeset
with:
token: ${{ secrets.GITHUB_TOKEN }}
fromRef: '@workday/${{steps.package.outputs.package}}@${{steps.previous-tag.outputs.tag}}'
toRef: 'main'
tagName: 'new'

- name: Update Changelog
run: node scripts/utils/update-changelog.js
env:
PACKAGE: ${{steps.package.outputs.package}}
VERSION: ${{inputs.version}}
CHANGESET_BODY: ${{steps.changeset.outputs.body}}

- name: Create Release Pull Request
uses: changesets/action@v1
env:
GITHUB_TOKEN: ${{ secrets.GH_RW_TOKEN }}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Add files here to ignore them from prettier formatting
/dist
*.yml
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions scripts/utils/get-package-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {PACKAGE, HEAD_COMMIT = ''} = process.env;
const regex = /\((.*)\):/;
const [_, packagePrefix] = HEAD_COMMIT.match(regex) || [];

if (!packagePrefix) {
throw Error('There is no package to release');
}

console.log(PACKAGE || `canvas-tokens-${packagePrefix}`);
28 changes: 28 additions & 0 deletions scripts/utils/update-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const fs = require('fs');

const {PACKAGE = '', CHANGESET_BODY = '', VERSION} = process.env;

console.log(process.env, VERSION);

const header = `---
'@workday/${PACKAGE}': ${VERSION || 'patch'}
---`;

const [prefix] = PACKAGE.split('-').reverse();

let changelogBody = CHANGESET_BODY.split('##').filter(
block => block.toLowerCase().startsWith(`# ${prefix}`) || block.startsWith('# All')
);

if (changelogBody) {
changelogBody = +'\n##' + changelogBody.join('##');
}

const changelogContents = `${header}\n\n${changelogBody}`;

fs.writeFileSync(
path.join(path.resolve('./'), './.changeset/pre-changelog.md'),
changelogContents,
'utf8'
);

0 comments on commit 1fa4aa5

Please sign in to comment.