|
2 | 2 |
|
3 | 3 | Enforce commit conventions with confidence by linting on your CI servers with `commitlint`. |
4 | 4 |
|
5 | | - We'll use TravisCI for this guide but the principles are valid for any CI server. |
6 | | - |
7 | | -## Install |
| 5 | +?> This guide assumes you have a already configured `commitlint` for local usage. Follow the [Getting Started](readme.md#getting-started) for basic installation and configuration instructions. |
8 | 6 |
|
9 | 7 | ```bash |
10 | | -# Create a git repository if needed |
11 | | -git init |
12 | | - |
13 | | -# Create a package.json if needed |
14 | | -npm init |
15 | | - |
16 | 8 | # Install and configure if needed |
17 | | -npm install --save-dev @commitlint/{config-conventional,cli} |
18 | | -echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js |
19 | | -``` |
20 | | - |
21 | | -Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`. |
22 | | - |
23 | | -## First test run with Travis |
24 | | - |
25 | | -Add a `.travis.yml` to your project root |
26 | | - |
27 | | -```yaml |
28 | | -# .travis.yml |
29 | | -language: node_js |
30 | | -before_install: git fetch --unshallow |
31 | | -script: |
32 | | - - ./node_modules/.bin/commitlint --from=HEAD~1 |
33 | | - - npm test |
| 9 | +npm install --save-dev @commitlint/travis-cli |
34 | 10 | ``` |
35 | 11 |
|
36 | | -Make sure Travis is connected to your git repository. |
37 | | -Trigger a build by pushing to your repository. |
38 | | -
|
39 | | -```bash |
40 | | -git add . |
41 | | -git commit -m "add travis stuff" |
42 | | -``` |
43 | | - |
44 | | -We expect this build to fail: |
45 | | - |
46 | | -```yaml |
47 | | -... |
48 | | -./node_modules/.bin/commitlint --from=HEAD~1 |
49 | | -⧗ input: add travis stuff |
50 | | -✖ message may not be empty [subject-empty] |
51 | | -✖ type may not be empty [type-empty] |
52 | | -✖ found 2 problems, 0 warnings |
53 | | -``` |
54 | | - |
55 | | -## Linting relevant commits |
56 | | - |
57 | | -What we did so far works but is not very useful as it simply lints the last commit in history. |
58 | | -Let's change that by using environment information provided by TravisCI. |
59 | | - |
60 | | -Every build exposes the commit that triggered the build via `TRAVIS_COMMIT`. |
61 | | - |
62 | | -```yaml |
63 | | -# .travis.yml |
| 12 | +```yml |
| 13 | +# travis.yml |
64 | 14 | language: node_js |
65 | | -before_install: git fetch --unshallow |
66 | 15 | script: |
67 | | - - ./node_modules/.bin/commitlint --from=$TRAVIS_COMMIT |
68 | | - - npm test |
69 | | -``` |
70 | | -
|
71 | | -That's a bit better, but we are not handling branches at all yet. Travis provides the branch we are on via `TRAVIS_BRANCH`. |
72 | | - |
73 | | -```yaml |
74 | | -# .travis.yml |
75 | | -language: node_js |
76 | | -before_install: git fetch --unshallow |
77 | | -script: |
78 | | - - ./node_modules/.bin/commitlint --from="$TRAVIS_BRANCH" --to="$TRAVIS_COMMIT" |
79 | | - - ./node_modules/.bin/commitlint --from=$TRAVIS_COMMIT |
80 | | - - npm test |
81 | | -``` |
82 | | - |
83 | | -Nice. This handles direct commits and PR originating from the same repository. Let's add forks to the mix. |
84 | | - |
85 | | -## The full scripts |
86 | | - |
87 | | -We'll have to differentiate between forks and same-repo PRs on our own and move the linting to a dedicated script. |
88 | | - |
89 | | -```yaml |
90 | | -# .travis.yml |
91 | | -language: node_js |
92 | | -before_install: git fetch --unshallow |
93 | | -script: |
94 | | - - /bin/bash lint-commits.sh |
95 | | - - npm test |
96 | | -``` |
97 | | - |
98 | | -```bash |
99 | | -# lint-commits.sh |
100 | | -#!/bin/bash |
101 | | -set -e |
102 | | -set -u |
103 | | -
|
104 | | -if [[ $TRAVIS_PULL_REQUEST_SLUG != "" && $TRAVIS_PULL_REQUEST_SLUG != $TRAVIS_REPO_SLUG ]]; then |
105 | | - # This is a Pull Request from a different slug, hence a forked repository |
106 | | - git remote add "$TRAVIS_PULL_REQUEST_SLUG" "https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git" |
107 | | - git fetch "$TRAVIS_PULL_REQUEST_SLUG" |
108 | | -
|
109 | | - # Use the fetched remote pointing to the source clone for comparison |
110 | | - TO="$TRAVIS_PULL_REQUEST_SLUG/$TRAVIS_PULL_REQUEST_BRANCH" |
111 | | -else |
112 | | - # This is a Pull Request from the same remote, no clone repository |
113 | | - TO=$TRAVIS_COMMIT |
114 | | -fi |
115 | | -
|
116 | | -# Lint all commits in the PR |
117 | | -# - Covers fork pull requests (when TO=slug/branch) |
118 | | -# - Covers branch pull requests (when TO=branch) |
119 | | -./node_modules/.bin/commitlint --from="$TRAVIS_BRANCH" --to="$TO" |
120 | | -
|
121 | | -# Always lint the triggering commit |
122 | | -# - Covers direct commits |
123 | | -./node_modules/.bin/commitlint --from="$TRAVIS_COMMIT" |
| 16 | + - $(npm bin)/travis-cli |
124 | 17 | ``` |
125 | 18 |
|
126 | 19 | ?> Help yourself adopting a commit convention by using an interactive commit prompt. Learn how to use `@commitlint/prompt-cli` in the [Use prompt guide](guides-use-prompt.md) |
0 commit comments