在本章中,將介紹3種Azure去搭建不同的pipeline的策略和具體的搭建流程,Azure Pipeline搭建,自己搭建Pipeline以及各種策略的優劣。
前提
部署的服務器是Azure VM或者是AKS。如果想用AWS部署,然後在Azure上搭建Pipeline,請關掉該頁面。
Azure Pipeline Microsoft-hosted 模式
簡介
Azure Pipeline中創建的各個pipeline中使用的構建機器都是由微軟提供的,當創建單個pipeline的時候,Azure Pipeline就會基於你的源碼給出對應的angent配置,用戶可以自行修改。pipeline用於構建的agent可能是虛擬機也可能是Docker啓動的,對客戶來說是完全透明的,不需要客戶去維護這個agent。
搭建流程
- 登錄Azure Devops服務
- 創建pipeline
- 修改Azure Pipeline的構建文件
微軟提供的Agent列表
Image | Classic Editor Agent Specification | YAML VM Image Label | Included Software |
---|---|---|---|
Windows Server 2019 with Visual Studio 2019 | windows-2019 | windows-latest OR windows-2019 |
Link |
Windows Server 2016 with Visual Studio 2017 | vs2017-win2016 | vs2017-win2016 |
Link |
Ubuntu 18.04 | ubuntu-18.04 | ubuntu-latest OR ubuntu-18.04 |
Link |
Ubuntu 16.04 | ubuntu-16.04 | ubuntu-16.04 |
Link |
macOS X Mojave 10.14 | macOS-10.14 | macOS-latest OR macOS-10.14 |
Link |
macOS X Catalina 10.15 | macOS-10.15 | macOS-10.15 |
Link |
價格
- 第一條流水線免費 30小時/月(也就是說,按照955來算,每天構建+部署時間不能超過1.5小時)
- 每增加一條新流水線 310.40港幣/月
Azure Pipeline Self-hosted 模式
簡介
pipeline使用的agen是自己持有的,可以爲Azure Pipeline添加自己agent進行構建,用於構建新的pipeline。用戶需要自己維護構建機器的信息。
搭建流程
-
配置agent,主要分爲三大步驟
- 在Azure Devops平臺設置訪問權限
- 從Azure Devops平臺下載agent,並在目標機器上配置agent
- 啓動agent
詳細步驟
-
創建Pipeline,步驟和上面Microsoft-hosted模式是一樣的。在yaml文件中設置pool時,使用自己配置的agent
pool:
name: 'MyAgentPool'
demands:
- npm
...
價格
- 第一條流水線免費,並且想跑多久就跑多久(畢竟不一定是Azure的機器)。
- 每增加一條,116.40港幣/月,比Microsoft-hosted模式便宜了一半!
Azure VM Jenkins模式
簡介
該模式將Jenkins安裝在Azure VM上。需要自己去配置Jenkins,也要自己去寫Jenkinsfile,並且自己去集成部署。
搭建流程
- 在Azure市場中找到Jenkins的方案
- 創建安裝了Jenkins的虛擬機。在創建過程中可以用“用戶名/密碼”的方式也可以用“SSH公鑰”的方式登錄服務器。推薦使用“SSH公鑰”的方式,這樣不用每次都輸入密碼,方便。
- 安裝完成後,可以在剛剛創建時填入的資源組中找到Jenkins的相關資源,並找到**“公共 IP 地址”**資源,查看外網IP,可以通過http://外網IP 訪問Jenkins的主頁。
- 爲了保證網絡安全,Jenkins默認是不允許HTTP協議訪問的。如果想配置服務,需要使用網頁上提示的
ssh -L xxx
命令去登錄到申請的虛擬機上。然後就可以正常配置Jenkins的admin用戶信息,下載Jenkins的插件了。 - 如果希望使用HTTP的方式訪問Jenkins。可以修改服務器上的nginx配置。將/signup 相關的url都禁用掉,這樣外網用戶就不能夠自己創建Jenkins用戶了。
修改~/etc/nginx/sites-enabled/default文件
server {
listen 80;
server_name xxx.southeastasia.cloudapp.azure.com;
error_page 403 /jenkins-on-azure;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080;
proxy_redirect http://localhost:8080 http://xxx.southeastasia.cloudapp.azure.com;
proxy_read_timeout 90;
}
location ~ /signup* {
rewrite ^ /jenkins-on-azure permanent;
}
location /jenkins-on-azure {
alias /usr/share/nginx/azure;
}
}
- 配置Jenkins的用戶策略
- 當Jenkins構建時,可能需要用到一些腳本命令,如果在不使用docker agent的情況下,可以使用以下命令來去掉sudo。
sudo groupadd $CMD
sudo usermod -aG $CMD $USER
總結一波
模式 | 優點 | 缺點 | 備註 |
---|---|---|---|
Azure Pipeline Microsoft-hosted | 一鍵創建,簡單,快捷 | 價格高,按分鐘收費 | 不差錢的請使用 |
Azure Pipeline Self-hosted | 自持有的Agent比較便宜。 | 需要自己配置Agent,維護Agent的環境 | 有一定的運維能力的團隊可以使用 |
Azure VM Jenkins | 便宜,靈活度高,依賴較少,出了問題可以自己解決 | 需要自己去做集成,人力成本高 | 有較高運維能力團隊適用,或者預算有限的團隊比較適合 |
參考資料
- https://azure.microsoft.com/en-us/pricing/details/devops/azure-devops-services/
- https://stackoverflow.com/questions/39638772/make-jenkins-run-docker-without-sudo
- https://docs.microsoft.com/zh-cn/azure/devops/pipelines/get-started/pipelines-sign-up?view=azure-devops
原文鏈接:https://huldoo.com/2020/02/16/azure-%e6%90%ad%e5%bb%ba-pipeline/