Skip to content

Commit

Permalink
Move build and publish to jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
yershalom committed Jan 27, 2019
1 parent f1648e9 commit 548e5fa
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ npm-debug.log
.idea
yarn.lock
.vscode
package-lock.json
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "React Native Calendar Components",
"scripts": {
"test": "jasmine src/*.spec.js && npm run lint",
"lint": "eslint src/ example/src"
"lint": "eslint src/ example/src",
"release": "node ./scripts/release.js"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +28,8 @@
"eslint": "^3.19.0",
"eslint-plugin-react": "^7.0.0",
"jasmine": "^2.5.2",
"semver": "5.x.x",
"shell-utils": "1.x.x",
"react": "16.6.0-alpha.8af6728",
"react-native": "0.57.4"
}
Expand Down
97 changes: 97 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-disable no-console */
const exec = require('shell-utils').exec;
const semver = require('semver');
const fs = require('fs');
const _ = require('lodash');

const ONLY_ON_BRANCH = 'origin/master';
const VERSION_TAG = 'latest';
const VERSION_INC = 'minor';

function run() {
if (!validateEnv()) {
return;
}
setupGit();
createNpmRc();
versionTagAndPublish();
}

function validateEnv() {
if (!process.env.JENKINS_CI) {
throw new Error('releasing is only available from CI');
}

if (!process.env.JENKINS_MASTER) {
console.log('not publishing on a different build');
return false;
}

if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
return false;
}

return true;
}

function setupGit() {
exec.execSyncSilent('git config --global push.default simple');
exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
const remoteUrl = new RegExp('https?://(\\S+)').exec(exec.execSyncRead('git remote -v'))[1];
exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
}

function createNpmRc() {
exec.execSync('rm -f package-lock.json');
const content = `
email=\${NPM_EMAIL}
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
`;
fs.writeFileSync('.npmrc', content);
}

function versionTagAndPublish() {
const packageVersion = semver.clean(process.env.npm_package_version);
console.log(`package version: ${packageVersion}`);

const currentPublished = findCurrentPublishedVersion();
console.log(`current published version: ${currentPublished}`);

const version = semver.gt(packageVersion, currentPublished) ? packageVersion : semver.inc(currentPublished, VERSION_INC);
tryPublishAndTag(version);
}

function findCurrentPublishedVersion() {
return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
}

function tryPublishAndTag(version) {
let theCandidate = version;
for (let retry = 0; retry < 5; retry++) {
try {
tagAndPublish(theCandidate);
console.log(`Released ${theCandidate}`);
return;
} catch (err) {
const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
if (!alreadyPublished) {
throw err;
}
console.log(`previously published. retrying with increased ${VERSION_INC}...`);
theCandidate = semver.inc(theCandidate, VERSION_INC);
}
}
}

function tagAndPublish(newVersion) {
console.log(`trying to publish ${newVersion}...`);
exec.execSync(`npm --no-git-tag-version --allow-same-version version ${newVersion}`);
exec.execSyncRead(`npm publish --tag ${VERSION_TAG}`);
exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
exec.execSyncSilent(`git push deploy ${newVersion} || true`);
}

run();

0 comments on commit 548e5fa

Please sign in to comment.