Skip to content

refactor(gh actions): add a step to reset working directory before le… #478

refactor(gh actions): add a step to reset working directory before le…

refactor(gh actions): add a step to reset working directory before le… #478

Workflow file for this run

name: Release
# triggers on pushes to the 'prod' branch
on:
push:
# NOTE(thuang): Since REGEX is not supported for branches, we cannot target
# any versioned branch: "+([0-9])?(.{+([0-9]),x}).x" as specified in
# .releaserc.js. So we'll need to manually add such branch's name here manually
branches:
- prod
jobs:
# Job 1
release:
name: Release
runs-on: ubuntu-latest # Use the latest version of the Ubuntu runner environment
steps:
# Step 1: Check out the repository code
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
# Step 2: Set up Node.js environment and cache Yarn dependencies
- name: Setup Node.js and Cache yarn
uses: actions/setup-node@v3
with:
node-version-file: ".node-version" # Specify the Node.js version from '.node-version' file
cache: "yarn" # Cache Yarn dependencies for faster builds
# Step 3: Set up npm and authentication
- name: "Setup npm"
run: |
npm set "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Step 4: Ensure npm access
- name: Ensure NPM access
run: npm whoami
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Step 5: Install project dependencies using Yarn
- name: Install dependencies
run: yarn
# Step 6: Build the component library in 'dist/' directory
- name: Build component library in dist/
run: yarn build
# Step 7: Configure git user for pushing release changes back to prod branch
- name: Config git user
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
# Step 8: Reset working directory
- name: Reset working directory
run: git checkout -- .
# Step 9: Lerna Version
- name: Lerna Version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: yarn version:ci
# Step 10: Commit changes
- name: Commit changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "chore(release): Publish"
branch: ${{ github.head_ref }}
# Step 11: Publish NPM packages
- name: Lerna Publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: yarn publish:ci
# Step 12: Post breaking changes to a Slack channel
- name: Post breaking changes to a Slack channel
env:
SLACK_WEBHOOK_URL: ${{ secrets.SDS_BREAKING_CHANGES_SLACK_WEBHOOK }}
uses: act10ns/slack@v1
with:
status: complete
channel: "#sci-design-system-breaking-changes"
config: .github/config/slack.yml
if: contains(toJson(github.event.commits.*.message), 'BREAKING CHANGE')
# Step 13: Merge prod branch into main
- name: Create PR to merge prod into main
id: createpr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: actions/github-script@v6
with:
script: |
const ownerAndRepo = '${{ github.repository }}';
const [owner, repo] = ownerAndRepo.split('/');
const head = "prod";
const base = "main";
const title = "chore(release): Merge prod into main";
const pr = await github.rest.pulls.list({owner, repo, base, head: `${owner}:${head}`})
if (pr.data.length>0) {
return pr.data[0].number;
}
const result = await github.rest.pulls.create({
owner,
repo,
head,
base,
title,
});
return result.data.number;
# Steps 13 - 15 are temporarily disable until we find a solution
# to the issue of GitHub Actions not being able to merge PRs
# Step 13: Automerge PR
# - name: Enable Automerge PR
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# uses: peter-evans/enable-pull-request-automerge@v3
# with:
# pull-request-number: ${{steps.createpr.outputs.result}}
# merge-method: squash
# Step 14: Generate token for autoapproving PR
# - name: Generate token
# id: generate_token
# uses: chanzuckerberg/[email protected]
# with:
# app_id: ${{ secrets.CZI_GITHUB_HELPER_APP_ID }}
# private_key: ${{ secrets.CZI_GITHUB_HELPER_PK }}
# Step 15: Autoapprove PR
# - name: Autoapprove PR
# run: gh pr review --approve "${{steps.createpr.outputs.result}}"
# env:
# GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
# Job 2
create-version-matrix:
needs: release
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
# Step 1: Check out the repository code
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: prod
# Step 2: Create a matrix of package versions and names
- name: Create Versions Matrix
id: set-matrix
run: |
matrix="["
for package_json in $(find ./packages -name package.json); do
version=$(jq -r .version "$package_json")
name=$(jq -r .name "$package_json")
matrix="${matrix}{\"name\":\"${name}\", \"version\":\"${version}\"},"
done
matrix="${matrix%,}" # Remove trailing comma
matrix="${matrix}]" # Close the JSON array
echo "matrix=${matrix}" >> $GITHUB_OUTPUT
# Job 3
create-github-release:
needs: create-version-matrix
runs-on: ubuntu-latest
strategy:
matrix:
cfg: ${{fromJson(needs.create-version-matrix.outputs.matrix)}}
steps:
# Step 1: Create a GitHub release for the latest version
- name: Release
uses: softprops/action-gh-release@v1
with:
tag_name: "${{ matrix.cfg.name }}@${{ matrix.cfg.version }}"