|
| 1 | +# Continuous Integration |
| 2 | + |
| 3 | +One of the best ways to keep your project bug free is through a test suite, but it's easy to forget |
| 4 | +to run tests all the time. |
| 5 | + |
| 6 | +That's where Continuous Integration (CI) servers come in. |
| 7 | +You can set up your project repository so that your tests run on every commit and pull request. |
| 8 | + |
| 9 | +There are paid CI services like [Circle CI](https://circleci.com/) and |
| 10 | +[Travis CI](https://travis-ci.com/), and you can also host your own for free using |
| 11 | +[Jenkins](https://jenkins.io/) and others. |
| 12 | + |
| 13 | +Even though Circle CI and Travis CI are paid services, they are provided free for open source |
| 14 | +projects. |
| 15 | +You can create a public project on GitHub and add these services without paying. |
| 16 | + |
| 17 | +We're going to see how to update your test configuration to run in CI environments, and how to |
| 18 | +set up Circle CI and Travis CI. |
| 19 | + |
| 20 | + |
| 21 | +## Update test configuration |
| 22 | + |
| 23 | +Even though `ng test` and `ng e2e` already run on your environment, they need to be adjusted to |
| 24 | +run in CI environments. |
| 25 | + |
| 26 | +When using Chrome in CI environments it has to be started without sandboxing. |
| 27 | +We can achieve that by editing our test configs. |
| 28 | + |
| 29 | +In `karma.conf.js`, add a custom launcher called `ChromeNoSandbox` below `browsers`: |
| 30 | + |
| 31 | +``` |
| 32 | +browsers: ['Chrome'], |
| 33 | +customLaunchers: { |
| 34 | + ChromeNoSandbox: { |
| 35 | + base: 'Chrome', |
| 36 | + flags: ['--no-sandbox'] |
| 37 | + } |
| 38 | +}, |
| 39 | +``` |
| 40 | + |
| 41 | +Create a new file in the root of your project called `protractor-ci.conf.js`, that extends |
| 42 | +the original `protractor.conf.js`: |
| 43 | + |
| 44 | +``` |
| 45 | +const config = require('./protractor.conf').config; |
| 46 | +
|
| 47 | +config.capabilities = { |
| 48 | + browserName: 'chrome', |
| 49 | + chromeOptions: { |
| 50 | + args: ['--no-sandbox'] |
| 51 | + } |
| 52 | +}; |
| 53 | +
|
| 54 | +exports.config = config; |
| 55 | +``` |
| 56 | + |
| 57 | +Now you can run the following commands to use the `--no-sandbox` flag: |
| 58 | + |
| 59 | +``` |
| 60 | +ng test --single-run --no-progress --browser=ChromeNoSandbox |
| 61 | +ng e2e --no-progress --config=protractor-ci.conf.js |
| 62 | +``` |
| 63 | + |
| 64 | +For CI environments it's also a good idea to disable progress reporting (via `--no-progress`) |
| 65 | +to avoid spamming the server log with progress messages. |
| 66 | + |
| 67 | + |
| 68 | +## Using Circle CI |
| 69 | + |
| 70 | +Create a folder called `.circleci` at the project root, and inside of it create a file called |
| 71 | +`config.yml`: |
| 72 | + |
| 73 | +```yaml |
| 74 | +version: 2 |
| 75 | +jobs: |
| 76 | + build: |
| 77 | + working_directory: ~/my-project |
| 78 | + docker: |
| 79 | + - image: circleci/node:6-browsers |
| 80 | + steps: |
| 81 | + - checkout |
| 82 | + - restore_cache: |
| 83 | + key: my-project-{{ .Branch }}-{{ checksum "package.json" }} |
| 84 | + - run: npm install |
| 85 | + - save_cache: |
| 86 | + key: my-project-{{ .Branch }}-{{ checksum "package.json" }} |
| 87 | + paths: |
| 88 | + - "node_modules" |
| 89 | + - run: xvfb-run -a npm run test -- --single-run --no-progress --browser=ChromeNoSandbox |
| 90 | + - run: xvfb-run -a npm run e2e -- --no-progress --config=protractor-ci.conf.js |
| 91 | + |
| 92 | +``` |
| 93 | + |
| 94 | +We're doing a few things here: |
| 95 | + - |
| 96 | + - `node_modules` is cached. |
| 97 | + - [npm run](https://docs.npmjs.com/cli/run-script) is used to run `ng` because `@angular/cli` is |
| 98 | + not installed globally. The double dash (`--`) is needed to pass arguments into the npm script. |
| 99 | + - `xvfb-run` is used to run `npm run` to run a command using a virtual screen, which is needed by |
| 100 | + Chrome. |
| 101 | + |
| 102 | +Commit your changes and push them to your repository. |
| 103 | + |
| 104 | +Next you'll need to [sign up for Circle CI](https://circleci.com/docs/2.0/first-steps/) and |
| 105 | +[add your project](https://circleci.com/add-projects). |
| 106 | +Your project should start building. |
| 107 | + |
| 108 | +Be sure to check out the [Circle CI docs](https://circleci.com/docs/2.0/) if you want to know more. |
| 109 | + |
| 110 | + |
| 111 | +## Using Travis CI |
| 112 | + |
| 113 | +Create a file called `.travis.yml` at the project root: |
| 114 | + |
| 115 | +```yaml |
| 116 | +dist: trusty |
| 117 | +sudo: false |
| 118 | + |
| 119 | +language: node_js |
| 120 | +node_js: |
| 121 | + - "6" |
| 122 | + |
| 123 | +cache: |
| 124 | + directories: |
| 125 | + - ./node_modules |
| 126 | + |
| 127 | +install: |
| 128 | + - npm install |
| 129 | + |
| 130 | +script: |
| 131 | + # Use Chromium instead of Chrome. |
| 132 | + - export CHROME_BIN=chromium-browser |
| 133 | + - xvfb-run -a npm run test -- --single-run --no-progress --browser=ChromeNoSandbox |
| 134 | + - xvfb-run -a npm run e2e -- --no-progress --config=protractor-ci.conf.js |
| 135 | + |
| 136 | +``` |
| 137 | + |
| 138 | +Although the syntax is different, we're mostly doing the same steps as were done in the |
| 139 | +Circle CI config. |
| 140 | +The only difference is that Travis doesn't come with Chrome, so we use Chromium instead. |
| 141 | + |
| 142 | +Commit your changes and push them to your repository. |
| 143 | + |
| 144 | +Next you'll need to [sign up for Travis CI](https://travis-ci.org/auth) and |
| 145 | +[add your project](https://travis-ci.org/profile). |
| 146 | +You'll need to push a new commit to trigger a build. |
| 147 | + |
| 148 | +Be sure to check out the [Travis CI docs](https://docs.travis-ci.com/) if you want to know more. |
0 commit comments