Skip to content

Commit

Permalink
Merge pull request #590 from bitmovin/feature/automate-release-on-merge
Browse files Browse the repository at this point in the history
Automate release on merge in develop
  • Loading branch information
mukulmishra18 authored Dec 29, 2023
2 parents fcd403d + 294a431 commit c59dee7
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 58 deletions.
29 changes: 29 additions & 0 deletions .github/scripts/defineVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const semver = require('semver');

function getPlayerUiVersion(versionInput) {
const playerUiVersion = semver.valid(versionInput);
if (!playerUiVersion) {
console.error(`${versionInput} is not a valid semver`);
process.exit(1);
}

return {
major: semver.major(playerUiVersion),
minor: semver.minor(playerUiVersion),
patch: semver.patch(playerUiVersion),
prereleaseLabels: semver.prerelease(playerUiVersion),
full: playerUiVersion,
};
}

function defineReleaseVersion({ core }, targetReleaseLevel, givenVersion) {
core.info(`Defining new release version for level ${targetReleaseLevel} and version ${givenVersion}`);

const newVersion = semver.inc(givenVersion, targetReleaseLevel);

const parsedPlayerVersion = getPlayerUiVersion(newVersion);
core.info(`Using release version ${parsedPlayerVersion.full}`);
return parsedPlayerVersion;
}

module.exports.defineReleaseVersion = defineReleaseVersion;
17 changes: 17 additions & 0 deletions .github/scripts/updateChangelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Updates the changelog by replacing the changelog header with the correct version
*
* @param {string} changelogString the content of the changelog file
* @param {string} version the player version to be released
* @param {string} releaseDate the release date to be written to the changelog
*/
function updateChangeLog(changelogString, version, releaseDate) {
const optionalBetaOrRc = '(-rc.d+)?(-(b|beta).d+)?';
const changelogVersionRegExp = new RegExp(
`\\[(development|develop|unreleased|${version})${optionalBetaOrRc}.*`,
'gi',
);
return changelogString.replace(changelogVersionRegExp, `[${version}] - ${releaseDate}`);
}

module.exports.updateChangeLog = updateChangeLog;
64 changes: 64 additions & 0 deletions .github/workflows/tag-release-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Trigger release on merge
run-name: Starting release for ${{ github.actor }} PR merge
on:
pull_request:
types:
- closed
branches:
- develop

jobs:
trigger-ui-release:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: npm ci

- name: Read package.json version
uses: actions/github-script@v6
id: define-package-json-version
with:
script: |
const { version } = require('./package.json')
core.info(`performing a minor release for existing version ${version}`)
core.setOutput('packageJsonVersion', version)
- name: Define release version
uses: actions/github-script@v6
id: define-release-version
with:
script: |
const { defineReleaseVersion } = require('./.github/scripts/defineVersion.js')
return defineReleaseVersion({core}, 'minor', undefined, "${{ steps.define-package-json-version.outputs.packageJsonVersion }}" )
- name: Bump package.json version
run: |
git config --global user.name 'Automated Release'
git config --global user.email '[email protected]'
npm version "${{ fromJson(steps.define-release-version.outputs.result).full }}"
- name: Update Changelog
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const { updateChangeLog } = require('./.github/scripts/updateChangelog.js')
const stableVersion = '${{ fromJson(steps.define-release-version.outputs.result).full }}'.split('-')[0]
const releaseDate = new Date().toISOString().split('T')[0]
const data = fs.readFileSync('./CHANGELOG.md',{encoding:'utf8', flag:'r'});
core.info(`Updating ${stableVersion} with date ${releaseDate} in Changelog`);
const changelogFileContents = updateChangeLog(data, stableVersion, releaseDate);
fs.writeFileSync('./CHANGELOG.md', changelogFileContents, 'utf-8');
- name: Push changes
run: |
git add .
git commit -m "Add release date to changelog"
git push origin develop
git push origin --tags
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [develop]

### Added
- Automate release on every PR merge to develop

## [3.52.2] - 2023-11-23

### Fixed
Expand Down
108 changes: 51 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"merge2": "^1.4.1",
"postcss-svg": "^3.0.0",
"sass": "^1.59.3",
"semver": "^7.5.4",
"stream-combiner2": "^1.1.1",
"ts-jest": "^29.0.5",
"tsify": "^5.0.4",
Expand All @@ -59,4 +60,4 @@
"watchify": "^4.0.0",
"yargs": "^17.7.1"
}
}
}
26 changes: 26 additions & 0 deletions spec/release/defineVersion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { defineReleaseVersion } = require('../../.github/scripts/defineVersion');

describe('defineReleaseVersion', () => {
test.each`
existingVersion | desiredReleaseLevel | expectedVersion
${'1.0.0'} | ${'major'} | ${'2.0.0'}
${'1.0.0'} | ${'minor'} | ${'1.1.0'}
${'1.1.0'} | ${'minor'} | ${'1.2.0'}
${'1.0.0'} | ${'patch'} | ${'1.0.1'}
${'1.1.0'} | ${'patch'} | ${'1.1.1'}
${'1.0.4'} | ${'patch'} | ${'1.0.5'}
${'1.0.0'} | ${'minor'} | ${'1.1.0'}
${'1.0.0'} | ${'minor'} | ${'1.1.0'}
${'1.0.0'} | ${'major'} | ${'2.0.0'}
${'1.5.0'} | ${'major'} | ${'2.0.0'}
${'2.0.0'} | ${'major'} | ${'3.0.0'}
`(
'should return version $expectedVersion with version $existingVersion and $desiredReleaseLevel release level',
({ existingVersion, desiredReleaseLevel, expectedVersion }) => {
const core = { info() {} };
const result = defineReleaseVersion({ core }, desiredReleaseLevel, existingVersion);

expect(result.full).toEqual(expectedVersion);
},
);
});

0 comments on commit c59dee7

Please sign in to comment.