Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmgallardo/330630b531b8a92443be062e79da1805 to your computer and use it in GitHub Desktop.
Save dmgallardo/330630b531b8a92443be062e79da1805 to your computer and use it in GitHub Desktop.
Github Action: Create Tag and Release when a pull request is merged into master/main branch with release notes parsed from merge comment

This Github Action workflow along with the rest of these files/scripts will allow you to create a Tag as well as a Release with the release notes set to the text added in the body of the merge comment when a pull request is merged.

Create release on merge github action

Note:

Merge comment title needs to contain the text "release" or else the workflow will not run (see line 11 of create-release.yml).

<?php
// This workflow assumes that you're setting the version of your software using a constant as defined below
// Adjust get-version.sh file as needed
define( 'PREFIX_SOFTWARE_VERSION', '2.0.0' );
//
// Code
//
#!/bin/bash
INPUT=$(grep PREFIX_SOFTWARE_VERSION index.php)
SUBSTRING=$(echo $INPUT| cut -d "'" -f 4 )
echo v$SUBSTRING
<?php
$commit_message = getenv( "COMMITTEXT" );
// Merge commit message has two new lines after title, this is set automatically by Github
$commit_message_parts = explode( "\n\n", $commit_message );
// Get everything after the commit message title
$commit_message_body = $commit_message_parts[1];
// Push commit message body (fix, changes etc ) into an array for formatting
$commit_message_body_lines = explode( "\n", $commit_message_body );
$release_notes = '';
foreach( $commit_message_body_lines as $commit_message_body_line ){
// Format Markdown
$release_notes .= '* ' . $commit_message_body_line . PHP_EOL;
}
// Write to file
file_put_contents( 'release_notes.txt', $release_notes );
exit;
name: Create Release
on:
push:
branches:
- main
jobs:
create-release:
runs-on: ubuntu-latest
if: contains( github.event.head_commit.message, 'release' )
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
- name: Create Release Notes File
env:
COMMITTEXT: "${{ github.event.head_commit.message }}"
run: |
php create-release-notes-file.php
- name: Set Tag Output
id: set-tag
run: echo "::set-output name=tag_name::$(sh get-version.sh)"
- name: Create Tag
uses: actions/github-script@v3
env:
TAG: ${{ steps.set-tag.outputs.tag_name }}
with:
github-token: ${{ github.token }}
script: |
github.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/${{ steps.set-tag.outputs.tag_name }}",
sha: context.sha
})
- name: Create release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.set-tag.outputs.tag_name }}
bodyFile: './release_notes.txt'
token: ${{ secrets.GITHUB_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment