Skip to content

Commit

Permalink
Extended yarn ship: more automation
Browse files Browse the repository at this point in the history
no issue

- added gulp task to extend casper release automation
- will draft the release for you
- will get the user facing commits from changelog
- runs after `yarn ship` (postship)
- full automation with env variables is possible
  • Loading branch information
kirrg001 committed Mar 15, 2019
1 parent 406ef78 commit 8729daf
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -22,3 +22,7 @@ projectFilesBackup
.DS_Store

dist/

config.json
changelog.md
changelog.md.bk
6 changes: 6 additions & 0 deletions config.example.json
@@ -0,0 +1,6 @@
{
"github": {
"username": "<username>",
"token": "<gh-personal-access-token>"
}
}
119 changes: 119 additions & 0 deletions gulpfile.js
Expand Up @@ -78,3 +78,122 @@ const dev = series(build, serve, watcher);
exports.build = build;
exports.zip = series(build, zipper);
exports.default = dev;

// release imports
const path = require('path');
const releaseUtils = require('@tryghost/release-utils');

let config;
try {
config = require('./config');
} catch (err) {
config = null;
}

const REPO = 'TryGhost/Casper';
const USER_AGENT = 'Casper';
const CHANGELOG_PATH = path.join(process.cwd(), '.', 'changelog.md');

const changelog = ({previousVersion}) => {
const changelog = new releaseUtils.Changelog({
changelogPath: CHANGELOG_PATH,
folder: path.join(process.cwd(), '.')
});

changelog
.write({
githubRepoPath: `https://github.com/${REPO}`,
lastVersion: previousVersion
})
.sort()
.clean();
};

const previousRelease = () => {
return releaseUtils
.releases
.get({
userAgent: USER_AGENT,
uri: `https://api.github.com/repos/${REPO}/releases`
})
.then((response) => {
if (!response || !response.length) {
console.log('No releases found. Skipping');
return;
}

console.log(`Previous version ${response[0].name}`);
return response[0].name;
});
};

/**
*
* `yarn ship` will trigger `postship` task.
*
* [optional] For full automation
*
* `GHOST=2.10.1,2.10.0 yarn ship`
* First value: Ships with Ghost
* Second value: Compatible with Ghost/GScan
*
* You can manually run in case the task has thrown an error.
*
* `npm_package_version=0.5.0 gulp release`
*/
const release = () => {
// @NOTE: https://yarnpkg.com/lang/en/docs/cli/version/
const newVersion = process.env.npm_package_version;
let shipsWithGhost = '{version}';
let compatibleWithGhost = '2.10.0';
const ghostEnvValues = process.env.GHOST || null;

if (ghostEnvValues) {
shipsWithGhost = ghostEnvValues.split(',')[0];
compatibleWithGhost = ghostEnvValues.split(',')[1];

if (!compatibleWithGhost) {
compatibleWithGhost = '2.10.0';
}
}

if (!newVersion || newVersion === '') {
console.log('Invalid version.');
return;
}

console.log(`\nDraft release for ${newVersion}.`);

if (!config || !config.github || !config.github.username || !config.github.token) {
console.log('Please copy config.example.json and configure Github token.');
return;
}

return previousRelease()
.then((previousVersion)=> {

changelog({previousVersion});

return releaseUtils
.releases
.create({
draft: true,
preRelease: false,
tagName: newVersion,
releaseName: newVersion,
userAgent: USER_AGENT,
uri: `https://api.github.com/repos/${REPO}/releases`,
github: {
username: config.github.username,
token: config.github.token
},
content: [`**Ships with Ghost ${shipsWithGhost} Compatible with Ghost >= ${compatibleWithGhost}**\n\n`],
changelogPath: CHANGELOG_PATH
})
.then((response)=> {
console.log(`\nRelease draft generated: ${response.releaseUrl}\n`);
});
});
};

exports.release = release;
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,8 @@
"test": "gscan .",
"pretest": "gulp build",
"preship": "yarn test",
"ship": "STATUS=$(git status --porcelain); echo $STATUS; if [ -z \"$STATUS\" ]; then yarn version && git push --follow-tags; fi"
"ship": "STATUS=$(git status --porcelain); echo $STATUS; if [ -z \"$STATUS\" ]; then yarn version && git push --follow-tags; else echo \"Uncomitted changes found.\" && exit 1; fi",
"postship": "git fetch && gulp release"
},
"author": {
"name": "Ghost Foundation",
Expand All @@ -44,6 +45,7 @@
"bugs": "https://github.com/TryGhost/Casper/issues",
"contributors": "https://github.com/TryGhost/Casper/graphs/contributors",
"devDependencies": {
"@tryghost/release-utils": "0.2.0",
"autoprefixer": "9.4.10",
"beeper": "1.1.1",
"cssnano": "4.1.10",
Expand Down

0 comments on commit 8729daf

Please sign in to comment.