Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
feat: elect build leader by highest node version
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The first job in the build matrix is no longer
automatically elected to be the build leader.
Instead the build job with the highest node version is selected.
If you want to control which job is the build leader set the
environment variable `BUILD_LEADER_ID`.
  • Loading branch information
boennemann authored and gr2m committed Aug 19, 2017
1 parent 2bcdee6 commit bd850b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
23 changes: 23 additions & 0 deletions elect-build-leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const semver = require('semver')

module.exports = versions => {
// if there is only one candidate, then it's the winner
if (!Array.isArray(versions) || versions.length === 1) return 1

// if there is latest stable it's the winner
const stable = versions.indexOf('node') + 1
if (stable) return stable

// otherwise we use the lower bound of all valid semver ranges
const validRanges = versions.filter(semver.validRange)
const lowVersionBoundaries = validRanges
.map(semver.Range)
.map(r => r.set[0][0].semver.version)

// then we find the highest of those
const highestVersion = semver.sort(Array.from(lowVersionBoundaries)).pop()
const highestRange = validRanges[lowVersionBoundaries.indexOf(highestVersion)]

// and make its build job the winner
return versions.indexOf(highestRange) + 1
}
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const {promisify} = require('util')
const request = require('axios')
const Travis = require('travis-ci')

const electBuildLeader = require('./elect-build-leader')

module.exports = async function travisDeployOnce (env = process.env) {
if (!env.GH_TOKEN) throw new Error('GitHub token missing')
if (env.TRAVIS !== 'true') throw new Error('Not running on Travis')
if (!env.TRAVIS_JOB_NUMBER.endsWith('.1')) return null
if (env.TRAVIS_TEST_RESULT === '1') return false
if (env.TRAVIS_TEST_RESULT !== '0') throw new Error('Not running in Travis after_success hook')

const {private: pro} = await request({
Expand All @@ -32,7 +32,14 @@ module.exports = async function travisDeployOnce (env = process.env) {

const buildId = parseInt(env.TRAVIS_BUILD_ID, 10)
const buildApi = travis.builds(buildId)
const {build: {job_ids: jobs}} = await promisify(buildApi.get.bind(buildApi))()
const {build: {config, job_ids: jobs}} = await promisify(buildApi.get.bind(buildApi))()

const buildLeader = env.BUILD_LEADER_ID || (config.node_js
? electBuildLeader(config.node_js)
: 1)

if (!env.TRAVIS_JOB_NUMBER.endsWith(`.${buildLeader}`)) return null
if (env.TRAVIS_TEST_RESULT === '1') return false

const currentJobId = parseInt(env.TRAVIS_JOB_ID, 10)
let attempt = 0
Expand Down

0 comments on commit bd850b1

Please sign in to comment.