Skip to content

Commit

Permalink
Combine reusable workflows into the main action (#61)
Browse files Browse the repository at this point in the history
* Combined jobs

* title

* inputs

* secrets

* clone after use

* merged

* removing reusable workflows

* run from develop branch

* integration test from the PR's branch

* only merged PR's

* Enable auto-merge option

* bash ofc

* run summary

* oh my bash
  • Loading branch information
michaelpaul authored Dec 5, 2023
1 parent ed7007a commit a38c899
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# `index.js` is the code that will run.
# For our project, we generate this file through a build process from other source files.
# We need to make sure the checked-in `index.js` actually matches what we expect it to be.
name: Check dist/
name: Check dist directory

on:
push:
Expand Down
20 changes: 0 additions & 20 deletions .github/workflows/github-release.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
name: Prepare release
name: Release management

on:
workflow_dispatch:
inputs:
pre-release:
required: false
type: boolean
default: false
description: "This release will be labeled as non-production ready"
pull_request:
types:
- closed
- labeled
- unlabeled
branches:
- main

jobs:
candidate:
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged && !startsWith(github.head_ref, 'promote/'))
release:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Preparing the next main release
uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
develop-branch: main
version-files: package.json
version-files: package.json
pre-release: ${{ inputs.pre-release || false }}
release-title: Release Automation Action
49 changes: 0 additions & 49 deletions .github/workflows/reusable-github-release.yml

This file was deleted.

7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'build-test'
name: 'Build and Integration Test'

on:
pull_request:
Expand Down Expand Up @@ -26,4 +26,7 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
pre-release: true
develop-branch: beta
version-files: README.md package.json
version-files: README.md package.json
release-title: Release Automation Action Beta
separator: '.beta.test'
enable-auto-merge: 0
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

A GitHub action to propose the next version to release of your repository.

## Prerequisites

* Branch protection rules (in Branch Settings)
- To prevent auto-merging releases into your `develop-branch` without reviews (e.g. 2 approvals)
* Allow auto-merge (in General Settings)
- If you want to release right after merging the PR, otherwise use the `enable-auto-merge` option
* Allow GitHub Actions to create and approve pull requests (in Actions Settings)
- If you use the default `secrets.GITHUB_TOKEN`, otherwise use a `repo` scoped Personal Access Token (PAT)

Copy this [changelog configuration](.github/release.yml) into your repository and label your PR's with it's categories.

## Code in Main

> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance. Consider using [Github Codespaces](https://github.com/features/codespaces) or [Gitpod](https://www.gitpod.io/).
Expand Down
25 changes: 25 additions & 0 deletions __tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const comparisonFixture: release.Comparison = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -44,6 +45,7 @@ const comparisonFixture: release.Comparison = {
{
node: {
number: 10,
merged: true,
labels: {
nodes: [
{
Expand All @@ -65,6 +67,7 @@ const comparisonFixture: release.Comparison = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -86,6 +89,7 @@ const comparisonFixture: release.Comparison = {
{
node: {
number: 30,
merged: false,
labels: {
nodes: []
}
Expand Down Expand Up @@ -118,6 +122,7 @@ const associatedPullRequestsMajor = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -139,6 +144,7 @@ const associatedPullRequestsMajor = {
{
node: {
number: 10,
merged: true,
labels: {
nodes: [
{
Expand All @@ -160,6 +166,7 @@ const associatedPullRequestsMajor = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -181,6 +188,7 @@ const associatedPullRequestsMajor = {
{
node: {
number: 30,
merged: true,
labels: {
nodes: []
}
Expand Down Expand Up @@ -213,6 +221,7 @@ const associatedPullRequestsMinor = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -234,6 +243,7 @@ const associatedPullRequestsMinor = {
{
node: {
number: 10,
merged: true,
labels: {
nodes: [
{
Expand All @@ -255,6 +265,7 @@ const associatedPullRequestsMinor = {
{
node: {
number: 20,
merged: true,
labels: {
nodes: [
{
Expand All @@ -276,6 +287,7 @@ const associatedPullRequestsMinor = {
{
node: {
number: 30,
merged: true,
labels: {
nodes: []
}
Expand Down Expand Up @@ -308,6 +320,19 @@ test('Changelog null ref', () => {
expect(changelog).toStrictEqual([])
})

test('Filter merged PR', () => {
const merged = release.filterMerged(comparisonFixture)
const changelog = release.changelog(merged)

expect(changelog).toStrictEqual(['- #10', '- #20'])
})

test('Filter merged PR no changes', () => {
const merged = release.filterMerged(refNotFound)

expect(merged).toStrictEqual(refNotFound)
})

describe('Detect changes', () => {
test('Major', () => {
const ver = release.detectChanges(comparisonFixture)
Expand Down
53 changes: 47 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inputs:
token:
description: 'GITHUB_TOKEN or a `repo` scoped Personal Access Token (PAT)'
default: ${{ github.token }}
release-title:
description: 'Release title prefix'
required: true
pre-release:
description: 'This release will be labeled as non-production ready'
default: false
Expand All @@ -17,6 +20,10 @@ inputs:
version-files:
description: 'Files to bump with the new version'
required: false
enable-auto-merge:
description: 'Enable release pull request auto-merge'
required: false
default: '1'
outputs:
increment:
description: 'Type of the next release (e.g. major)'
Expand All @@ -27,6 +34,12 @@ outputs:
runs:
using: "composite"
steps:
- name: Clone consumer repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ inputs.token }}
ref: ${{ inputs.develop-branch }}
- name: Grab current version
id: current-version
run: echo "current-version=$(cat VERSION)" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -63,10 +76,38 @@ runs:
commit-message: "chore(release): bump to ${{steps.release.outputs.next-version}}"
delete-branch: true
labels: release
- name: Enable Pull Request Automerge
if: steps.cpr.outputs.pull-request-operation == 'created'
uses: peter-evans/enable-pull-request-automerge@a660677d5469627102a1c1e11409dd063606628d # v3.0.0
- name: Enable Pull Request Auto-merge
if: inputs.enable-auto-merge == '1' && steps.cpr.outputs.pull-request-operation == 'created'
run: gh pr merge --merge --auto "${{ steps.cpr.outputs.pull-request-number }}"
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
- name: Create a new Github release
if: github.event.pull_request.merged && contains(github.event.pull_request.labels.*.name, 'release')
id: gh-release
uses: actions/github-script@v6
with:
token: ${{ inputs.token }}
pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
merge-method: merge
github-token: ${{ inputs.token }}
result-encoding: string
script: |
const currentVersion = "${{ steps.current-version.outputs.current-version }}";
const stableVersion = /^\d+\.\d+\.\d+$/;
const preRelease = !stableVersion.test(currentVersion);
github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `v${currentVersion}`,
target_commitish: "${{ inputs.develop-branch }}",
name: `${{ inputs.release-title }} v${currentVersion}`,
generate_release_notes: true,
prerelease: preRelease
});
return 'Created';
- name: Run summary
shell: bash
run: |
echo "- Current version: ${{steps.current-version.outputs.current-version}}" >> $GITHUB_STEP_SUMMARY
echo "- Next version: ${{steps.release.outputs.next-version}}" >> $GITHUB_STEP_SUMMARY
echo "- Release pull request: ${{ steps.cpr.outputs.pull-request-number }}" >> $GITHUB_STEP_SUMMARY
echo "- Github release: ${{ steps.gh-release.outputs.result }}" >> $GITHUB_STEP_SUMMARY
Loading

0 comments on commit a38c899

Please sign in to comment.