本文已經不合時宜,請參考 Github Action - 更換成 beta v2 新的格式 (yaml)
本文使用 HCL syntax in GitHub Actions 將會在 2019-09-30 被棄用。(for info)
大家在開發 CI 的時候,一定會遇到需要寫 script,無論是 Shell Script, Python, Makefile,多少都會用到。那麼當你在第二個、第三個專案也需要一樣的流程,那我們就會從舊的專案複製一樣的 script 到新專案。
這個時候,如果發現 script 寫錯了,或是要擴充時。那麼你有數十個專案,都要一一進去每個專案改動這個 CI script,是不是想到就頭痛?
那麼你可以開始嘗試 Github Action,Github Action 非常符合 DRY(Don’t Repeat Yourself) 的原則,只要是 public repo 的 Dockerfile,那麼你就可以使用它,這等於是可以將 CI script 模組化,並重複利用。
你甚至可以在 local 使用,當作是 Jenkins, Ansible 的替代品。
Talk Is Cheap. Show Me The Code
Sample code: https://github.com/swaglive/action-demo/tree/master/.github
workflow "Show env" { |
如果你還沒有辦法使用 Github Action 的話,那麼你可以先在 Local 安裝 act,在 Local 感受一下 Github Action 的威力。
2019.08.01 Github Action 還是 beta 版,需要額外註冊才能使用
Enable:
Disable:
Act - Local Run Your Github Action Workflow
brew tap nektos/tap && brew install nektos/tap/act |
More Configs
Use
actions/heroku@master |
@ branch, ref, SHA
Event
push, pull_request, release
Env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
Dockerfile Example
https://developer.github.com/actions/creating-github-actions/creating-a-docker-container/#label
- icon: https://feathericons.com/
- color: white, yellow, blue, green, orange, red, purple, gray-dark
Label:
FROM alpine:3.10 |
Push Docker Image to Github Registry(Package)
Tutorial: https://help.github.com/en/articles/configuring-docker-for-use-with-github-package-registry
Behavior
workflow 可以有多個,但是只能放在
main.workflow。
# Show env |
會 cache docker layer,所以第二次 run 或是相同的 image 會非常快。
# Docker CLI |
同一個 Build(workflow) 當中,會共享 workspace,所以 action 1 如果做 docker login,action 2 能使用在同一個 workspace 下的 credential。
## Push Github Package |
某些 Docker 指令被鎖住 ex. network, image, volume,…
# Docker Info |
Docker daemon 也會 share
可以同時 (parellel) 跑多個 Actions,跟 workflow 上限無關
缺點
- 不能 manual trigger (re-run)
- 不能及時看到 Container Log
Limit
- Rate limits apply to individual workflows. The current rate limit is 20 workflow executions per minute. (link)[https://developer.github.com/actions/]
- Dockerfile 不能使用
User,因為 Github Action 都會用 root 啟動 container (link)[https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#filesystem] - 1 virtual CPU, 3.75 GB memory, 100 GB of disk space (link)[https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#runtime-environment-resources]
Publish Github Action
Tutorial: https://developer.github.com/marketplace/actions/publishing-an-action-in-the-github-marketplace/
實際範例: https://github.com/marketplace/actions/facebook-notify