Skip to content

Commit

Permalink
ci: add publish workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
jeangovil committed Dec 5, 2023
1 parent af3bb73 commit a7695e9
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/canary-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Canary Release
on:
pull_request:

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
publish-canary:
if: startsWith(github.head_ref, 'topic-')
runs-on: ubuntu-latest
steps:
- name: Get branch name
shell: bash
run: echo "name=$(echo $GITHUB_HEAD_REF | sed 's/refs\/heads\///' | tr '/' '-')" >> $GITHUB_OUTPUT
id: branch_name

- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: 1
ref: ${{ github.event.pull_request.head.sha }}

- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'

- name: Install dependencies
run: |
yarn install --frozen-lockfile
yarn lerna link
- name: Publish
id: publish
run: |
echo ".npmrc" >> .git/info/exclude
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
mkdir ${{ runner.temp }}/${{ steps.branch_name.outputs.name }}
yarn lerna publish prerelease --canary --no-private --no-verify-access --yes --exact --dist-tag=${{ steps.branch_name.outputs.name }} --preid=${{ steps.branch_name.outputs.name }} --summary-file=${{ runner.temp }}/${{ steps.branch_name.outputs.name }}
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs');
const summary = JSON.parse(fs.readFileSync('${{ runner.temp }}/${{ steps.branch_name.outputs.name }}/lerna-publish-summary.json', 'utf8'));
const packages = summary.map(pkg => `**${pkg.packageName}@${pkg.version}**\n\`\`\`bash\nyarn add ${pkg.packageName}@${{ steps.branch_name.outputs.name }}\n\`\`\``).join('\n');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🎉 Canary Release!\n\n${packages}`
})
131 changes: 131 additions & 0 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: Prerelease
on:
push:
branches:
- 'main'

jobs:
prerelease:
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true

if: github.actor_id != vars.CI_BOT_ID && !contains(github.event.head_commit.message, format('chore{0} release', ':'))
runs-on: ubuntu-latest
steps:
- id: create_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.CI_APP_ID }}
private_key: ${{ secrets.CI_APP_PRIVATE_KEY }}
permissions: >-
{ "contents": "write", "statuses": "write" }
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: 1
token: ${{ steps.create_token.outputs.token }}

- name: Extract version from lerna.json
run: echo "value=$(jq .version lerna.json -r)" >> $GITHUB_OUTPUT
id: current_version

- name: Determine Prerelease Preid
shell: bash
run: echo "value=$([[ ${{ steps.current_version.outputs.value }} =~ beta ]] && echo "beta" || echo "alpha")" >> $GITHUB_OUTPUT
id: preid

- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'

- name: Install dependencies
run: |
yarn install --frozen-lockfile
yarn lerna link
- name: Check for changed packages
id: changes
run: |
error_filename="${{ runner.temp }}/changes_errors.log";
changes=$(echo $(yarn -s lerna changed --json --loglevel 2>>$error_filename) 2>>$error_filename);
cat $error_filename;
count=$([[ -n $changes ]] && echo $(echo $changes | jq '. | map(select(.private == false)) | length')) || echo 0
changed=$([[ $changes > 0 ]] && echo "true" || echo "false")
echo "changes=$count" >> $GITHUB_OUTPUT
echo "changed=$changed" >> $GITHUB_OUTPUT
echo "changes: $changes - changed: $changed"
- name: Build packages
run: yarn build

- name: Publish
id: publish
if: ${{ steps.changes.outputs.changed == 'true' }}
run: |
git config --global user.name '${{ vars.CI_APP_NAME }}[bot]'
git config --global user.email '${{ vars.CI_BOT_ID }}+${{ vars.CI_APP_NAME }}[bot]@users.noreply.github.com'
echo ".npmrc" >> .git/info/exclude
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
yarn lerna version prerelease --no-private --conventional-commits --conventional-prerelease --preid=${{ steps.preid.outputs.value }} --yes
yarn lerna publish from-git --summary-file=${{ runner.temp }} --yes
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}

# - name: Run release-pr.yaml workflow
# if: ${{ steps.changes.outputs.changed == 'true' }}
# run: |
# tag=$(git describe --tags $(git rev-list --tags --max-count=1))
# echo "${{ runner.temp }}/lerna-publish-summary.json";
# if [[ -f "${{ runner.temp }}/lerna-publish-summary.json" ]]; then
# gh workflow run release-pr.yml --ref $tag;
# fi;
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get release commit
if: ${{ steps.changes.outputs.changed == 'true' }}
shell: bash
run: echo "sha=$(git rev-parse --verify HEAD)" >> $GITHUB_OUTPUT
id: release_commit

- uses: actions/github-script@v6
if: ${{ steps.changes.outputs.changed == 'true' }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require("fs/promises");
let summary = null;
try {
summary = await fs.readFile(
"${{ runner.temp }}/lerna-publish-summary.json",
"utf8"
);
} catch {
console.log("No new releases!");
}
if (!summary) return;
summary = JSON.parse(summary);
const packages = summary
.map(
(pkg) =>
`**${pkg.packageName}@${pkg.version}**\n\`\`\`bash\nyarn add ${pkg.packageName}@${pkg.version}\n\`\`\``
)
.join("\n");
const preid = "${{ steps.preid.outputs.value }}";
github.rest.repos.createCommitComment({
commit_sha: "${{ steps.release_commit.outputs.sha }}",
owner: context.repo.owner,
repo: context.repo.repo,
body: `🎉 ${preid === 'alpha' ? 'Alpha' : 'Beta'} Release!\n\n${packages}`,
});
83 changes: 83 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Publish
on:
pull_request:
branches:
- 'main'
types:
- closed

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
publish:
if: ${{ (github.head_ref == 'release-stable' || github.head_ref == 'release-beta') && github.event.pull_request.merged == true }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: 1

- name: Extract version from lerna.json
run: echo "value=$(jq .version lerna.json -r)" >> $GITHUB_OUTPUT
id: current_version

- name: Determine Release type
shell: bash
run: echo "value=$([[ ${{ steps.current_version.outputs.value }} =~ beta ]] && echo "beta" || echo "stable")" >> $GITHUB_OUTPUT
id: release_type

- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'

- name: Install dependencies
run: |
yarn install --frozen-lockfile
yarn lerna link
- name: Bump versions
id: bump
run: |
git config --global user.name 'github-actions';
git config --global user.email '[email protected]';
echo ".npmrc" >> .git/info/exclude
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc;
yarn lerna publish from-package --summary-file=${{ runner.temp }}/ --yes
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Get release commit
shell: bash
run: |
COMMIT_HASH=$(git rev-parse --verify HEAD);
echo "sha=$COMMIT_HASH" >> $GITHUB_OUTPUT
id: release_commit

- uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const releaseType = "${{ steps.release_type.outputs.value }}";
const summary = JSON.parse(fs.readFileSync('${{ runner.temp }}/lerna-publish-summary.json', 'utf8'));
const packages = summary.map(pkg => `**${pkg.packageName}@${pkg.version}**\n\`\`\`bash\nyarn add ${pkg.packageName}@${{ steps.branch_name.outputs.name }}\n\`\`\``).join('\n');
const body = `🎉 ${releaseType === 'stable' ? "Stable" : "Beta"} Release!\n\n${packages}`
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
github.rest.repos.createCommitComment({
commit_sha: "${{ steps.release_commit.outputs.sha }}",
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
Loading

0 comments on commit a7695e9

Please sign in to comment.