Skip to content

Commit

Permalink
fix: updates to ci params sent to cloud for jenkins (#25036)
Browse files Browse the repository at this point in the history
* feat: updates to ci params sent to cloud for jenkins

* fix: add more context around git commits via change trigger for jenkins pipelines

* chore: test cleanup
  • Loading branch information
davidr-cy committed Dec 29, 2022
1 parent a693687 commit 285e2b3
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 42 deletions.
34 changes: 29 additions & 5 deletions packages/server/lib/util/ci_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ const _detectProviderName = () => {
})
}

// User provided environment variables are used to allow users to define their own
// values should the CI provider not have an existing or correct mapping from the list below.
const _userProvidedProviderCiParams = () => {
return extract([
'CYPRESS_PULL_REQUEST_ID',
'CYPRESS_PULL_REQUEST_URL',
'CYPRESS_CI_BUILD_URL',
])
}
// TODO: don't forget about buildNumber!
// look at the old commit that was removed to see how we did it
const _providerCiParams = () => {
Expand Down Expand Up @@ -299,11 +308,21 @@ const _providerCiParams = () => {
'SHORT_SHA',
// https://cloud.google.com/cloud-build/docs/api/reference/rest/Shared.Types/Build
]),
/**
* References:
* https://ci.eclipse.org/webtools/env-vars.html/
* https://www.jenkins.io/doc/book/pipeline/multibranch/#additional-environment-variables
*/
jenkins: extract([
'BUILD_ID',
'BUILD_URL',
'BUILD_NUMBER',
'ghprbPullId',
// Jenkins pipeline options change options
'CHANGE_ID',
'CHANGE_URL',
'CHANGE_TARGET',
'CHANGE_TITLE',
]),
// https://semaphoreci.com/docs/available-environment-variables.html
// some come from v1, some from v2 of semaphore
Expand Down Expand Up @@ -539,10 +558,10 @@ const _providerCommitParams = () => {
},
jenkins: {
sha: env.GIT_COMMIT,
branch: env.GIT_BRANCH,
// message: ???
// authorName: ???
// authorEmail: ???
branch: env.GIT_BRANCH || env.BRANCH_NAME || env.CHANGE_BRANCH,
// message: ??,
authorName: env.GIT_AUTHOR_NAME || env.CHANGE_AUTHOR_DISPLAY_NAME,
authorEmail: env.GIT_AUTHOR_EMAIL || env.CHANGE_AUTHOR_EMAIL,
// remoteOrigin: ???
// defaultBranch: ???
},
Expand Down Expand Up @@ -617,7 +636,12 @@ const _get = (fn) => {
}

const ciParams = () => {
return _get(_providerCiParams)
const ciParams = {
..._.chain(_userProvidedProviderCiParams()).thru(omitUndefined).defaultTo(null).value(),
..._get(_providerCiParams),
}

return Object.keys(ciParams).length > 0 ? ciParams : null
}

