From 1e3550b525112f66f57c770a7f45ea5b0421e56d Mon Sep 17 00:00:00 2001 From: Marc-Antoine Fernandes Date: Wed, 17 Apr 2024 22:34:54 +0200 Subject: [PATCH] fix: Fix release process --- .github/workflows/release.yml | 27 +++---------- scripts/release-from-ci.js | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 scripts/release-from-ci.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 617850c..ae20865 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,27 +27,12 @@ jobs: - name: Yarn install run: yarn - - name: Apply version to packages - id: version-apply - run: yarn version apply -all ${{ inputs.prerelease && '--prerelease' }} - - - name: Compute commit message + - name: Publish changed packages and create release uses: actions/github-script@v7 - id: commit-message - with: - result-encoding: string - script: | - return `chore: release ${versions.map((v) => `${v.ident}@${v.newVersion}`)}`; - - # Commiting using the GitHub action bot - # More info here: https://github.com/actions/checkout/issues/13#issuecomment-724415212 - - name: Create commit of changed files - run: | - git add . - git commit -m "${{ steps.commit-message.outputs.result }}" --author="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" - git push - - - name: Publish changed packages - run: yarn workspaces foreach -pt --since ${{ github.ref }} npm publish env: YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} + PRERELEASE: ${{ inputs.prerelease }} + with: + script: | + const script = require('./scripts/release-from-ci.js') + await script({ github, context, exec }); diff --git a/scripts/release-from-ci.js b/scripts/release-from-ci.js new file mode 100644 index 0000000..91f4334 --- /dev/null +++ b/scripts/release-from-ci.js @@ -0,0 +1,75 @@ +module.exports = async ({ github, context, exec }) => { + const isPrerelease = process.env.PRERELEASE === 'true'; + + console.log('Step 1. Apply version changes to all packages'); + const versionApplyArgs = ['version', 'apply', '--all', '--json']; + if (isPrerelease) { + versionApplyArgs.push('--prerelease'); + } + const { stdout: ndjson } = await exec.getExecOutput('yarn', versionApplyArgs); + const versions = ndjson + .split('\n') + .filter(Boolean) + .map((line) => JSON.parse(line)); + console.log('Releasing versions:', versions); + + const versionStrings = versions.map((v) => `${v.ident}@${v.newVersion}`); + + console.log('Step 2. Create a release commit'); + + // Commiting using the GitHub action bot + // More info here: https://github.com/actions/checkout/issues/13#issuecomment-724415212 + await exec.exec('git', [ + 'config', + '--global', + 'user.email', + '41898282+github-actions[bot]@users.noreply.github.com', + ]); + await exec.exec('git', [ + 'config', + '--global', + 'user.name', + 'github-actions[bot]', + ]); + + await exec.exec('git', ['add', '.']); + await exec.exec('git', [ + 'commit', + '-m', + `chore: release ${versionStrings.join(', ')}`, + ]); + await exec.exec('git', [ + 'push', + 'origin', + process.env.GITHUB_REF_NAME || `main`, + ]); + + console.log('Step 3. Publish changed packages'); + await exec.exec('yarn', [ + 'workspaces', + 'foreach', + '-pt', + '--since', + context.ref, // Check diff of the release commit + 'npm', + 'publish', + ]); + + console.log('Step 4. Create release'); + const { stdout: targetCommitish } = await exec.getExecOutput('git', [ + 'rev-parse', + 'HEAD', + ]); + console.log(`Using commit ${targetCommitish} as release ref`); + + const release = await github.rest.repos.createRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + tag_name: `${versions[0].ident}@${versions[0].newVersion}`, + target_commitish: targetCommitish, + name: versionStrings.join(', '), + prereleasee: isPrerelease, + generate_release_notes: true, + }); + console.log('Release created:', release); +};