Skip to content

Commit

Permalink
chore: automate releases with pr (#3089)
Browse files Browse the repository at this point in the history
* chore: automate releases with pr

Closes #3042

* factor js out of workflows

* remove release PR from release notes

* delete release branch after release
  • Loading branch information
mweberxyz committed Apr 12, 2024
1 parent ca1dbb8 commit d78ca7a
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 26 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/publish-undici-types.yml

This file was deleted.

57 changes: 57 additions & 0 deletions .github/workflows/release-create-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Create release PR

permissions:
contents: read

on:
workflow_dispatch:
inputs:
version:
description: 'The version number to release (has priority over release_type)'
type: string
release_type:
description: Type of release
type: choice
default: patch
options:
- patch
- minor
- major

jobs:
create-pr:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

outputs:
version: ${{ steps.bump.outputs.version }}

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- name: Git Config
run: |
git config --global user.email "github-actions@github.com"
git config --global user.name "github-actions"
- name: Change version number and push
id: bump
run: |
npm version ${{ inputs.version || inputs.release_type }} --git-tag-version=false
VERSION=`jq -r ".version" package.json`
RELEASE_BRANCH="release/v$VERSION"
git add -u
git commit -m "Bumped v$VERSION"
git push origin "HEAD:$RELEASE_BRANCH"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Create PR
uses: actions/github-script@v7
with:
script: |
const defaultBranch = "${{ github.event.repository.default_branch }}"
const versionTag = "v${{ steps.bump.outputs.version }}"
await require('./scripts/release').generatePr({ github, context, defaultBranch, versionTag })
72 changes: 72 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Create release

on:
push:
branches:
- main
paths:
- package.json

permissions:
contents: read

jobs:
check-release-version:
runs-on: ubuntu-latest
outputs:
release-version: ${{ steps.set-release-version.outputs.result }}
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
id: set-release-version
with:
result-encoding: string
script: |
const { owner, repo } = context.repo
const version = require("./package.json").version
const versionTag = `v${version}`
const { data: releases } = await github.rest.repos.listReleases({
owner,
repo
})
if (versionTag !== releases[0]?.tag_name) {
return versionTag
}
release:
runs-on: ubuntu-latest
needs: check-release-version
if: ${{ startsWith(needs.check-release-version.outputs.release-version, 'v') }}

permissions:
contents: write
id-token: write

environment: release

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
- run: npm install -g npm@latest
- run: npm install
- name: Create NPM release
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: node scripts/generate-undici-types-package-json.js
- run: npm publish --provenance
working-directory: './types'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub release
uses: actions/github-script@v7
with:
script: |
const defaultBranch = "${{ github.event.repository.default_branch }}"
const versionTag = "${{ needs.check-release-version.outputs.release-version }}"
await require('./scripts/release').release({ github, context, defaultBranch, versionTag })
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [Lint](#lint)
* [Test](#test)
* [Coverage](#coverage)
* [Releases](#releases)
* [Update `WPTs`](#update-wpts)
* [Building for externally shared node builtins](#external-builds)
* [Developer's Certificate of Origin 1.1](#developers-certificate-of-origin)
Expand Down Expand Up @@ -166,6 +167,12 @@ npm run test
npm run coverage
```

<a id="releases"></a>
### Issuing Releases

Release is automatic on commit to main which bumps the package.json version field.
Use the "Create release PR" github action to generate a release PR.

<a id="external-builds"></a>
### Building for externally shared node builtins

Expand Down
71 changes: 71 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

// Called from .github/workflows

const generateReleaseNotes = async ({ github, owner, repo, versionTag, defaultBranch }) => {
const { data: releases } = await github.rest.repos.listReleases({
owner,
repo
})

const { data: { body } } = await github.rest.repos.generateReleaseNotes({
owner,
repo,
tag_name: versionTag,
target_commitish: defaultBranch,
previous_tag_name: releases[0]?.tag_name
})

const bodyWithoutReleasePr = body.split('\n')
.filter((line) => !line.includes('[Release] v'))
.join('\n')

return bodyWithoutReleasePr
}

const generatePr = async ({ github, context, defaultBranch, versionTag }) => {
const { owner, repo } = context.repo
const releaseNotes = await generateReleaseNotes({ github, owner, repo, versionTag, defaultBranch })

await github.rest.pulls.create({
owner,
repo,
head: `release/${versionTag}`,
base: defaultBranch,
title: `[Release] ${versionTag}`,
body: releaseNotes
})
}

const release = async ({ github, context, defaultBranch, versionTag }) => {
const { owner, repo } = context.repo
const releaseNotes = await generateReleaseNotes({ github, owner, repo, versionTag, defaultBranch })

await github.rest.repos.createRelease({
owner,
repo,
tag_name: versionTag,
target_commitish: defaultBranch,
name: versionTag,
body: releaseNotes,
draft: false,
prerelease: false,
generate_release_notes: false
})

try {
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/release/${versionTag}`
})
} catch (err) {
console.log("Couldn't delete release PR ref")
console.log(err)
}
}

module.exports = {
generatePr,
release
}

0 comments on commit d78ca7a

Please sign in to comment.