# 怎麼用 Travis CI 測試 Roles？

## 27. 怎麼用 Travis CI 測試 Roles？

在「[好的 Roles 應具備什麼？](https://ansible.drx.tw/25.how_to_share_roles_on_galaxy-1#好的-roles-應具備什麼)」一節裡，凍仁曾提過好的 Roles 應導入 [**Travis CI**](https://travis-ci.org/) 整合測試，有了持續整合 (CI) 才能確保每次提交的品質。

接下來將以 [`chusiang.helloworld`](https://galaxy.ansible.com/chusiang/helloworld/) 這個簡單的 role 為例，讓凍仁帶大家串起 [GitHub](https://github.com/), [Galaxy](https://galaxy.ansible.com/) 和 Travis CI 三個服務。

![automate\_with\_ansible\_practice-30.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-511fdf7a28e55a687d9dddd6527b49282e76ecd8%2Fautomate_with_ansible_practice-30.jpg?alt=media)

1. 撰寫一個 Role，並上傳至 GitHub。 ![2016-12-27-test-role-with-travis-1.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-c2af6138947ba9e054e2971f612581d124373415%2F2016-12-27-test-role-with-travis-1.png?alt=media) <https://github.com/chusiang/helloworld.ansible.role>
2. 於 Galaxy 網站進行匯入，詳請請參考「[怎麼在 Galaxy 匯入 Roles？](https://ansible.drx.tw/26.how_to_share_roles_on_galaxy-2#怎麼在-galaxy-匯入-roles)」一節。 ![2016-12-27-test-role-with-travis-2.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-4e62f0f0f41e425cc5e618db5110d549dfdf971c%2F2016-12-27-test-role-with-travis-2.png?alt=media) <https://galaxy.ansible.com/chusiang/helloworld/>
3. 進入 [Travis CI](https://travis-ci.org/) 網站。 ![2016-12-27-test-role-with-travis-3.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-bddc100e31fee5734958f6d805d77da3cd22cf99%2F2016-12-27-test-role-with-travis-3.png?alt=media)
4. 點擊左上角的 **✚** 整合新 Repository。 ![2016-12-27-test-role-with-travis-4.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-9d5b78b0b4564c8c982165f5793b9149977ebcc8%2F2016-12-27-test-role-with-travis-4.png?alt=media)
5. 只需三個步驟即可整合 Travis CI。 ![2016-12-27-test-role-with-travis-5.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-d5c93b79a2eb8478f3dda9fb73b5db280ad67eac%2F2016-12-27-test-role-with-travis-5.png?alt=media)
6. 將左方的開關切換至 on。
7. 於 Git Repository 裡新增 `.travis.yml`。
8. 使用 `git push` 即會觸發 Travis CI。
9. `chusiang.helloworld/.travis.yml` 的內容如下。

```yaml
---
language: python
python: "2.7"

# Use the new container infrastructure
sudo: false

# Install ansible
addons:
  apt:
    packages:
    - python-pip

install:
  # Install ansible
  - pip install ansible

  # Check ansible version
  - ansible --version

  # Create ansible.cfg with correct roles_path
  - printf '[defaults]\nroles_path=../' >ansible.cfg

script:
  # Basic role syntax check
  - ansible-playbook tests/test.yml -i tests/inventory --syntax-check

notifications:
  webhooks: https://galaxy.ansible.com/api/v1/notifications/
```

* 此例用了 `--syntax-check` 參數進行基本的語法檢測，較進階的作法則是藉由 Docker 進行測試。
* 使用 `notifications` 和 `webhooks` 發送通訊給 Galaxy。

1. 將範例 Role 左方的開關切至 on 以整合 Travis CI。 ![2016-12-27-test-role-with-travis-6.gif](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-5621eff5add27e440b09600c518681f294e4f924%2F2016-12-27-test-role-with-travis-6.gif?alt=media)
2. 建立完畢，等待觸發，這時的 build state 會是 **unknown**。 ![2016-12-27-test-role-with-travis-7.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-27128845eb9ab05e25f5b680295594ffe16d9b88%2F2016-12-27-test-role-with-travis-7.png?alt=media)
3. 使用 `git push` 到 GitHub 後，Travis CI 會自動觸發。 ![2016-12-27-test-role-with-travis-8.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-d8035b0ba3ff1879f9e63d241c60fd5c07cb8896%2F2016-12-27-test-role-with-travis-8.png?alt=media)
4. 若想把 **build state** 的 badge 放入 `README.md`，可點擊該 badge 獲得各式語法。 ![2016-12-27-test-role-with-travis-10.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-96ad7d6b3b5a11155ccf1f408162bd4cdc32ecff%2F2016-12-27-test-role-with-travis-10.png?alt=media)
5. 回到 Galaxy 網站，會發現多了 **build state** 的 badge。 ![2016-12-27-test-role-with-travis-9.png](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-4d902e3a02ed8f7d21000589766fb5472ff662a0%2F2016-12-27-test-role-with-travis-9.png?alt=media)

以上，恭喜您學會了在 Roles 加入 Travis CI 自動化測試的技能。

> 凍仁提醒您：「當 **build state** badge 顯示 **fail** 時，記得回頭修一下 Roles 喔！」

### 後語

此文發佈不到一天，凍仁就收到熱心網友回報的 [bug issue](https://github.com/chusiang/php7.ansible.role/issues/6)。百忙之中上了 hotfix 修正，並確認 Travis CI 測試無誤。

仔細回想，要是當初沒這麼做，誰又可以確保半年前寫的 code 現在還可以正常運作呢！

### 相關連結

* [Automated Testing | Ansible Galaxy](https://galaxy.ansible.com/intro#travis)
* [CI 從入門到入坑 系列 by Miles](http://ithelp.ithome.com.tw/users/20102562/ironman/987)
