From 8df5e2bdd33a620e683f3adabe174e94ceaa88d9 Mon Sep 17 00:00:00 2001 From: Casey Vockrodt Date: Mon, 19 Nov 2018 22:35:16 -0700 Subject: [PATCH] feat(CI checks): Add support for GitLab CI checks Check `GITLAB_CI` environment variable and then verify `CI_COMMIT_REF_NAME` matches the given branch. Includes tests Closes #88 re #32 --- docs/automatic-releases/index.rst | 8 ++++++++ semantic_release/ci_checks.py | 14 ++++++++++++++ tests/ci_checks/test_gitlab.py | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/ci_checks/test_gitlab.py diff --git a/docs/automatic-releases/index.rst b/docs/automatic-releases/index.rst index b9ece4431..5b0c2ec66 100644 --- a/docs/automatic-releases/index.rst +++ b/docs/automatic-releases/index.rst @@ -43,6 +43,14 @@ Checks for circle-ci to ensure that the build is not a pull-request and on the c The branch check, checks against the branch that circle-ci said it checked out, not the current branch. +GitLab CI +^^^^^^^^ +*Condition:* Environment variable ``GITLAB_CI`` is ``'true'`` + +Checks for gitlab-ci to ensure that the build is on the correct branch. +The branch check, checks against the branch that gitlab-ci said it checked out, not the current +branch. + Publish with CI ~~~~~~~~~~~~~~~ Add ``python setup.py publish`` or ``semantic-release publish`` as an after success task on your diff --git a/semantic_release/ci_checks.py b/semantic_release/ci_checks.py index 4223c7b7f..17b247553 100644 --- a/semantic_release/ci_checks.py +++ b/semantic_release/ci_checks.py @@ -72,6 +72,18 @@ def circle(branch): assert not os.environ.get('CI_PULL_REQUEST') +@checker +def gitlab(branch): + """ + Performs necessary checks to ensure that the gitlab build is one + that should create releases. + + :param branch: The branch the environment should be running against. + """ + assert os.environ.get('CI_COMMIT_REF_NAME') == branch + # TODO - don't think there's a merge request indicator variable + + def check(branch='master'): """ Detects the current CI environment, if any, and performs necessary @@ -88,3 +100,5 @@ def check(branch='master'): return frigg(branch) elif os.environ.get('CIRCLECI') == 'true': return circle(branch) + elif os.environ.get('GITLAB_CI') == 'true': + return gitlab(branch) diff --git a/tests/ci_checks/test_gitlab.py b/tests/ci_checks/test_gitlab.py new file mode 100644 index 000000000..04fc4fe2d --- /dev/null +++ b/tests/ci_checks/test_gitlab.py @@ -0,0 +1,23 @@ +import pytest + +from semantic_release import ci_checks +from semantic_release.errors import CiVerificationError + + +def test_gitlab_should_pass_if_branch_is_master(monkeypatch): + monkeypatch.setenv('CI_COMMIT_REF_NAME', 'master') + + assert ci_checks.gitlab('master') + + +def test_gitlab_should_pass_if_branch_is_correct(monkeypatch): + monkeypatch.setenv('CI_COMMIT_REF_NAME', 'other-branch') + + assert ci_checks.gitlab('other-branch') + + +def test_gitlab_should_raise_ci_verification_error_for_wrong_branch(monkeypatch): + monkeypatch.setenv('CI_COMMIT_REF_NAME', 'other-branch') + + with pytest.raises(CiVerificationError): + ci_checks.gitlab('master')