リリースの自動化#

プロジェクトが意味バージョン管理に従っている場合は、リリースに必要なステップを自動化するのもいい考えです。以下のレシピはプロジェクトのバージョンを更新し、gitに変更をコミットし、新しいGitHubリリースを作成します。

GitHubリリースを公開するには、パーソナルアクセストークンを作成して、プロジェクトに追加する必要があります。ただし、コミットしたくないので、dotenvを使用して、git無視の.envファイルからロードします。

GH_TOKEN=ff34885...

.gitignoreに必ず.envを追加してください。

次に、このレシピに必要な依存関係をすべてインストールします

npm install --save-dev conventional-recommended-bump conventional-changelog-cli conventional-github-releaser dotenv execa

環境、設定、好みに基づいて、リリースワークフローは次のような感じになるかもしれません

const gulp = require('gulp');
const conventionalRecommendedBump = require('conventional-recommended-bump');
const conventionalGithubReleaser = require('conventional-github-releaser');
const execa = require('execa');
const fs = require('fs');
const { promisify } = require('util');
const dotenv = require('dotenv');
// 環境変数をロードする
const result = dotenv.config();
if (result.error) {
throw result.error;
}
// Conventional Changelog のプリセット
const preset = 'angular';
// ターミナルにコマンドのアウトプットを出力する
const stdio = 'inherit';
async function bumpVersion() {
// コミットに基づいて推奨するバージョンバンプを取得する
const { releaseType } = await promisify(conventionalRecommendedBump)({ preset });
// コミットやタグ付けをせずにバージョンをバンプする
await execa('npm', ['version', releaseType, '--no-git-tag-version'], {
stdio,
});
}
async function changelog() {
await execa(
'npx',
[
'conventional-changelog',
'--preset',
preset,
'--infile',
'CHANGELOG.md',
'--same-file',
],
{ stdio }
);
}
async function commitTagPush() {
// ここでは “require” で済むが、安全策を講じる
// “require” は値をキャッシュするため、別の場所で二度目に “require” を使用する場合
// 最後の “require” の呼び出し時の値を取得するのではなく、現在の値を取得する
const { version } = JSON.parse(await promisify(fs.readFile)('package.json'));
constコミットメッセージ= `chore: リリース ${バージョン}`;
await execa('git', ['add', '.'], {stdio });
await execa('git', ['commit', '--message',コミットメッセージ], {stdio });
await execa('git', ['tag', `v${version}`], { stdio });
await execa('git', ['push', '--follow-tags'], { stdio });
}
function githubRelease(done) {
conventionalGithubReleaser(
{タイプ: 'oauth',トークン:プロセス.env.GH_TOKEN },
{プリセット },
完了
);
}
exports.release =gulp.series(
bumpVersion,
changelog,
commitTagPush,
githubRelease
);