|
1 |
| -# Automate release workflow |
| 1 | +<!-- front-matter |
| 2 | +id: automate-releases |
| 3 | +title: Automate Releases |
| 4 | +hide_title: true |
| 5 | +sidebar_label: Automate Releases |
| 6 | +--> |
| 7 | + |
| 8 | +# Automate Releases |
2 | 9 |
|
3 | 10 | If your project follows a semantic versioning, it may be a good idea to automatize the steps needed to do a release.
|
4 |
| -Below you have a simple recipe that bumps the project version, commits the changes to git and creates a new tag. |
5 |
| - |
6 |
| -``` javascript |
7 |
| - |
8 |
| -var gulp = require('gulp'); |
9 |
| -var conventionalChangelog = require('gulp-conventional-changelog'); |
10 |
| -var conventionalGithubReleaser = require('conventional-github-releaser'); |
11 |
| -var bump = require('gulp-bump'); |
12 |
| -var log = require('gulplog'); |
13 |
| -var git = require('gulp-git'); |
14 |
| -var fs = require('fs'); |
15 |
| - |
16 |
| -gulp.task('changelog', function () { |
17 |
| - return gulp.src('CHANGELOG.md', { |
18 |
| - buffer: false |
19 |
| - }) |
20 |
| - .pipe(conventionalChangelog({ |
21 |
| - preset: 'angular' // Or to any other commit message convention you use. |
22 |
| - })) |
23 |
| - .pipe(gulp.dest('./')); |
24 |
| -}); |
25 |
| - |
26 |
| -gulp.task('github-release', function(done) { |
27 |
| - conventionalGithubReleaser({ |
28 |
| - type: "oauth", |
29 |
| - token: 'abcdefghijklmnopqrstuvwxyz1234567890' // change this to your own GitHub token or use an environment variable |
30 |
| - }, { |
31 |
| - preset: 'angular' // Or to any other commit message convention you use. |
32 |
| - }, done); |
33 |
| -}); |
34 |
| - |
35 |
| -gulp.task('bump-version', function () { |
36 |
| -// We hardcode the version change type to 'patch' but it may be a good idea to |
37 |
| -// use minimist (https://www.npmjs.com/package/minimist) to determine with a |
38 |
| -// command argument whether you are doing a 'major', 'minor' or a 'patch' change. |
39 |
| - return gulp.src(['./bower.json', './package.json']) |
40 |
| - .pipe(bump({type: "patch"}).on('error', log.error)) |
41 |
| - .pipe(gulp.dest('./')); |
42 |
| -}); |
43 |
| - |
44 |
| -gulp.task('commit-changes', function () { |
45 |
| - return gulp.src('.') |
46 |
| - .pipe(git.add()) |
47 |
| - .pipe(git.commit('[Prerelease] Bumped version number')); |
48 |
| -}); |
49 |
| - |
50 |
| -gulp.task('push-changes', function (done) { |
51 |
| - git.push('origin', 'master', done); |
52 |
| -}); |
53 |
| - |
54 |
| -gulp.task('create-new-tag', function (done) { |
55 |
| - var version = getPackageJsonVersion(); |
56 |
| - git.tag(version, 'Created Tag for version: ' + version, function (error) { |
57 |
| - if (error) { |
58 |
| - return done(error); |
59 |
| - } |
60 |
| - git.push('origin', 'master', {args: '--tags'}, done); |
| 11 | +The recipe below bumps the project version, commits the changes to git and creates a new GitHub release. |
| 12 | + |
| 13 | +For publishing a GitHub release you'll need to [create a personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token) and add it to your project. However, we don't want to commit it, so we'll use [`dotenv`](https://www.npmjs.com/package/dotenv) to load it from a git-ignored `.env` file: |
| 14 | + |
| 15 | +``` |
| 16 | +GH_TOKEN=ff34885... |
| 17 | +``` |
| 18 | + |
| 19 | +Don't forget to add `.env` to your `.gitignore`. |
| 20 | + |
| 21 | +Next, install all the necessary dependencies for this recipe: |
| 22 | + |
| 23 | +```sh |
| 24 | +npm install --save-dev conventional-recommended-bump conventional-changelog-cli conventional-github-releaser dotenv execa |
| 25 | +``` |
| 26 | + |
| 27 | +Based on your environment, setup and preferences, your release workflow might look something like this: |
| 28 | + |
| 29 | +``` js |
| 30 | +const gulp = require('gulp'); |
| 31 | +const conventionalRecommendedBump = require('conventional-recommended-bump'); |
| 32 | +const conventionalGithubReleaser = require('conventional-github-releaser'); |
| 33 | +const execa = require('execa'); |
| 34 | +const fs = require('fs'); |
| 35 | +const { promisify } = require('util'); |
| 36 | +const dotenv = require('dotenv'); |
| 37 | + |
| 38 | +// load environment variables |
| 39 | +const result = dotenv.config(); |
| 40 | + |
| 41 | +if (result.error) { |
| 42 | + throw result.error; |
| 43 | +} |
| 44 | + |
| 45 | +// Conventional Changelog preset |
| 46 | +const preset = 'angular'; |
| 47 | +// print output of commands into the terminal |
| 48 | +const stdio = 'inherit'; |
| 49 | + |
| 50 | +async function bumpVersion() { |
| 51 | + // get recommended version bump based on commits |
| 52 | + const { releaseType } = await promisify(conventionalRecommendedBump)({ preset }); |
| 53 | + // bump version without committing and tagging |
| 54 | + await execa('npm', ['version', releaseType, '--no-git-tag-version'], { |
| 55 | + stdio, |
61 | 56 | });
|
| 57 | +} |
| 58 | + |
| 59 | +async function changelog() { |
| 60 | + await execa( |
| 61 | + 'npx', |
| 62 | + [ |
| 63 | + 'conventional-changelog', |
| 64 | + '--preset', |
| 65 | + preset, |
| 66 | + '--infile', |
| 67 | + 'CHANGELOG.md', |
| 68 | + '--same-file', |
| 69 | + ], |
| 70 | + { stdio } |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +async function commitTagPush() { |
| 75 | + // even though we could get away with "require" in this case, we're taking the safe route |
| 76 | + // because "require" caches the value, so if we happen to use "require" again somewhere else |
| 77 | + // we wouldn't get the current value, but the value of the last time we called "require" |
| 78 | + const { version } = JSON.parse(await promisify(fs.readFile)('package.json')); |
| 79 | + const commitMsg = `chore: release ${version}`; |
| 80 | + await execa('git', ['add', '.'], { stdio }); |
| 81 | + await execa('git', ['commit', '--message', commitMsg], { stdio }); |
| 82 | + await execa('git', ['tag', `v${version}`], { stdio }); |
| 83 | + await execa('git', ['push', '--follow-tags'], { stdio }); |
| 84 | +} |
62 | 85 |
|
63 |
| - function getPackageJsonVersion () { |
64 |
| - // We parse the json file instead of using require because require caches |
65 |
| - // multiple calls so the version number won't be updated |
66 |
| - return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version; |
67 |
| - }; |
68 |
| -}); |
69 |
| - |
70 |
| -gulp.task('release', gulp.series( |
71 |
| - 'bump-version', |
72 |
| - 'changelog', |
73 |
| - 'commit-changes', |
74 |
| - 'push-changes', |
75 |
| - 'create-new-tag', |
76 |
| - 'github-release' |
77 |
| -)); |
| 86 | +function githubRelease(done) { |
| 87 | + conventionalGithubReleaser( |
| 88 | + { type: 'oauth', token: process.env.GH_TOKEN }, |
| 89 | + { preset }, |
| 90 | + done |
| 91 | + ); |
| 92 | +} |
78 | 93 |
|
| 94 | +exports.release = gulp.series( |
| 95 | + bumpVersion, |
| 96 | + changelog, |
| 97 | + commitTagPush, |
| 98 | + githubRelease |
| 99 | +); |
79 | 100 | ```
|
0 commit comments