Azure DevOps构建CICD流水线

环境准备

Azure资源

  • Azure AKS
  • Azure CR
  • Azure DevOps

代码准备

.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

构建CICD流水线

应用授权

注册Azure AD应用

  • 打开Azure portal,导航到Azure AD
    在这里插入图片描述

  • 选择应用注册,点击新注册
    注册AD应用

  • 输入应用名称,点击注册
    注册应用

  • 创建客户端密码
    在这里插入图片描述

分配应用订阅的参与者角色

  • 导航到订阅,选择Access control(IAM),点击添加按钮,添加角色分配,将此应用分配为订阅的参与者权限
    分配角色

配置Service Connection

配置gitee的链接服务

  • 导航到Project Settings页面,选择Service Connection选项卡,点击New Service Connection按钮,创建连接服务
    添加链接服务

配置AKS的链接服务

  • 点击创建链接服务,选择Azure Resource Manager
    创建Resource Manager Connection

  • 选择Service principal (manual)
    在这里插入图片描述

  • 选择Azure Cloud China,输入必要信息
    在这里插入图片描述

  • 验证并保存。

创建Pipeline

选择模板

  • 导航到Pipeline,点击New Pipeline
    在这里插入图片描述

  • 选择手动编辑器方式创建Pipeline,不使用yaml方式

在这里插入图片描述

选择代码仓库

  • 如果是Azure代码仓库
    在这里插入图片描述

  • 如果是gitee代码仓库
    在这里插入图片描述

选择Agent

  • 保存默认即可。

在这里插入图片描述

构建镜像

  • 使用Docker作业来构建一个服务镜像

在这里插入图片描述

推送镜像

  • 将构建出来的镜像推送到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

部署服务

  • 更新部署服务。

在这里插入图片描述

  • Command Arguments
image deploy $(deploymentName) *=xxxazurecr.cn/$(imageNameWithTag)

启用IP地址范围限制

  • 启用IP Range限制。

在这里插入图片描述

  • 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

设置CD

  • 设置自动触发

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值