Skip to content

Commit 3a9520f

Browse files
quikeEnrique Mora Parraga
andauthored
feat: add screwdriver.cd support. (#278)
Co-authored-by: Enrique Mora Parraga <[email protected]>
1 parent eab0512 commit 3a9520f

File tree

6 files changed

+162
-19
lines changed

6 files changed

+162
-19
lines changed

README.md

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ if (isCI) {
7575

7676
**Note**: Some variables can be detected only on certain CI services. See [Supported CI](#supported-ci).
7777

78-
**Note**: The `pr` and `prBranch` properties are only available for builds triggered when a Pull Request is opened/updated and not on builds triggered by a push on a branch even if that branch happens to be the branch from which the Pull Request originated.
78+
**Note**: The `pr` and `prBranch` properties are only available for builds triggered when a Pull Request is
79+
opened/updated and not on builds triggered by a push on a branch even if that branch happens to be the branch from which
80+
the Pull Request originated.
7981

8082
## Supported CI
8183

@@ -101,6 +103,7 @@ if (isCI) {
101103
| [Netlify](https://docs.netlify.com/configure-builds/environment-variables/#netlify-configuration-variables) | `netlify` | :white_check_mark: | [:warning:](#netlify) | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
102104
| [Puppet](https://puppet.com/docs/pipelines-for-apps/enterprise/environment-variable.html) | `puppet` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: |
103105
| [Sail CI](https://sail.ci/docs/environment-variables) | `sail` | :white_check_mark: | [:warning:](#sail) | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: |
106+
| [Screwdriver.cd](https://docs.screwdriver.cd/user-guide/environment-variables) | `screwdriver` | :white_check_mark: | [:warning:](#screwdriver) | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
104107
| [Scrutinizer](https://scrutinizer-ci.com/docs/build/environment-variables) | `scrutinizer` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: |
105108
| [Semaphore](https://docs.semaphoreci.com/article/12-environment-variables) | `semaphore` | :white_check_mark: | [:warning:](#semaphore) | :white_check_mark: | [:warning:](#semaphore) | :white_check_mark: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
106109
| [Shippable](http://docs.shippable.com/ci/env-vars/#stdEnv) | `shippable` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
@@ -114,9 +117,11 @@ if (isCI) {
114117

115118
:warning: See [Caveats](#caveats)
116119

117-
**Note**: Unsupported properties will always be `undefined`. For example if a Ci services doesn't support triggering builds when a Pull Request is opened/updated, `isPr` will be `undefined`.
120+
**Note**: Unsupported properties will always be `undefined`. For example if a Ci services doesn't support triggering
121+
builds when a Pull Request is opened/updated, `isPr` will be `undefined`.
118122

119-
**Note**: If none of the above CI services is detected, `commit` and `branch` are determined based on the local Git repository, and `isCi` is determined based on the `CI` environment variable.
123+
**Note**: If none of the above CI services is detected, `commit` and `branch` are determined based on the local Git
124+
repository, and `isCi` is determined based on the `CI` environment variable.
120125

121126
## API
122127

@@ -138,7 +143,8 @@ The object to read environment variables from.
138143
Type: `String`<br>
139144
Default: `process.cwd()`
140145

141-
The current working directory in which to execute `git` commands used to determine the `commit` and `branch` [Result](#result) properties in case no [supported CI](#supported-ci) is detected.
146+
The current working directory in which to execute `git` commands used to determine the `commit`
147+
and `branch` [Result](#result) properties in case no [supported CI](#supported-ci) is detected.
142148

143149
### Result
144150

@@ -150,48 +156,75 @@ Type: `Object`
150156

151157
### AWS CodeBuild
152158

153-
AWS CodeBuild doesn't provide an environment variable to determine the current Git branch being built. In addition, it clones the repository in a [detached head state](https://git-scm.com/docs/git-checkout#_detached_head) so the branch cannot be determined with `git rev-parse --abbrev-ref HEAD`.
154-
To work around this limitation, `env-ci` look for the remote branches having the same `HEAD` as the local detached `HEAD` to determine the branch from which the detached `HEAD` was created.
155-
In the rare case where there is multiple remote branches with the same `HEAD` as the local detached `HEAD`, `env-ci` will arbitrarily pick the first one. This can lead to an inaccurate `branch` value in such circumstances.
159+
AWS CodeBuild doesn't provide an environment variable to determine the current Git branch being built. In addition, it
160+
clones the repository in a [detached head state](https://git-scm.com/docs/git-checkout#_detached_head) so the branch
161+
cannot be determined with `git rev-parse --abbrev-ref HEAD`.
162+
To work around this limitation, `env-ci` look for the remote branches having the same `HEAD` as the local
163+
detached `HEAD` to determine the branch from which the detached `HEAD` was created.
164+
In the rare case where there is multiple remote branches with the same `HEAD` as the local detached `HEAD`, `env-ci`
165+
will arbitrarily pick the first one. This can lead to an inaccurate `branch` value in such circumstances.
156166

157167
### Buddy
158168

159-
For builds triggered when [a Pull Request is opened/updated](https://buddy.works/knowledge/deployments/pull-requests), Buddy doesn't provide an environment variable indicating the branch from which the Pull Request originated nor the target branch. It also build from a branch named `pull/<PR number>` so the target branch cannot be determined with a `git` command.
160-
Therefore in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` and `prBranch` properties.
169+
For builds triggered when [a Pull Request is opened/updated](https://buddy.works/knowledge/deployments/pull-requests),
170+
Buddy doesn't provide an environment variable indicating the branch from which the Pull Request originated nor the
171+
target branch. It also build from a branch named `pull/<PR number>` so the target branch cannot be determined with
172+
a `git` command.
173+
Therefore, in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` and `prBranch`
174+
properties.
161175

162176
See [feature request](https://forum.buddy.works/t/determine-pull-request-branch-with-environment-variable/911).
163177

164178
### CircleCI
165179

166-
For builds triggered when a Pull Request is opened/updated, CircleCI doesn't provide an environment variable indicating the target branch.
167-
Therefore in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property. However `prBranch` will be set.
180+
For builds triggered when a Pull Request is opened/updated, CircleCI doesn't provide an environment variable indicating
181+
the target branch.
182+
Therefore, in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property.
183+
However `prBranch` will be set.
168184

169185
See [feature request](https://discuss.circleci.com/t/create-a-circle-target-branch-envar/10022).
170186

171187
### Cloudflare Pages
172188

173-
For builds triggered when a Pull Request is opened/updated, Cloudflare Pages will re-use the branch variable for the originating branch and not provide a target. Therefore `env-ci` will not be able to determine the `prBranch` property however `branch` will always be set.
189+
For builds triggered when a Pull Request is opened/updated, Cloudflare Pages will re-use the branch variable for the
190+
originating branch and not provide a target. Therefore `env-ci` will not be able to determine the `prBranch` property
191+
however `branch` will always be set.
174192

175193
### Jenkins
176194

177-
Triggering build when a Pull Request is opened/updated is supported only via the [ghprb-plugin](https://github.com/jenkinsci/ghprb-plugin) and [gitlab-plugin](https://github.com/jenkinsci/gitlab-plugin). Therefore `env-ci` will set `isPr`, `pr` and `prBranch` and define `branch` with the Pull Request target branch only if one those plugin is used.
195+
Triggering build when a Pull Request is opened/updated is supported only via
196+
the [ghprb-plugin](https://github.com/jenkinsci/ghprb-plugin)
197+
and [gitlab-plugin](https://github.com/jenkinsci/gitlab-plugin). Therefore `env-ci` will set `isPr`, `pr` and `prBranch`
198+
and define `branch` with the Pull Request target branch only if one those plugin is used.
178199

179200
### Netlify
180201

181-
For builds triggered when a Pull Request is opened/updated, Netlify doesn't provide an environment variable indicating the target branch.
182-
Therefore in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property. However `prBranch` will be set.
202+
For builds triggered when a Pull Request is opened/updated, Netlify doesn't provide an environment variable indicating
203+
the target branch.
204+
Therefore, in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property.
205+
However `prBranch` will be set.
183206

184207
See [feature request](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402)
185208

186209
### Sail
187210

188-
For builds triggered when a Pull Request is opened/updated, Sail doesn't provide an environment variable indicating the target branch, and the one for the current branch is set to `pull/<PR number>` independently of the the branch name from which the Pull Request originated.
189-
Therefore in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` and `prBranch` properties.
211+
For builds triggered when a Pull Request is opened/updated, Sail doesn't provide an environment variable indicating the
212+
target branch, and the one for the current branch is set to `pull/<PR number>` independently of the the branch name from
213+
which the Pull Request originated.
214+
Therefore, in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` and `prBranch`
215+
properties.
190216

191217
### Semaphore
192218

193-
For builds triggered when a Pull Request is opened/updated, Semaphore 1.0 doesn't provide an environment variable indicating the target branch.
194-
Therefore in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property. However `prBranch` will be set.
219+
For builds triggered when a Pull Request is opened/updated, Semaphore 1.0 doesn't provide an environment variable
220+
indicating the target branch.
221+
Therefore, in the case of Pull Request builds, `env-ci` will not be able to determine the `branch` property.
222+
However `prBranch` will be set.
195223
On Semaphore 2.0 the `branch` and `prBranch` properties will work as expected.
196224

197225
The property `tag` is only available on Semaphore 2.0.
226+
227+
### Screwdriver
228+
229+
For builds triggered when a Pull Request is opened/updated, Screwdriver sets the `env.GIT_BRANCH` as `head:pr` branch
230+
type (Example:`origin/refs/pull/1/head:pr`) while at commit level (non PR) it does set it with the actual branch (Example: `origin/main`).

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import jenkins from "./services/jenkins.js";
1919
import netlify from "./services/netlify.js";
2020
import puppet from "./services/puppet.js";
2121
import sail from "./services/sail.js";
22+
import screwdriver from "./services/screwdriver.js";
2223
import scrutinizer from "./services/scrutinizer.js";
2324
import semaphore from "./services/semaphore.js";
2425
import shippable from "./services/shippable.js";
@@ -50,6 +51,7 @@ const services = {
5051
netlify,
5152
puppet,
5253
sail,
54+
screwdriver,
5355
scrutinizer,
5456
semaphore,
5557
shippable,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"netlify",
4949
"puppet",
5050
"sail",
51+
"screwdriver",
5152
"scrutinizer",
5253
"semaphore",
5354
"shippable",

services/screwdriver.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://docs.screwdriver.cd/user-guide/environment-variables
2+
3+
export default {
4+
detect({ env }) {
5+
return Boolean(env.SCREWDRIVER);
6+
},
7+
configuration({ env }) {
8+
const pr = env.SD_PULL_REQUEST;
9+
const isPr = Boolean(pr);
10+
11+
return {
12+
name: "Screwdriver.cd",
13+
service: "screwdriver",
14+
branch: isPr ? env.PR_BASE_BRANCH_NAME : env.GIT_BRANCH,
15+
prBranch: isPr ? env.PR_BRANCH_NAME : undefined,
16+
commit: env.SD_BUILD_SHA,
17+
build: env.SD_BUILD_ID,
18+
buildUrl: env.SD_UI_BUILD_URL,
19+
job: env.SD_JOB_ID,
20+
pr,
21+
isPr,
22+
slug: env.SD_PIPELINE_NAME,
23+
root: env.SD_ROOT_DIR,
24+
};
25+
},
26+
};

test/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ test("Sail CI", (t) => {
146146
t.is(service, "sail");
147147
});
148148

149+
test("Screwdriver.cd", (t) => {
150+
const { isCi, service } = envCi({ env: { SCREWDRIVER: "true" } });
151+
152+
t.is(isCi, true);
153+
t.is(service, "screwdriver");
154+
});
155+
149156
test("Scrutinizer", (t) => {
150157
const { isCi, service } = envCi({ env: { SCRUTINIZER: "true" } });
151158

test/services/screwdriver.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import test from "ava";
2+
import screwdriver from "../../services/screwdriver.js";
3+
4+
const env = {
5+
SCREWDRIVER: "true",
6+
SD_BUILD_SHA: "b5a94cdabf23b21303a0e6d5be5e96bd6300847a",
7+
SD_PROJECT: "259",
8+
SD_BUILD_ID: "173",
9+
SD_EVENT_ID: "1",
10+
SD_JOB_ID: "123",
11+
SD_PIPELINE_NAME: "d2lam/myPipeline",
12+
SCM_URL: "https://github.com/d2lam/myPipeline",
13+
GIT_URL: "https://github.com/d2lam/myPipeline.git",
14+
GIT_BRANCH: "my-branch",
15+
SD_ROOT_DIR: "sd/workspace",
16+
SD_SOURCE_DIR: "sd/workspace/src/github.com/d2lam/myPipeline",
17+
};
18+
19+
test("Push", (t) => {
20+
t.deepEqual(
21+
screwdriver.configuration({
22+
env: {
23+
...env,
24+
SD_EVENT_ID: "pipeline",
25+
GIT_BRANCH: "origin/main",
26+
SD_UI_BUILD_URL: `https://cd.screwdriver.cd/pipelines/${env.SD_PROJECT}/builds/${env.SD_BUILD_ID}`,
27+
},
28+
}),
29+
{
30+
name: "Screwdriver.cd",
31+
service: "screwdriver",
32+
commit: "b5a94cdabf23b21303a0e6d5be5e96bd6300847a",
33+
build: "173",
34+
buildUrl: "https://cd.screwdriver.cd/pipelines/259/builds/173",
35+
job: "123",
36+
branch: "origin/main",
37+
prBranch: undefined,
38+
pr: undefined,
39+
isPr: false,
40+
slug: "d2lam/myPipeline",
41+
root: "sd/workspace",
42+
}
43+
);
44+
});
45+
46+
test("PR", (t) => {
47+
t.deepEqual(
48+
screwdriver.configuration({
49+
env: {
50+
...env,
51+
SD_PULL_REQUEST: "1",
52+
SD_EVENT_ID: "pr",
53+
PR_BASE_BRANCH_NAME: "main",
54+
PR_BRANCH_NAME: "origin/feat/env-ci",
55+
GIT_BRANCH: "origin/refs/pull/1/head:pr",
56+
SD_UI_BUILD_URL: `https://cd.screwdriver.cd/pipelines/${env.SD_PROJECT}/builds/${env.SD_BUILD_ID}`,
57+
},
58+
}),
59+
{
60+
name: "Screwdriver.cd",
61+
service: "screwdriver",
62+
commit: "b5a94cdabf23b21303a0e6d5be5e96bd6300847a",
63+
build: "173",
64+
buildUrl: "https://cd.screwdriver.cd/pipelines/259/builds/173",
65+
job: "123",
66+
branch: "main",
67+
prBranch: "origin/feat/env-ci",
68+
pr: "1",
69+
isPr: true,
70+
slug: "d2lam/myPipeline",
71+
root: "sd/workspace",
72+
}
73+
);
74+
});

0 commit comments

Comments
 (0)