Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
feat: add buildkite support for env vars for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 21, 2017
1 parent 69b5d1a commit d6c24bc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/providers/buildkite-api.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
axios = require("axios")
R = require("ramda")
la = require("lazy-ass")
check = require("check-more-types")
debug = require("debug")("bumper")

toData = R.prop("data")

getPipelineUrl = (organization, project) ->
la(check.unemptyString(organization), "missing organization", organization)
la(check.unemptyString(project), "missing project", project)
"/organizations/#{organization}/pipelines/#{project}"

module.exports = {
create: (token) ->
throw new Error("Buildkite requires an access token!") if !token

api = axios.create({
baseURL: "https://api.buildkite.com/v2"
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer #{token}"
}
})

api.getPipelineUrl = getPipelineUrl

api.getPipeline = (organization, project) ->
url = getPipelineUrl(organization, project)
debug("getting pipeline", url)
api.get(url)
.then toData

api.setPipeline = (organization, project, pipeline) ->
la(check.object(pipeline), "missing pipeline to set", pipeline)
debug("setting pipeline for", organization, project)
url = getPipelineUrl(organization, project)
api.post(url, pipeline)
.then toData

api.setEnvironmentVariables = (organization, project, variables = {}) ->
url = getPipelineUrl(organization, project)
api.patch(url, {
env: variables
})

api.updateEnvironmentVariables = (organization, project, variables) ->
api.getPipeline(organization, project)
.then (pipeline) ->
merged = R.merge(pipeline.env, variables)
api.setEnvironmentVariables(organization, project, merged)

api.triggerNewBuild = (organization, project) ->
throw new Error "Not implemented for Buildkite yet"
# https://buildkite.com/docs/rest-api/builds#create-a-build

return api
}
30 changes: 30 additions & 0 deletions lib/providers/buildkite.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
_ = require("lodash")
Promise = require("bluebird")
providerApi = require("./buildkite-api")
debug = require("debug")("bumper")

api =
configure: (options={}) ->
api = providerApi.create(options.buildkiteToken)

return {
updateProjectEnv: (organization, project, variables) ->
debug("updating Buildkite variables for", organization, project)
debug(variables)
api.updateEnvironmentVariables(organization, project, variables)

runProject: (organization, project) ->
api.triggerNewBuild(organization, project)
}

module.exports = api

if !module.parent
console.log("Buildkite demo")
if !process.env.support__ci_json
throw new Error("Missing support__ci_json in environment")
tokens = JSON.parse(process.env.support__ci_json)
ci = api.configure(tokens)
ci.updateProjectEnv("cypress-io", "bumpercar-test", {
foo: "foo"
}).catch(console.error)
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"@cypress/coffee-script": "0.1.2",
"axios": "^0.15.3",
"bluebird": "^3.5.0",
"check-more-types": "^2.24.0",
"debug": "^3.1.0",
"lazy-ass": "^1.6.0",
"lodash": "^4.17.4",
"ramda": "^0.24.1"
},
Expand Down

0 comments on commit d6c24bc

Please sign in to comment.