環(huán)境準(zhǔn)備
Azure資源
- Azure AKS
- Azure CR
- Azure DevOps
代碼準(zhǔn)備
.NET Core示例
Dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
#ENV ConnectionStrings:Default=""
#ENV ConnectionStrings:Log=""
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
#RUN dotnet restore
#RUN dotnet build MyProject.API.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish MyProject.API.csproj -c Release -o /app/publish
COPY MyProject.API.xml /app/publish/MyProject.API.xml
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.API.dll"]
deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: awesome-api
spec:
replicas: 1
selector:
matchLabels:
app: awesome-api
template:
metadata:
labels:
app: awesome-api
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- name: awesome-api
image: dataplatformacr.azurecr.cn/awesomeapi:latest
env:
- name: ALLOW_EMPTY_PASSWORD
value: "yes"
---
apiVersion: v1
kind: Service
metadata:
name: awesome-api
spec:
ports:
- port: 80
type: LoadBalancer
selector:
app: awesome-api
Java示例
Dockerfile
FROM java:8
EXPOSE 8080
VOLUME /tmp
ADD target/*.jar /app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","-Xms128m","-Xmx300m","/app.jar","--spring.profiles.active=prod"]
deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: awesomemall-gateway
namespace: awesomemall
labels:
app: awesomemall-gateway
spec:
replicas: 1
selector:
matchLabels:
app: awesomemall-gateway
template:
metadata:
labels:
app: awesomemall-gateway
spec:
containers:
- name: awesomemall-gateway
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 8080
protocol: TCP
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
restartPolicy: Always
---
kind: Service
apiVersion: v1
metadata:
name: awesomemall-gateway
namespace: awesomemall
labels:
app: awesomemall-gateway
spec:
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
selector:
app: awesomemall-gateway
type: NodePort
構(gòu)建CICD流水線
應(yīng)用授權(quán)
注冊Azure AD應(yīng)用
-
打開Azure portal,導(dǎo)航到Azure AD
-
選擇應(yīng)用注冊,點(diǎn)擊新注冊
-
輸入應(yīng)用名稱,點(diǎn)擊注冊
-
創(chuàng)建客戶端密碼
分配應(yīng)用訂閱的參與者角色
- 導(dǎo)航到訂閱,選擇Access control(IAM),點(diǎn)擊添加按鈕,添加角色分配,將此應(yīng)用分配為訂閱的參與者權(quán)限
配置Service Connection
配置gitee的鏈接服務(wù)
- 導(dǎo)航到Project Settings頁面,選擇Service Connection選項(xiàng)卡,點(diǎn)擊New Service Connection按鈕,創(chuàng)建連接服務(wù)
配置AKS的鏈接服務(wù)
-
點(diǎn)擊創(chuàng)建鏈接服務(wù),選擇Azure Resource Manager
-
選擇Service principal (manual)
-
選擇Azure Cloud China,輸入必要信息
-
驗(yàn)證并保存。
創(chuàng)建Pipeline
選擇模板
-
導(dǎo)航到Pipeline,點(diǎn)擊New Pipeline
-
選擇手動編輯器方式創(chuàng)建Pipeline,不使用yaml方式
選擇代碼倉庫
-
如果是Azure代碼倉庫
-
如果是gitee代碼倉庫
選擇Agent
- 保存默認(rèn)即可。
構(gòu)建鏡像
- 使用Docker作業(yè)來構(gòu)建一個服務(wù)鏡像
推送鏡像
- 將構(gòu)建出來的鏡像推送到Azure鏡像倉庫
臨時禁用IP地址范圍限制
- 臨時禁用IP Range限制。
- shell腳本
# Get authorized ip ranges allowed to access API server of AKS cluster
current_authorized_ip=`az aks show -n $(aks.clusterName) -g $(aks.resourceGroupName) --query [apiServerAccessProfile.authorizedIpRanges] -o table|sed -n '3,1p' |sed 's/\s\+/,/g'`
echo ${current_authorized_ip}
# Get self public IP
# self_ip=$(curl ifconfig.co)
# echo "Self public IP address: $self_ip"
# Set current authorized ips as output variable
echo "##vso[task.setvariable variable=authorized_ip;isOutput=true]${current_authorized_ip}"
# Temperarily disable authorized IP ranges
arrIPs=(${current_authorized_ip//,/ })
if [ ${#arrIPs[@]} -gt 0 ];then
echo "Temperarily disable authorized IP ranges..."
az aks update -n $(aks.clusterName) -g $(aks.resourceGroupName) --api-server-authorized-ip-ranges ""
else
echo "Authorized IP is already disabled, skip temperary disable"
fi
部署服務(wù)
- 更新部署服務(wù)。
- Command Arguments
image deploy $(deploymentName) *=xxxazurecr.cn/$(imageNameWithTag)
啟用IP地址范圍限制
- 啟用IP Range限制。
文章來源:http://www.zghlxwxcb.cn/news/detail-484016.html
- shell 腳本
original_authorized_ip=$(aks.authorized_ip)
echo Original Authorized IP ranges: ${original_authorized_ip}
# Recover authorized IP ranges if need
arrIPs=(${original_authorized_ip//,/ })
if [ ${#arrIPs[@]} -gt 0 ];then
echo "Recover authorized IP ranges to original configuration ..."
az aks update -n $(aks.clusterName) -g $(aks.resourceGroupName) --api-server-authorized-ip-ranges ${original_authorized_ip}
else
echo "Authorized IP ranges is disabled orginally, skip recover step"
fi
設(shè)置CD
- 設(shè)置自動觸發(fā)
文章來源地址http://www.zghlxwxcb.cn/news/detail-484016.html
到了這里,關(guān)于Azure DevOps構(gòu)建CICD流水線的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!