const commitParams = () => {
Expand Down
163 changes: 126 additions & 37 deletions packages/server/test/unit/util/ci_provider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ describe('lib/util/ci_provider', () => {
return expectsCommitParams(null)
})

it('allows for user provided environment variables', () => {
resetEnv = mockedEnv({
CYPRESS_PULL_REQUEST_ID: 'cypressPullRequestId',
CYPRESS_PULL_REQUEST_URL: 'cypressPullRequestUrl',
CYPRESS_CI_BUILD_URL: 'cypressCiBuildUrl',
}, { clear: true })

expectsName(null)
expectsCiParams({
cypressPullRequestId: 'cypressPullRequestId',
cypressPullRequestUrl: 'cypressPullRequestUrl',
cypressCiBuildUrl: 'cypressCiBuildUrl',
})

return expectsCommitParams(null)
})

it('does not extract from commit environment variables yet', () => {
// see fallback environment variables
// https://github.com/cypress-io/commit-info#fallback-environment-variables
Expand Down Expand Up @@ -733,55 +750,127 @@ describe('lib/util/ci_provider', () => {
return expectsName('googleCloud')
})

it('jenkins', () => {
resetEnv = mockedEnv({
JENKINS_URL: 'true',
describe('jenkins', () => {
it('with legacy env', () => {
resetEnv = mockedEnv({
JENKINS_URL: 'true',

BUILD_ID: 'buildId',
BUILD_URL: 'buildUrl',
BUILD_NUMBER: 'buildNumber',
ghprbPullId: 'gbprbPullId',
BUILD_ID: 'buildId',
BUILD_URL: 'buildUrl',
BUILD_NUMBER: 'buildNumber',
ghprbPullId: 'gbprbPullId',

GIT_COMMIT: 'gitCommit',
GIT_BRANCH: 'gitBranch',
}, { clear: true })
GIT_COMMIT: 'gitCommit',
GIT_BRANCH: 'gitBranch',
GIT_AUTHOR_NAME: 'gitAuthorName',
GIT_AUTHOR_EMAIL: 'gitAuthorEmail',
}, { clear: true })

expectsName('jenkins')
expectsCiParams({
buildId: 'buildId',
buildUrl: 'buildUrl',
buildNumber: 'buildNumber',
ghprbPullId: 'gbprbPullId',
})
expectsName('jenkins')
expectsCiParams({
buildId: 'buildId',
buildUrl: 'buildUrl',
buildNumber: 'buildNumber',
ghprbPullId: 'gbprbPullId',
})

expectsCommitParams({
sha: 'gitCommit',
branch: 'gitBranch',
})
expectsCommitParams({
sha: 'gitCommit',
branch: 'gitBranch',
authorName: 'gitAuthorName',
authorEmail: 'gitAuthorEmail',
})

resetEnv = mockedEnv({
JENKINS_HOME: '/path/to/jenkins',
}, { clear: true })
resetEnv = mockedEnv({
JENKINS_HOME: '/path/to/jenkins',
}, { clear: true })

expectsName('jenkins')
expectsName('jenkins')

resetEnv = mockedEnv({
JENKINS_VERSION: '1.2.3',
}, { clear: true })
resetEnv = mockedEnv({
JENKINS_VERSION: '1.2.3',
}, { clear: true })

expectsName('jenkins')
expectsName('jenkins')

resetEnv = mockedEnv({
HUDSON_HOME: '/path/to/jenkins',
}, { clear: true })
resetEnv = mockedEnv({
HUDSON_HOME: '/path/to/jenkins',
}, { clear: true })

expectsName('jenkins')
expectsName('jenkins')

resetEnv = mockedEnv({
HUDSON_URL: 'true',
}, { clear: true })
resetEnv = mockedEnv({
HUDSON_URL: 'true',
}, { clear: true })

return expectsName('jenkins')
})

it('with change request params (PR Scenario)', () => {
resetEnv = mockedEnv({
JENKINS_URL: 'true',

BUILD_ID: 'buildId',
BUILD_NUMBER: 'buildNumber',
CHANGE_BRANCH: 'changeBranch',
CYPRESS_CI_BUILD_URL: 'cypressCiBuildUrl',

GIT_COMMIT: 'gitCommit',
CHANGE_ID: 'changeId',
CHANGE_URL: 'changeUrl',
CHANGE_TITLE: 'changeTitle',
CHANGE_TARGET: 'changeTarget',
CHANGE_AUTHOR_DISPLAY_NAME: 'changeAuthorDisplayName',
CHANGE_AUTHOR_EMAIL: 'changeAuthorEmail',
}, { clear: true })

expectsName('jenkins')
expectsCiParams({
buildId: 'buildId',
buildNumber: 'buildNumber',
cypressCiBuildUrl: 'cypressCiBuildUrl',
changeId: 'changeId',
changeTitle: 'changeTitle',
changeUrl: 'changeUrl',
changeTarget: 'changeTarget',
})

return expectsCommitParams({
sha: 'gitCommit',
branch: 'changeBranch',
authorName: 'changeAuthorDisplayName',
authorEmail: 'changeAuthorEmail',
})
})

return expectsName('jenkins')
it('with userProvided', () => {
resetEnv = mockedEnv({
JENKINS_URL: 'true',

BUILD_ID: 'buildId',
BUILD_NUMBER: 'buildNumber',
CYPRESS_PULL_REQUEST_ID: 'cypressPullRequestId',
CYPRESS_PULL_REQUEST_URL: 'cypressPullRequestUrl',
CYPRESS_CI_BUILD_URL: 'cypressCiBuildUrl',

GIT_COMMIT: 'gitCommit',
GIT_BRANCH: 'gitBranch',
}, { clear: true })

expectsName('jenkins')
expectsCiParams({
buildId: 'buildId',
buildNumber: 'buildNumber',
cypressPullRequestId: 'cypressPullRequestId',
cypressPullRequestUrl: 'cypressPullRequestUrl',
cypressCiBuildUrl: 'cypressCiBuildUrl',
})

return expectsCommitParams({
sha: 'gitCommit',
branch: 'gitBranch',
})
})
})

it('semaphore', () => {
Expand Down

4 comments on commit 285e2b3

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 285e2b3 Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.2.1/linux-arm64/develop-285e2b3b741def566db5620f8397053304588227/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 285e2b3 Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.2.1/linux-x64/develop-285e2b3b741def566db5620f8397053304588227/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 285e2b3 Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.2.1/darwin-arm64/develop-285e2b3b741def566db5620f8397053304588227/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 285e2b3 Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.2.1/darwin-x64/develop-285e2b3b741def566db5620f8397053304588227/cypress.tgz

Please sign in to comment.