Skip to content

Commit

Permalink
feat: basic jenkins support
Browse files Browse the repository at this point in the history
  • Loading branch information
finnp committed Jun 12, 2017
1 parent e2e41a7 commit 654d594
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ After [enabling Greenkeeper for your repository](https://github.com/integration/

* ✅ Travis CI
* ✅ Circle CI _Thank you [@ethanrubio](https://github.com/greenkeeperio/greenkeeper-lockfile/pull/18) 👏_
* ✅ Jenkins
* 🙏 [Contribute your own](#contributing-a-ci-service)

## How does it work
Expand Down
12 changes: 12 additions & 0 deletions ci-services/jenkins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const env = process.env

const _ = require('lodash')
const gitHelpers = require('../lib/git-helpers')

module.exports = {
gitUrl: env.GIT_URL,
branchName: _.drop(_.split(env.GIT_BRANCH, '/')).join('/'),
firstPush: gitHelpers.getNumberOfCommitsOnBranch(env.GIT_BRANCH) === 1,
correctBuild: true, // assuming pull requests are not build
uploadBuild: true // assuming 1 build per branch
}
3 changes: 2 additions & 1 deletion ci-services/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const env = process.env

module.exports = {
travis: () => env.TRAVIS === 'true',
circleci: () => env.CIRCLECI === 'true'
circleci: () => env.CIRCLECI === 'true',
jenkins: () => env.JENKINS_URL !== undefined
}
6 changes: 3 additions & 3 deletions lib/git-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const _ = require('lodash')

module.exports = {
getNumberOfCommitsOnBranch: function getNumberOfCommitsOnBranch (branch) {
const grepAgument = `refs/heads/${branch}`
const notArgument = `$(git for-each-ref --format="%(refname)" refs/heads/ | grep -v ${grepAgument})`
const refArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep /${branch} | head -1)`
const notArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep -v /${branch})`
return _.toNumber(
exec(
`git log ${branch} --oneline --not ${notArgument} | wc -l`
`git log ${refArgument} --oneline --not ${notArgument} | wc -l`
).toString()
)
}
Expand Down
14 changes: 9 additions & 5 deletions upload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const exec = require('child_process').execSync
const url = require('url')

const config = require('./lib/config')
const info = require('./ci-services')()
Expand Down Expand Up @@ -32,14 +33,17 @@ module.exports = function upload () {
return console.error('Only uploading on one build job')
}

let remote = `[email protected]:${info.repoSlug}`
if (info.gitUrl) remote = info.gitUrl

if (env.GH_TOKEN) {
// Use the GH_TOKEN if provided.
exec(`git remote add gk-origin https://${env.GH_TOKEN}@github.com/${info.repoSlug}`)
} else {
// Otherwise, assume an SSH key is available.
exec(`git remote add gk-origin [email protected]:${info.repoSlug}`)
if (remote.slice(0, 5) !== 'https') remote = `https://github.com/${info.repoSlug}`
const urlParsed = url.parse(remote)
urlParsed.auth = env.GH_TOKEN
remote = url.format(urlParsed)
}

exec(`git remote add gk-origin ${remote}`)
exec(`git push gk-origin HEAD:${info.branchName}`)
}

Expand Down

0 comments on commit 654d594

Please sign in to comment.