|
| 1 | +# GitLab CI, Bitbucket and Azure Pipelines |
| 2 | + |
| 3 | +On GitHub, the [Action](github-actions.md) and the [App](github-app.md) do |
| 4 | +this work for you. Everywhere else Commit Check runs as one job in the |
| 5 | +pipeline: it is a Python package with no runtime dependencies, so any runner |
| 6 | +that can `pip install` can run it, and it reads the same `cchk.toml` as the |
| 7 | +hook and the Action. |
| 8 | + |
| 9 | +Two things are different from running it on your own machine, and each |
| 10 | +example below handles both: |
| 11 | + |
| 12 | +- **The checkout is a detached HEAD.** git has no branch to report, so |
| 13 | + `commit-check --branch` would judge `HEAD`, which is always allowed, and |
| 14 | + pass whatever the branch is called. Pipe the branch name in from the CI's |
| 15 | + own variable instead: a value piped into `--branch` on its own is the name |
| 16 | + it checks. |
| 17 | +- **A merge request has more than one commit.** `--rev` checks one commit, so |
| 18 | + the job loops over the commits the merge request adds, as in |
| 19 | + [Checking a range of commits](../example.md#checking-a-range-of-commits). |
| 20 | + The loop needs the history those commits sit on, so each example turns |
| 21 | + shallow cloning off. If the range cannot be read, the job fails rather than |
| 22 | + passing with nothing checked. |
| 23 | + |
| 24 | +Each job runs on merge or pull requests only, because the variables it reads |
| 25 | +exist only there. Add `--author-name --author-email` to the `commit-check` |
| 26 | +call in the loop to check each commit's author as well. |
| 27 | + |
| 28 | +## GitLab CI |
| 29 | + |
| 30 | +```yaml title=".gitlab-ci.yml" |
| 31 | +commit-check: |
| 32 | + image: python:3.13 |
| 33 | + variables: |
| 34 | + GIT_DEPTH: "0" |
| 35 | + rules: |
| 36 | + - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 37 | + script: |
| 38 | + - pip install commit-check |
| 39 | + - echo "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" | commit-check --branch |
| 40 | + - | |
| 41 | + head="${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA:-$CI_COMMIT_SHA}" |
| 42 | + shas=$(git rev-list "$CI_MERGE_REQUEST_DIFF_BASE_SHA..$head") || exit 1 |
| 43 | + status=0 |
| 44 | + for sha in $shas; do |
| 45 | + commit-check --message --rev "$sha" --compact || status=1 |
| 46 | + done |
| 47 | + exit $status |
| 48 | +``` |
| 49 | +
|
| 50 | +- `GIT_DEPTH: "0"` turns shallow cloning off. |
| 51 | +- In a merged results pipeline, `HEAD` is a merge commit GitLab made, and |
| 52 | + `CI_MERGE_REQUEST_SOURCE_BRANCH_SHA` names the merge request's own last |
| 53 | + commit. In a plain merge request pipeline that variable is empty and |
| 54 | + `CI_COMMIT_SHA` is that commit. |
| 55 | +- The `python` image ships with git. A `-slim` image does not, and needs it |
| 56 | + installed first. |
| 57 | + |
| 58 | +## Bitbucket Pipelines |
| 59 | + |
| 60 | +```yaml title="bitbucket-pipelines.yml" |
| 61 | +image: python:3.13 |
| 62 | +
|
| 63 | +pipelines: |
| 64 | + pull-requests: |
| 65 | + '**': |
| 66 | + - step: |
| 67 | + name: Commit Check |
| 68 | + clone: |
| 69 | + depth: full |
| 70 | + script: |
| 71 | + - pip install commit-check |
| 72 | + - echo "$BITBUCKET_BRANCH" | commit-check --branch |
| 73 | + - | |
| 74 | + shas=$(git rev-list "$BITBUCKET_PR_DESTINATION_COMMIT..$BITBUCKET_COMMIT") || exit 1 |
| 75 | + status=0 |
| 76 | + for sha in $shas; do |
| 77 | + commit-check --message --rev "$sha" --compact || status=1 |
| 78 | + done |
| 79 | + exit $status |
| 80 | +``` |
| 81 | + |
| 82 | +- `depth: full` turns off the default clone depth of 50 commits. |
| 83 | +- Bitbucket merges the destination branch into the pull request before the |
| 84 | + step runs. The range stops at `BITBUCKET_COMMIT`, the pull request's own |
| 85 | + last commit, so commits that only came in with that merge are not checked. |
| 86 | + |
| 87 | +## Azure Pipelines |
| 88 | + |
| 89 | +```yaml title="azure-pipelines.yml" |
| 90 | +trigger: none |
| 91 | +
|
| 92 | +pr: |
| 93 | + branches: |
| 94 | + include: ["*"] |
| 95 | +
|
| 96 | +pool: |
| 97 | + vmImage: ubuntu-latest |
| 98 | +
|
| 99 | +steps: |
| 100 | + - checkout: self |
| 101 | + fetchDepth: 0 |
| 102 | + - task: UsePythonVersion@0 |
| 103 | + inputs: |
| 104 | + versionSpec: "3.13" |
| 105 | + - script: pip install commit-check |
| 106 | + displayName: Install Commit Check |
| 107 | + - script: echo "${SYSTEM_PULLREQUEST_SOURCEBRANCH#refs/heads/}" | commit-check --branch |
| 108 | + displayName: Check the branch name |
| 109 | + - script: | |
| 110 | + shas=$(git rev-list HEAD^1..HEAD^2) || exit 1 |
| 111 | + status=0 |
| 112 | + for sha in $shas; do |
| 113 | + commit-check --message --rev "$sha" --compact || status=1 |
| 114 | + done |
| 115 | + exit $status |
| 116 | + displayName: Check the commit messages |
| 117 | +``` |
| 118 | + |
| 119 | +- `trigger: none` keeps the pipeline to pull requests; on a plain push there |
| 120 | + is no source branch to check and no merge commit to read the range from. |
| 121 | +- `fetchDepth: 0` turns off shallow fetch, which new pipelines have on by |
| 122 | + default. |
| 123 | +- A pull request build checks out a merge commit whose first parent is the |
| 124 | + target branch and second the pull request, so `HEAD^1..HEAD^2` is exactly |
| 125 | + the commits the pull request adds. |
| 126 | +- `System.PullRequest.SourceBranch` is `refs/heads/feature/x` in Azure Repos |
| 127 | + and `feature/x` for a GitHub repository; `#refs/heads/` strips the prefix |
| 128 | + where there is one. |
| 129 | +- In Azure Repos the `pr:` section is ignored. Pull request builds come from a |
| 130 | + [build validation branch policy](https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies#set-build-validation) |
| 131 | + on the target branch, and `System.PullRequest.SourceBranch` is only set for |
| 132 | + builds a branch policy started. |
0 commit comments