Skip to content

Commit

Permalink
workflow: improve release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hujiulong committed Jun 26, 2021
1 parent 3236bc2 commit aea8dbb
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
'prefer-object-spread': 'off',
'no-param-reassign': 'off',
'arrow-parens': 'off',
'linebreak-style': ['off', 'windows']
'linebreak-style': ['off', 'windows'],
},
parserOptions: {
parser: 'babel-eslint',
Expand Down
66 changes: 53 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
name: Publish Package
name: Release

on:
release:
types: [published]
push:
tags:
- 'v*'

jobs:

tag:
name: Create release tag
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Create release tag
id: release_tag
uses: yyx990803/release-tag@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
body: |
[CHANGELOG.md](https://github.com/hujiulong/vue-3d-model/blob/master/CHANGELOG.md)
publish-npm:
name: Publish to NPM
runs-on: ubuntu-latest
needs: tag
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: '14'
registry-url: 'https://registry.npmjs.org'
- name: Build & publish
run: |
npm install
npm run build
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

publish-gpr:
name: Publish to GPR
runs-on: ubuntu-latest
needs: tag
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm run test
- run: npm run build
- run: npm publish
node-version: '14'
registry-url: 'https://npm.pkg.github.com/'
scope: '@hujiulong'
- name: Build & publish
run: |
npm install
npm run build
npm run gpr-setup
npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
"build": "vue-cli-service build --target lib --name vue-3d-model src/index.js",
"lint": "vue-cli-service lint src/ tests/ examples/",
"lint:fix": "npm run lint -- --fix",
"test": "vue-cli-service test:unit"
"test": "vue-cli-service test:unit",
"gpr-setup": "node scripts/gpr-setup.js",
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
},
"gitHooks": {
"commit-msg": "node scripts/verify-commit.js"
},
"peerDependencies": {
"vue": ">=2.0.0"
Expand All @@ -39,13 +45,20 @@
"@vue/eslint-config-airbnb": "^5.0.2",
"@vue/test-utils": "^1.0.3",
"babel-eslint": "^10.1.0",
"chalk": "^4.1.1",
"conventional-changelog-cli": "^2.1.1",
"enquirer": "^2.3.6",
"eslint": "^7.5.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-vue": "^7.1.0",
"execa": "^5.1.1",
"less": "^4.1.1",
"less-loader": "^10.0.0",
"minimist": "^1.2.5",
"semver": "^7.3.5",
"vue": "^2.6.11",
"vue-router": "^3.3.4",
"vue-template-compiler": "^2.6.11"
"vue-template-compiler": "^2.6.11",
"yorkie": "^2.0.0"
}
}
11 changes: 11 additions & 0 deletions scripts/gpr-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');

pkg.name = '@hujiulong/vue-3d-model';

// Update package.json with the udpated name
fs.writeFileSync(
path.join(__dirname, '../package.json'),
JSON.stringify(pkg, null, 2),
);
130 changes: 130 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* eslint-disable */

const args = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const semver = require('semver');
const { prompt } = require('enquirer');
const execa = require('execa');
const pkg = require('../package.json');

const currentVersion = pkg.version;
const preId =
args.preid ||
(semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0]);
const isDryRun = args.dry;
const skipTests = args.skipTests;
const skipBuild = args.skipBuild;

const pkgPath = path.resolve(__dirname, '../package.json');
const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []),
];

const inc = (i) => semver.inc(currentVersion, i, preId);
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts });
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts);
const runIfNotDry = isDryRun ? dryRun : run;
const step = (msg) => console.log(chalk.cyan(msg));

async function main() {
let targetVersion = args._[0];

if (!targetVersion) {
// no explicit version, offer suggestions
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map((i) => `${i} (${inc(i)})`)
.concat(['custom']),
});

if (release === 'custom') {
targetVersion = (
await prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion,
})
).version;
} else {
targetVersion = release.match(/\((.*)\)/)[1];
}
}

if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`);
}

const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`,
});

if (!yes) {
return;
}

// update all package versions and inter-dependencies
step('\nUpdating cross dependencies...');
if (!isDryRun) {
pkg.version = targetVersion;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
} else {
console.log(`(skipped)`);
}

// run tests before release
step('\nRunning tests...');
if (!skipTests && !isDryRun) {
await run('npm', ['test']);
} else {
console.log(`(skipped)`);
}

// build package
step('\nBuilding package...');
if (!skipBuild && !isDryRun) {
await run('npm', ['build', '--release']);
} else {
console.log(`(skipped)`);
}

// generate changelog
await run(`npm`, ['run', 'changelog']);

const { stdout } = await run('git', ['diff'], { stdio: 'pipe' });
if (stdout) {
step('\nCommitting changes...');
await runIfNotDry('git', ['add', '-A']);
await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`]);
} else {
console.log('No changes to commit.');
}

// push to GitHub
step('\nPushing to GitHub...');
await runIfNotDry('git', ['tag', `v${targetVersion}`]);
await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`]);
await runIfNotDry('git', ['push']);

if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`);
}

console.log();
}

main().catch((err) => {
console.error(err);
});
28 changes: 28 additions & 0 deletions scripts/verify-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Invoked on the commit-msg git hook by yorkie.

/* eslint-disable prefer-template, quotes, operator-linebreak */

const chalk = require('chalk');

const msgPath = process.env.GIT_PARAMS;
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim();

const commitRE = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/;

if (!commitRE.test(msg)) {
console.log();
console.error(
` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
`invalid commit message format.`,
)}\n\n` +
chalk.red(
` Proper commit message format is required for automated changelog generation. Examples:\n\n`,
) +
` ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
` ${chalk.green(
`fix(v-model): handle events on blur (close #28)`,
)}\n\n` +
chalk.red(` See .github/commit-convention.md for more details.\n`),
);
process.exit(1);
}

0 comments on commit aea8dbb

Please sign in to comment.