From 0f2f37d4db67b87779fa06fe6351d7b4e510ecb7 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 6 Jul 2023 23:24:05 -0300 Subject: [PATCH] ci: Improve release automation (#29752) --- .changeset/config.json | 2 +- .changeset/nine-yaks-draw.md | 5 ++ .github/workflows/changesets.yml | 51 ------------------- .github/workflows/publish-release.yml | 3 +- package.json | 1 - .../release-action/src/bumpNextVersion.ts | 12 ++--- .../src/fixWorkspaceVersionsBeforePublish.ts | 4 -- packages/release-action/src/gitUtils.ts | 38 +++++++++++++- packages/release-action/src/publishRelease.ts | 17 +++++-- .../release-action/src/startPatchRelease.ts | 2 +- yarn.lock | 33 +----------- 11 files changed, 66 insertions(+), 102 deletions(-) create mode 100644 .changeset/nine-yaks-draw.md delete mode 100644 .github/workflows/changesets.yml diff --git a/.changeset/config.json b/.changeset/config.json index 5dfd3f70619..9d77693b3b3 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -1,6 +1,6 @@ { "$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json", - "changelog": ["@changesets/changelog-github", { "repo": "RocketChat/Rocket.Chat" }], + "changelog": "@changesets/changelog-git", "commit": false, "fixed": [ ["@rocket.chat/meteor", "@rocket.chat/core-typings", "@rocket.chat/rest-typings"] diff --git a/.changeset/nine-yaks-draw.md b/.changeset/nine-yaks-draw.md new file mode 100644 index 00000000000..bd12a98350f --- /dev/null +++ b/.changeset/nine-yaks-draw.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/release-action': minor +--- + +Use `release-automation` branch to perform the release diff --git a/.github/workflows/changesets.yml b/.github/workflows/changesets.yml deleted file mode 100644 index 56759167493..00000000000 --- a/.github/workflows/changesets.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Changesets - -on: - push: - branches: - - develop - -concurrency: ${{ github.workflow }}-${{ github.ref }} - -jobs: - release-versions: - name: ⚙️ Variables Setup - runs-on: ubuntu-latest - outputs: - node-version: ${{ steps.var.outputs.node-version }} - steps: - - uses: Bhacaz/checkout-files@v2 - with: - files: package.json - branch: ${{ github.ref }} - - - id: var - run: | - NODE_VERSION=$(node -p "require('./package.json').engines.node") - echo "NODE_VERSION: ${NODE_VERSION}" - echo "node-version=${NODE_VERSION}" >> $GITHUB_OUTPUT - - release: - name: Release - needs: [release-versions] - runs-on: ubuntu-latest - steps: - - name: Checkout Repo - uses: actions/checkout@v3 - - - name: Setup NodeJS - uses: ./.github/actions/setup-node - with: - node-version: ${{ needs.release-versions.outputs.node-version }} - cache-modules: true - install: true - - - uses: dtinth/setup-github-actions-caching-for-turbo@v1 - - - name: Create Release Pull Request - uses: changesets/action@v1 - with: - title: 'chore: Bump packages' - env: - HUSKY: 0 - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index cb85da9fad6..ba66adc5d29 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -3,7 +3,7 @@ name: Publish Final Release on: push: branches: - - master + - release-automation concurrency: ${{ github.workflow }}-${{ github.ref }} @@ -18,6 +18,7 @@ jobs: - name: Checkout Repo uses: actions/checkout@v3 with: + fetch-depth: 0 token: ${{ secrets.CI_PAT }} - name: Setup NodeJS diff --git a/package.json b/package.json index 7a79038a3ac..a47523eefce 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "fuselage": "./fuselage.sh" }, "devDependencies": { - "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.1", "@types/chart.js": "^2.9.37", "@types/js-yaml": "^4.0.5", diff --git a/packages/release-action/src/bumpNextVersion.ts b/packages/release-action/src/bumpNextVersion.ts index cc977900b21..999fa3d7b49 100644 --- a/packages/release-action/src/bumpNextVersion.ts +++ b/packages/release-action/src/bumpNextVersion.ts @@ -9,6 +9,7 @@ import { setupOctokit } from './setupOctokit'; import { createNpmFile } from './createNpmFile'; import { getChangelogEntry, bumpFileVersions, readPackageJson } from './utils'; import { fixWorkspaceVersionsBeforePublish } from './fixWorkspaceVersionsBeforePublish'; +import { commitChanges, createBranch, createTag, pushNewBranch } from './gitUtils'; export async function bumpNextVersion({ githubToken, @@ -59,26 +60,25 @@ export async function bumpNextVersion({ await bumpFileVersions(cwd, currentVersion, newVersion); // TODO check if branch exists - await exec('git', ['checkout', '-b', newBranch]); + await createBranch(newBranch); - await exec('git', ['add', '.']); - await exec('git', ['commit', '-m', newVersion]); + await commitChanges(`Release ${newVersion}`); core.info('fix dependencies in workspace packages'); await fixWorkspaceVersionsBeforePublish(); await exec('yarn', ['changeset', 'publish', '--no-git-tag']); - await exec('git', ['tag', newVersion]); + await createTag(newVersion); - await exec('git', ['push', '--force', '--follow-tags', 'origin', `HEAD:refs/heads/${newBranch}`]); + await pushNewBranch(newBranch); if (newVersion.includes('rc.0')) { const finalPrTitle = `Release ${finalVersion}`; core.info('creating pull request'); await octokit.rest.pulls.create({ - base: 'master', + base: 'release-automation', head: newBranch, title: finalPrTitle, body: prBody, diff --git a/packages/release-action/src/fixWorkspaceVersionsBeforePublish.ts b/packages/release-action/src/fixWorkspaceVersionsBeforePublish.ts index 1a5780996fe..3a45e4fb879 100644 --- a/packages/release-action/src/fixWorkspaceVersionsBeforePublish.ts +++ b/packages/release-action/src/fixWorkspaceVersionsBeforePublish.ts @@ -41,10 +41,6 @@ export async function fixWorkspaceVersionsBeforePublish() { for (const dependency of dependencies) { const dependencyVersion = packageJson[dependencyType][dependency]; if (dependencyVersion.startsWith('workspace:')) { - if (!dependencyVersion.startsWith('workspace:^')) { - throw new Error(`Unsupported workspace version range: ${dependencyVersion}`); - } - const realVersion = workspaceVersions.get(dependency); if (!realVersion) { throw new Error(`Could not find version for workspace ${dependency}`); diff --git a/packages/release-action/src/gitUtils.ts b/packages/release-action/src/gitUtils.ts index 0550841f281..808f6d8885f 100644 --- a/packages/release-action/src/gitUtils.ts +++ b/packages/release-action/src/gitUtils.ts @@ -1,6 +1,42 @@ -import { exec } from '@actions/exec'; +import { exec, getExecOutput } from '@actions/exec'; export async function setupGitUser() { await exec('git', ['config', 'user.name', '"rocketchat-github-ci"']); await exec('git', ['config', 'user.email', '"buildmaster@rocket.chat"']); } + +export async function createBranch(newBranch: string) { + await exec('git', ['checkout', '-b', newBranch]); +} + +export async function checkoutBranch(branchName: string) { + await exec('git', ['checkout', branchName]); +} + +export async function mergeBranch(branchName: string) { + await exec('git', ['merge', '--no-edit', branchName]); +} + +export async function commitChanges(commitMessage: string) { + await exec('git', ['add', '.']); + await exec('git', ['commit', '-m', commitMessage]); +} + +export async function createTag(version: string) { + // create an annotated tag so git push --follow-tags will push the tag + await exec('git', ['tag', version, '-m', version]); +} + +export async function getCurrentBranch() { + const { stdout: branchName } = await getExecOutput('git', ['rev-parse', '--abbrev-ref', 'HEAD']); + + return branchName.trim(); +} + +export async function pushChanges() { + await exec('git', ['push', '--follow-tags']); +} + +export async function pushNewBranch(newBranch: string) { + await exec('git', ['push', '--force', '--follow-tags', 'origin', `HEAD:refs/heads/${newBranch}`]); +} diff --git a/packages/release-action/src/publishRelease.ts b/packages/release-action/src/publishRelease.ts index a776c9d74d2..e9d76d19a75 100644 --- a/packages/release-action/src/publishRelease.ts +++ b/packages/release-action/src/publishRelease.ts @@ -9,6 +9,7 @@ import { createNpmFile } from './createNpmFile'; import { setupOctokit } from './setupOctokit'; import { bumpFileVersions, getChangelogEntry, readPackageJson } from './utils'; import { fixWorkspaceVersionsBeforePublish } from './fixWorkspaceVersionsBeforePublish'; +import { checkoutBranch, commitChanges, createTag, getCurrentBranch, mergeBranch, pushChanges } from './gitUtils'; export async function publishRelease({ githubToken, @@ -29,7 +30,7 @@ export async function publishRelease({ await createNpmFile(); if (baseRef) { - await exec('git', ['checkout', baseRef]); + await checkoutBranch(baseRef); } const { version: currentVersion } = await readPackageJson(cwd); @@ -73,17 +74,23 @@ export async function publishRelease({ core.info('update version in all files to new'); await bumpFileVersions(cwd, currentVersion, newVersion); - await exec('git', ['add', '.']); - await exec('git', ['commit', '-m', `Release ${newVersion}`]); + await commitChanges(`Release ${newVersion}`); + + // get current branch name + const branchName = await getCurrentBranch(); + + // merge release changes to master + await checkoutBranch('master'); + await mergeBranch(branchName); core.info('fix dependencies in workspace packages'); await fixWorkspaceVersionsBeforePublish(); await exec('yarn', ['changeset', 'publish', '--no-git-tag']); - await exec('git', ['tag', newVersion]); + await createTag(newVersion); - await exec('git', ['push', '--follow-tags']); + await pushChanges(); core.info('create release'); await octokit.rest.repos.createRelease({ diff --git a/packages/release-action/src/startPatchRelease.ts b/packages/release-action/src/startPatchRelease.ts index 05681604e47..a499b223608 100644 --- a/packages/release-action/src/startPatchRelease.ts +++ b/packages/release-action/src/startPatchRelease.ts @@ -48,7 +48,7 @@ export async function startPatchRelease({ core.info('creating pull request'); await octokit.rest.pulls.create({ - base: 'master', + base: 'release-automation', head: newBranch, title: finalPrTitle, body: '', diff --git a/yarn.lock b/yarn.lock index 7bc670d2dd2..df384064d53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4165,17 +4165,6 @@ __metadata: languageName: node linkType: hard -"@changesets/changelog-github@npm:^0.4.8": - version: 0.4.8 - resolution: "@changesets/changelog-github@npm:0.4.8" - dependencies: - "@changesets/get-github-info": ^0.5.2 - "@changesets/types": ^5.2.1 - dotenv: ^8.1.0 - checksum: 8a357cc08757e0eeca267ee05141f68bef936582abef8b78a5d30d99f5a86e41b7d3debba70992b73b2f57b0fc6201ec1cc3c65116930167ee3197b427b865c5 - languageName: node - linkType: hard - "@changesets/cli@npm:^2.26.1": version: 2.26.1 resolution: "@changesets/cli@npm:2.26.1" @@ -4256,16 +4245,6 @@ __metadata: languageName: node linkType: hard -"@changesets/get-github-info@npm:^0.5.2": - version: 0.5.2 - resolution: "@changesets/get-github-info@npm:0.5.2" - dependencies: - dataloader: ^1.4.0 - node-fetch: ^2.5.0 - checksum: 067e07eeaecdbedbd1c715513c4aa6206a941bd1d3af292d067792808c6fa6644caad2b35fba614a44892559c031c234df8028f8d2abd4cb2682d48080ef5df3 - languageName: node - linkType: hard - "@changesets/get-release-plan@npm:^3.0.16": version: 3.0.16 resolution: "@changesets/get-release-plan@npm:3.0.16" @@ -20149,13 +20128,6 @@ __metadata: languageName: node linkType: hard -"dataloader@npm:^1.4.0": - version: 1.4.0 - resolution: "dataloader@npm:1.4.0" - checksum: e2c93d43afde68980efc0cd9ff48e9851116e27a9687f863e02b56d46f7e7868cc762cd6dcbaf4197e1ca850a03651510c165c2ae24b8e9843fd894002ad0e20 - languageName: node - linkType: hard - "date-fns@npm:^2.15.0, date-fns@npm:^2.28.0": version: 2.28.0 resolution: "date-fns@npm:2.28.0" @@ -21058,7 +21030,7 @@ __metadata: languageName: node linkType: hard -"dotenv@npm:^8.0.0, dotenv@npm:^8.1.0": +"dotenv@npm:^8.0.0": version: 8.6.0 resolution: "dotenv@npm:8.6.0" checksum: 38e902c80b0666ab59e9310a3d24ed237029a7ce34d976796349765ac96b8d769f6df19090f1f471b77a25ca391971efde8a1ea63bb83111bd8bec8e5cc9b2cd @@ -30812,7 +30784,7 @@ __metadata: languageName: node linkType: hard -"node-fetch@npm:^2.5.0, node-fetch@npm:^2.6.11": +"node-fetch@npm:^2.6.11": version: 2.6.11 resolution: "node-fetch@npm:2.6.11" dependencies: @@ -35958,7 +35930,6 @@ __metadata: version: 0.0.0-use.local resolution: "rocket.chat@workspace:." dependencies: - "@changesets/changelog-github": ^0.4.8 "@changesets/cli": ^2.26.1 "@types/chart.js": ^2.9.37 "@types/js-yaml": ^4.0.5