Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions samcli/lib/telemetry/cicd.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,25 @@ def _is_codeship(environ: Mapping) -> bool:
return ci_name == "codeship"


def _is_jenkins(environ: Mapping) -> bool:
"""
Use environ to determine whether it is running in Jenkins.
According to the doc,
https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#working-with-your-jenkinsfile
> BUILD_TAG
> String of jenkins-${JOB_NAME}-${BUILD_NUMBER}.
> ...
> JENKINS_URL
> Full URL of Jenkins, such as https://example.com:port/jenkins/
> (NOTE: only available if Jenkins URL set in "System Configuration")

Here firstly check JENKINS_URL's presence, if not, then fallback to check BUILD_TAG starts with "jenkins"
"""
return "JENKINS_URL" in environ or environ.get("BUILD_TAG", "").startswith("jenkins-")


_ENV_VAR_OR_CALLABLE_BY_PLATFORM: Dict[CICDPlatform, Union[str, Callable[[Mapping], bool]]] = {
# https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
CICDPlatform.Jenkins: "JENKINS_URL",
CICDPlatform.Jenkins: _is_jenkins,
Copy link
Contributor

Choose a reason for hiding this comment

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

should we do an or condition here? or is build_tag always true and present with the jenkins prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the doc, it does not have a condition.
I tested it in my own jenkins instance and can see the variable value did starts with jenkins-

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, but should we look for JENKINS_URL or BUILD_TAG with jenkins-, is there a condition under which either one may be present or not, we would want to include both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BUILD_TAG itself should suffice, but I don't mind adding a JENKINS_URL since it works today already, having it is safer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

# https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
CICDPlatform.GitLab: "GITLAB_CI",
# https://docs.github.com/en/actions/reference/environment-variables
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/lib/telemetry/test_cicd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class TestCICD(TestCase):
@parameterized.expand(
[
(CICDPlatform.Jenkins, "BUILD_TAG", "jenkins-jobname-123"),
(CICDPlatform.Jenkins, "JENKINS_URL", Mock()),
(CICDPlatform.GitLab, "GITLAB_CI", Mock()),
(CICDPlatform.GitHubAction, "GITHUB_ACTION", Mock()),
Expand All @@ -26,3 +27,12 @@ class TestCICD(TestCase):
)
def test_is_cicd_platform(self, cicd_platform, env_var, env_var_value):
self.assertTrue(_is_cicd_platform(cicd_platform, {env_var: env_var_value}))

@parameterized.expand(
[
(CICDPlatform.Jenkins, "BUILD_TAG", "not-jenkins-"),
(CICDPlatform.CodeShip, "CI_NAME", "not-CodeShip"),
]
)
def test_is_not_cicd_platform(self, cicd_platform, env_var, env_var_value):
self.assertFalse(_is_cicd_platform(cicd_platform, {env_var: env_var_value}))