Skip to content

Commit 548e5fa

Browse files
committed
Move build and publish to jenkins
1 parent f1648e9 commit 548e5fa

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ npm-debug.log
44
.idea
55
yarn.lock
66
.vscode
7+
package-lock.json

.travis.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"description": "React Native Calendar Components",
66
"scripts": {
77
"test": "jasmine src/*.spec.js && npm run lint",
8-
"lint": "eslint src/ example/src"
8+
"lint": "eslint src/ example/src",
9+
"release": "node ./scripts/release.js"
910
},
1011
"repository": {
1112
"type": "git",
@@ -27,6 +28,8 @@
2728
"eslint": "^3.19.0",
2829
"eslint-plugin-react": "^7.0.0",
2930
"jasmine": "^2.5.2",
31+
"semver": "5.x.x",
32+
"shell-utils": "1.x.x",
3033
"react": "16.6.0-alpha.8af6728",
3134
"react-native": "0.57.4"
3235
}

scripts/release.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* eslint-disable no-console */
2+
const exec = require('shell-utils').exec;
3+
const semver = require('semver');
4+
const fs = require('fs');
5+
const _ = require('lodash');
6+
7+
const ONLY_ON_BRANCH = 'origin/master';
8+
const VERSION_TAG = 'latest';
9+
const VERSION_INC = 'minor';
10+
11+
function run() {
12+
if (!validateEnv()) {
13+
return;
14+
}
15+
setupGit();
16+
createNpmRc();
17+
versionTagAndPublish();
18+
}
19+
20+
function validateEnv() {
21+
if (!process.env.JENKINS_CI) {
22+
throw new Error('releasing is only available from CI');
23+
}
24+
25+
if (!process.env.JENKINS_MASTER) {
26+
console.log('not publishing on a different build');
27+
return false;
28+
}
29+
30+
if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
31+
console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
38+
function setupGit() {
39+
exec.execSyncSilent('git config --global push.default simple');
40+
exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
41+
exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
42+
const remoteUrl = new RegExp('https?://(\\S+)').exec(exec.execSyncRead('git remote -v'))[1];
43+
exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
44+
exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
45+
}
46+
47+
function createNpmRc() {
48+
exec.execSync('rm -f package-lock.json');
49+
const content = `
50+
email=\${NPM_EMAIL}
51+
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
52+
`;
53+
fs.writeFileSync('.npmrc', content);
54+
}
55+
56+
function versionTagAndPublish() {
57+
const packageVersion = semver.clean(process.env.npm_package_version);
58+
console.log(`package version: ${packageVersion}`);
59+
60+
const currentPublished = findCurrentPublishedVersion();
61+
console.log(`current published version: ${currentPublished}`);
62+
63+
const version = semver.gt(packageVersion, currentPublished) ? packageVersion : semver.inc(currentPublished, VERSION_INC);
64+
tryPublishAndTag(version);
65+
}
66+
67+
function findCurrentPublishedVersion() {
68+
return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
69+
}
70+
71+
function tryPublishAndTag(version) {
72+
let theCandidate = version;
73+
for (let retry = 0; retry < 5; retry++) {
74+
try {
75+
tagAndPublish(theCandidate);
76+
console.log(`Released ${theCandidate}`);
77+
return;
78+
} catch (err) {
79+
const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
80+
if (!alreadyPublished) {
81+
throw err;
82+
}
83+
console.log(`previously published. retrying with increased ${VERSION_INC}...`);
84+
theCandidate = semver.inc(theCandidate, VERSION_INC);
85+
}
86+
}
87+
}
88+
89+
function tagAndPublish(newVersion) {
90+
console.log(`trying to publish ${newVersion}...`);
91+
exec.execSync(`npm --no-git-tag-version --allow-same-version version ${newVersion}`);
92+
exec.execSyncRead(`npm publish --tag ${VERSION_TAG}`);
93+
exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
94+
exec.execSyncSilent(`git push deploy ${newVersion} || true`);
95+
}
96+
97+
run();

0 commit comments

Comments
 (0)