Skip to content

Commit

Permalink
fix: add github action access for version packages
Browse files Browse the repository at this point in the history
  • Loading branch information
SarjuHansaliya committed Sep 14, 2023
1 parent 6b5fceb commit 3406993
Show file tree
Hide file tree
Showing 25 changed files with 545 additions and 9 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/monorepo-create-or-update-dev-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Create or Update dev->main PR (Monorepo)

on:
pull_request:
types:
- closed
branches:
- dev
paths:
- 'monorepo/**'

defaults:
run:
working-directory: ./monorepo

jobs:
create-or-update-pr:
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GIT_ACCESS_TOKEN }}

- run: git config user.name "github-actions[bot]"
- run: git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org/'

- name: Install dependencies
run: |
npm install @octokit/rest ts-node typescript @types/node
- name: Run TypeScript script
run: npx ts-node ./scripts/test.ts
env:
GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
184 changes: 184 additions & 0 deletions .github/workflows/monorepo-pre-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
name: Pre-Release (Monorepo)

on:
issue_comment:
types:
- created

env:
GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}

defaults:
run:
working-directory: ./monorepo

jobs:
check_validity:
name: Should Run ?
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
should_run: ${{ steps.should_run_action.outputs.result }}
steps:
- name: Validate pull request
uses: actions/github-script@v6
id: should_run_action
env:
GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
with:
script: |
// only run action if
// 1. comment is = "/pre-release"
// 2. is made on dev -> main pull request
const eventData = context.payload;
if (eventData.issue.pull_request && eventData.comment.body === "/pre-release") {
try {
const { data } = await github.rest.pulls.get({
owner: eventData.repository.owner.login,
repo: eventData.repository.name,
pull_number: eventData.issue.number,
});
const baseBranch = data.base.ref;
const headBranch = data.head.ref;
return baseBranch === "main" && headBranch === "dev";
} catch (err) {
core.setFailed(`Request failed with error ${err}`);
}
}
pre-release:
name: Pre Release
needs: check_validity
if: ${{ needs.check_validity.outputs.should_run == 'true' }}
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:

- name: Enforce permission requirement
uses: prince-chrismc/check-actor-permissions-action@v1
with:
permission: write

- name: Add initial reaction
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ github.event.comment.id }}
reactions: eyes

- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GIT_ACCESS_TOKEN }}

- run: git config user.name "github-actions[bot]"
- run: git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org/'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Creating .npmrc
run: |
cat << EOF > "$HOME/.npmrc"
//registry.npmjs.org/:_authToken=$NPM_TOKEN
EOF
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Install Dependencies
run: yarn

- name: enter into pre release mode
run: yarn changeset pre enter rc

- name: do yarn changeset version
run: yarn changeset version

- name: exit from pre release mode
run: yarn changeset pre exit

- run: git add .

- name: Commit changes & push
run: |
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "[skip ci] pre-release version"
git push
fi
# this step is needed when we do actual npm publish
# - name: do yarn release
# run: yarn release
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: check running directory3
run: pwd

- name: do yarn release
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
with:
script: |
const childProcesses = require("child_process");
const util = require("util");
const process = require("process");
const execAsync = util.promisify(childProcesses.exec);
const eventData = context.payload;
process.chdir("./monorepo");
try {
// Asynchronous execution with await and execAsync
const { stdout } = await execAsync("yarn release");
const newTags = Array.from(stdout.matchAll(/New tag:\s+([^\s\n]+)/g)).map(
([_, tag]) => tag
);
if (newTags.length) {
const multiple = newTags.length > 1;
const body =
`🫰✨ **Thanks @${context.actor}! ` +
`Your pre-release ${
multiple ? "s have" : " has"
} been published to npm.**\n\n` +
`Test the pre-release${
multiple ? "s" : ""
} by updating your \`package.json\` ` +
`with the newly published version${multiple ? "s" : ""}:\n` +
newTags.map((tag) => "```sh\n" + `yarn add ${tag}\n` + "```").join("\n");
await github.rest.issues.createComment({
issue_number: eventData.issue.number,
owner: eventData.repository.owner.login,
repo: eventData.repository.name,
body,
});
}
} catch (error) {
console.error(error);
}
- name: Add final reaction
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ github.event.comment.id }}
reactions: rocket
45 changes: 45 additions & 0 deletions .github/workflows/monorepo-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Release (Monorepo)

on:
push:
branches:
- main
paths:
- 'monorepo/**'

concurrency: ${{ github.workflow }}-${{ github.ref }}

defaults:
run:
working-directory: ./monorepo

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GIT_ACCESS_TOKEN }}

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org/'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Install Dependencies
run: yarn

- name: Create Release Pull Request or Publish to npm
uses: changesets/action@v1
with:
publish: yarn release
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
Loading

0 comments on commit 3406993

Please sign in to comment.