-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(envs): add abstruse defined ENV variables (closes #311)
- Loading branch information
Showing
6 changed files
with
181 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Environment Variables | ||
|
||
A common way to customize the build process is to define environment variables, which can be accessed from any stage in your build process. | ||
|
||
### Public ENV variables reference | ||
|
||
- `ABSTRUSE_BRANCH` | ||
- for push builds, or builds not triggered by a pull request, this is the name of the branch. | ||
- for builds triggered by a pull request this is the name of the branch targeted by the pull request. | ||
- for builds triggered by a tag, this is the same as the name of the tag (`ABSTRUSE_TAG`) | ||
|
||
- `ABSTRUSE_BUILD_DIR` | ||
- The absolute path to the directory where the repository being built has been copied on the worker. | ||
|
||
- `ABSTRUSE_BUILD_ID` | ||
- The id of the current build that Abstruse CI uses internally. | ||
|
||
- `ABSTRUSE_JOB_ID` | ||
- The if of the current job that Abstruse CI uses internally. | ||
|
||
- `ABSTRUSE_COMMIT` | ||
- The commit that the current build is testing. | ||
|
||
- `ABSTRUSE_EVENT_TYPE` | ||
- Indicates how the build was triggered. One of `push` or `pull_request` | ||
|
||
- `ABSTRUSE_PULL_REQUEST` | ||
- The pull request number if the current job is a pull request, “false” if it’s not a pull request. | ||
|
||
- `ABSTRUSE_PULL_REQUEST_BRANCH` | ||
- if the current job is a pull request, the name of the branch from which the PR originated. | ||
- if the current job is a push build, this variable is empty (`""`) | ||
|
||
- `ABSTRUSE_TAG` | ||
- If the current build is for a git tag, this variable is set to the tag’s name. | ||
|
||
- `ABSTRUSE_PULL_REQUEST_SHA` | ||
- if the current job is a pull request, the commit SHA of the HEAD commit of the PR. | ||
- if the current job is a push build, this variable is empty (`""`) | ||
|
||
- `ABSTRUSE_SECURE_ENV_VARS` | ||
- Set to `true` if there are any encrypted environment variables. | ||
- Set to `false` if no encrypted environment variables are available. | ||
|
||
- `ABSTRUSE_TEST_RESULT` | ||
- is set to `0` if the build is successful and `1-255` if the build is broken. | ||
- this variable is available only since `test` command is executed | ||
|
||
### Define public ENV variables in .abstruse.yml | ||
|
||
You can define multiple ENV variables per item. | ||
|
||
```yml | ||
matrix: | ||
- env: SCRIPT=lint NODE_VERSION=8 | ||
- env: SCRIPT=test NODE_VERSION=8 | ||
- env: SCRIPT=test:e2e NODE_VERSION=8 | ||
- env: SCRIPT=test:protractor NODE_VERSION=8 | ||
- env: SCRIPT=test:karma NODE_VERSION=8 | ||
``` | ||
### Define variables public and encrypted variables under repository | ||
Variables defined in repository settings are the same for all builds, and when you restart an old build, it uses the latest values. These variables are not automatically available to forks. | ||
Define variables in the Repository Settings that: | ||
- differ per repository. | ||
- contain sensitive data, such as third-party credentials (encrypted variables). | ||
<img src="https://user-images.githubusercontent.com/1796022/34071301-9d4e4d04-e274-11e7-8be7-57f411d3f93f.png"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export interface EnvVariables { | ||
[key: string]: string | number | boolean; | ||
} | ||
|
||
export function set(envs: EnvVariables, key: string, value: string | number | boolean): void { | ||
envs[key] = value; | ||
} | ||
|
||
export function unset(envs: EnvVariables, key: string): void { | ||
envs[key] = null; | ||
} | ||
|
||
export function serialize(envs: EnvVariables): string[] { | ||
return Object.keys(envs).map(key => `${key}=${envs[key]}`); | ||
} | ||
|
||
export function unserialize(envs: string[]): EnvVariables { | ||
return envs.reduce((acc, curr) => { | ||
const splitted = curr.split('='); | ||
acc = Object.assign({}, acc, { [splitted[0]]: splitted[1] }); | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
export function generate(data: any): EnvVariables { | ||
const envs = init(); | ||
const request = data.requestData; | ||
const commit = request.data.pull_request && request.data.pull_request.head | ||
&& request.data.pull_request.head.sha || | ||
request.data.head_commit && request.data.head_commit.id || | ||
request.data.sha || | ||
request.data.object_attributes && request.data.object_attributes.last_commit && | ||
request.data.object_attributes.last_commit.id || | ||
request.data.push && request.data.push.changes[0].commits[0].hash || | ||
request.data.pullrequest && request.data.pullrequest.source && | ||
request.data.pullrequest.source.commit && | ||
request.data.pullrequest.source.commit.hash || | ||
request.data.commit || ''; | ||
const tag = request.ref && request.ref.startsWith('refs/tags/') ? | ||
request.ref.replace('refs/tags/', '') : null; | ||
|
||
set(envs, 'ABSTRUSE_BRANCH', request.branch); | ||
set(envs, 'ABSTRUSE_BUILD_ID', data.build_id); | ||
set(envs, 'ABSTRUSE_JOB_ID', data.job_id); | ||
set(envs, 'ABSTRUSE_COMMIT', commit); | ||
set(envs, 'ABSTRUSE_EVENT_TYPE', request.pr ? 'pull_request' : 'push'); | ||
set(envs, 'ABSTRUSE_PULL_REQUEST', request.pr ? request.pr : false); | ||
set(envs, 'ABSTRUSE_PULL_REQUEST_BRANCH', request.pr ? request.branch : ''); | ||
set(envs, 'ABSTRUSE_TAG', tag); | ||
|
||
const prSha = request.pr ? commit : ''; | ||
set(envs, 'ABSTRUSE_PULL_REQUEST_SHA', prSha); | ||
|
||
return envs; | ||
} | ||
|
||
function init(): EnvVariables { | ||
return [ | ||
'ABSTRUSE_BRANCH', 'ABSTRUSE_BUILD_DIR', 'ABSTRUSE_BUILD_ID', | ||
'ABSTRUSE_JOB_ID', 'ABSTRUSE_COMMIT', 'ABSTRUSE_EVENT_TYPE', | ||
'ABSTRUSE_PULL_REQUEST', 'ABSTRUSE_PULL_REQUEST_BRANCH', | ||
'ABSTRUSE_TAG', 'ABSTRUSE_PULL_REQEUST_SHA', 'ABSTRUSE_SECURE_ENV_VARS', | ||
'ABSTRUSE_TEST_RESULT' | ||
].reduce((acc, curr) => { | ||
acc = Object.assign(acc, { [curr]: null }); | ||
return acc; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters