|
| 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