Skip to content

Commit

Permalink
feat(CI checks): Add support for GitLab CI checks
Browse files Browse the repository at this point in the history
Check `GITLAB_CI` environment variable and then verify
`CI_COMMIT_REF_NAME` matches the given branch.

Includes tests

Closes #88  re #32
  • Loading branch information
cvockrodt authored and relekang committed Nov 21, 2018
1 parent af3ad59 commit 8df5e2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/automatic-releases/index.rst
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions semantic_release/ci_checks.py
Expand Up @@ -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
Expand All @@ -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)
23 changes: 23 additions & 0 deletions 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')

0 comments on commit 8df5e2b

Please sign in to comment.