下載代理
代理服務器池的作用用于CI也可以用于CD
主要用于編譯打包
部署的話使用部署池
當前下載到本地了
https://vstsagentpackage.azureedge.net/agent/2.188.4/vsts-agent-linux-x64-2.188.4.tar.gz
創建代理文件夾
mkdir myagent && cd myagent
sudo curl -OL https://vstsagentpackage.azureedge.net/agent/2.188.4/vsts-agent-linux-x64-2.188.4.tar.gz
解壓
tar zxvf vsts-agent-linux-x64-2.188.4.tar.gz
配置代理 - 在目錄 ~/myagent
下
./config.sh
這里可能會出現一些錯誤提示:Must not run with sudo
注意:代理服務器是基于.NET CORE3.1編寫,所以需要相應的運行環境,如果沒有,請運行如下命令:
./bin/installdependencies.sh to install
當前的解決方案
修改 config.sh文件
if [ $user_id -eq 0 ]; then
echo "Must not run with sudo"
exit 1
fi
為
if [ $user_id -eq 0 ] && [ "${ALLOW_RUNASROOT:-default_value}" == "default_value" ]; then
echo "Must not run with sudo"
exit 1
fi
運行
./config.sh
或者
export ALLOW_RUNASROOT=1; ./config.sh --unattended
創建自己的訪問令牌 (PAT) 進行身份驗證
我當前的Azure DevOps網址為:https://機構.visualstudio.com/
在Azure DevOps中找到 User Setting
->Personal Access Tokens
User Setting
在右上角頭像旁邊
Create a new personal access token
Scopes選擇Full access
當前token請妥善保存 在配置代理的時候需要使用
配置代理
Enter (Y/N) Accept the Team Explorer Everywhere license agreement now? (press enter for N) > y
>> Connect:
Enter server URL > https://dev.azure.com/機構名稱
Enter authentication type (press enter for PAT) >
Enter personal access token > ****************************************************
Connecting to server ...
>> Register Agent:
Enter agent pool (press enter for default) > QSPool
Enter agent name (press enter for iz2ze5jl9wtbhfev2i9kqjz) >
Scanning for tool capabilities.
Connecting to the server.
Enter replace? (Y/N) (press enter for N) > y
Successfully replaced the agent
Testing agent connection.
Enter work folder (press enter for _work) >
2021-07-14 03:18:32Z: Settings Saved.
-
輸入【Y】接受Team Explorer Everywhere許可協議;
-
輸入服務器URL,即項目的Azure的地址;
我的Azure網站為
https://機構.visualstudio.com/
這里我輸入https://dev.azure.com/機構名稱
才正確 -
輸入身份驗證類型,這里直接回車,選擇默認的PAT;
-
輸入個人訪問令牌,即PAT;
-
連接服務器成功后,輸入創建好的代理池;
-
默認代理名稱,也可以進行修改;
-
因為我之前已經創建過了,所以是否替換的選擇選擇了【Y】;
-
輸入工作文件夾,直接回車,選擇默認的【_work】;
然后運行啟動命令:
./run.sh
如果出現Must not run interactively with sudo
也需要修改run.sh
文件
if [ $user_id -eq 0 -a -z "$AGENT_ALLOW_RUNASROOT" ]; then
echo "Must not run with sudo"
exit 1
fi
改為
if [ $user_id -eq 0 ] && [ "${ALLOW_RUNASROOT:-default_value}" == "default_value" ]; then
echo "Must not run with sudo"
exit 1
fi
采用服務的形式運行代理服務器
上面我們的代理服務器雖然上線了,但是在linux中是主線程的形式,退出就關閉了..
我們需要修改為服務的形式來運行.
運行命令安裝服務:
sudo ./svc.sh install
啟動服務:
sudo ./svc.sh start
查看服務狀態:
sudo ./svc.sh status
停止服務:
sudo ./svc.sh stop
更新環境變量(當你有其他插件安裝或者更新時)
./env.sh
sudo ./svc.sh stop
sudo ./svc.sh start
查看代理是否成功部署
在Azure DevOps網站中又下角設置找到
Agent pools
-> 代理名稱->Agents
查看是否Online
創建CI持續集成管道
創建私有Docker Registry
首先需要到持續集成的服務器上 安裝Docker Registry來獲取我們的docker image
拉取registry鏡像
docker pull registry:2.7.0
registry 2.7以上刪除了包 apache2-utils
創建票據文件夾
mkdir -p ~/auth ~/auth/registry && cd ~/auth/registry
創建htpasswd
myuser :賬號
mypassword:密碼
docker run --entrypoint htpasswd registry:2.7.0 -Bbn myuser mypassword > htpasswd
創建文件夾
mkdir -p ~/data/registry
現在,我必須將我的憑據添加到注冊表中。在這里,我將在容器中安裝auth目錄:
docker run -d -p 8081:5000 --restart=unless-stopped --name registry-auth \
-v /root/auth/registry:/auth \
-v /data/registry:/var/lib/registry \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
registry
然后通過nginx代理配置https
配置后的地址為https://xxx:8081/v2
創建Service connections(服務連接)
在Azure DevOps網站中又下角設置找到
Service conntections
->New service connection
->選中Docker Registry點擊下一步-> 選擇others
https://index.docker.io/v2/ 配置的鏡像代理 需要配置成https
Docker ID :myuser
Docker Password: mypassword
添寫Service connection name點擊保存
創建持續集成管道
找到Pipelines菜單->New pipeline
Connect :Azure Repos Git
Select:選擇一個項目
//配置管道 選擇 docker
Configure:docker
Yaml配置
# Docker
# Build a Docker image
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool: QSPool
steps:
- task: Docker@2
inputs:
containerRegistry: 'QSDockerRegistry'
repository: 'qs.api'
command: 'buildAndPush'
Dockerfile: '**/Dockerfile'
安裝git
-
安裝 WANDisco 倉庫包
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
-
安裝 Git
yum -y install git
-
版本驗證
git version
安裝aspnet:5.0失敗
docker pull mcr.microsoft.com/dotnet/aspnet:5.0
Error response from daemon: Head https://mcr.microsoft.com/v2/dotnet/aspnet/manifests/5.0: read tcp 172.17.85.195:49560->13.75.34.156:443: read: connection reset by peer
該問題出現的原因是因為我們無法訪問mcr.microsoft.com的鏡像 需要更換一個可以訪問的鏡像源地址
拉取國內服務器上的鏡像
加速的本質是因為我將鏡像推送到了國內的服務器,目前在以下服務器均存在鏡像:
阿里云:registry.cn-hangzhou.aliyuncs.com/newbe36524
騰訊云:ccr.ccs.tencentyun.com/mcr_newbe36524
docker pull registry.cn-hangzhou.aliyuncs.com/newbe36524/aspnet:5.0