因KubeSphere的DevOps系统官方未提供.net core的ci/cd解决方案,需要自己进行DIY。现把实施过程记录下来,供需要的小伙伴自取。
前提环境:
上述准备工作已列出官方文档(如需要私有镜像仓库,可以直接使用Deployment部署nexus3,网上资料比较多),不再赘述。
我们基于官方提供的base镜像构建dotnet的编译观景。使用如下Dockerfile构建用于打包的基础镜像:
FROM kubesphere/builder-base:v2.1.0
RUN rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
RUN yum install -y dotnet-sdk-3.1 #此处可以换成其他版本,也可以同时安装多个版本sdk一步到位
RUN dotnet tool install --global dotnet-sonarscanner --version 5.0.4 #(可选)安装dotnet的sonar-scanner
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/sonar-scanner-3.3.0.1492-linux/bin:/root/.nuget/tools:/root/.dotnet/tools
CMD ["dotnet", "--version"]
将Dockerfile置于一个空目录即可,然后打包并推送:
docker build -t builder-dotnet:v1.0.0 .
docker tag builder-dotnet:v1.0.0 xxx.com/builder-dotnet:v1.0.0
docker push xxx.com/builder-dotnet:v1.0.0
如果需要使用本地docker仓库,请提前配置好仓库密钥(不赘述)。
如果要使用本地Nuget仓库,请在“配置中心->配置”中,筛选项目为kubesphere-devops-system,找到ks-devops-agent,在其中添加一条配置,比如NugetSetting,并把Nuget.config的内容粘进去。
ks安装的jenkins采用CasC进行配置,而CasC配置文件又通过ConfigMap定义,然后挂载到jenkins容器。
找到“配置中心->配置”,筛选项目为kubesphere-devops-system,找到jenkins-casc-config。修改其jenkins.yml的配置项,在containers下,对go的描述后面添加如下内容:
- name: "dotnetcore"
namespace: "kubesphere-devops-system"
label: "dotnetcore"
nodeUsageMode: "EXCLUSIVE"
idleMinutes: 0
containers:
- name: "dotnetcore"
image: "xxx.com/builder-dotnet:v1.0.0" #镜像地址
command: "cat"
args: ""
ttyEnabled: true
resourceRequestCpu: "100m"
resourceLimitCpu: "4000m"
resourceRequestMemory: "100Mi"
resourceLimitMemory: "8192Mi"
alwaysPullImage: true
- name: "jnlp"
image: "jenkins/jnlp-slave:3.27-1"
command: "jenkins-slave"
args: "^${computer.jnlpmac} ^${computer.name}"
resourceRequestCpu: "50m"
resourceRequestMemory: "400Mi"
resourceLimitMemory: "1536Mi"
imagePullSecrets: #(可选)此处如果是使用私有仓库,请提前在密钥里准备好docker仓库密钥。
- name: docker-local
workspaceVolume:
emptyDirWorkspaceVolume:
memory: false
volumes:
- hostPathVolume:
hostPath: "/var/run/docker.sock"
mountPath: "/var/run/docker.sock"
- hostPathVolume: #将nuget包缓存持久化到hostPath
hostPath: "jenkins_nuget_cache"
mountPath: "/root/.nuget"
- hostPathVolume:
hostPath: "sonar_cache"
mountPath: "/root/.sonar/cache"
yamls: #请注意,此处从ConfigMap挂载了Nuget.Config,如不需要可以删除volumnMounts和volumns部分
- "spec:\r\n affinity:\r\n nodeAffinity:\r\n preferredDuringSchedulingIgnoredDuringExecution:\r\n - weight: 1\r\n preference:\r\n matchExpressions:\r\n - key: node-role.kubernetes.io/worker\r\n operator: In\r\n values:\r\n - ci\r\n tolerations:\r\n - key: \"node.kubernetes.io/ci\"\r\n operator: \"Exists\"\r\n effect: \"NoSchedule\"\r\n - key: \"node.kubernetes.io/ci\"\r\n operator: \"Exists\"\r\n effect: \"PreferNoSchedule\"\r\n containers:\r\n - name: \"dotnetcore\"\r\n resources:\r\n requests:\r\n ephemeral-storage: \"1Gi\"\r\n limits:\r\n ephemeral-storage: \"10Gi\"\r\n volumeMounts:\r\n - name: config-volume\r\n mountPath: /root/.nuget/NuGet/NuGet.Config\r\n subPath: NuGet.Config\r\n volumes:\r\n - name: config-volume\r\n configMap:\r\n name: ks-devops-agent\r\n items:\r\n - key: NugetSetting\r\n path: NuGet.Config\r\n securityContext:\r\n fsGroup: 1000\r\n "
修改并更新后,接下来需要去配置Jenkins使其生效。
打开你的Jenkins,点击“Manage Jenkins->Configuration as Code->Apply new configuration”。等待一会儿,如果没有报错,则配置完成。可以点击此页下的“View Configuration”检查配置是否生效。
此步骤其他细节还可以参考官方文档。
在KubeSphere的DevOps项目中,提前准备好git密钥。
建立一条流水线。编辑Jenkinsfile如下:(sonar分析部分可选,请自行替换其中变量)
pipeline {
agent {
node {
label 'dotnetcore'
}
}
stages {
stage('拉代码') {
steps {
git(url: 'https://你的域名.com/你的项目.git', credentialsId: 'git密钥名', branch: 'master', changelog: true, poll: false)
}
}
stage('代码分析') {
steps {
withSonarQubeEnv('sonar') {
container('dotnetcore') {
sh 'dotnet sonarscanner begin /k:"项目名" /n:项目名'
sh 'dotnet publish -c Release 源码目录/项目名.csproj -o 源码目录/bin/publish/'
sh 'dotnet sonarscanner end'
}
}
waitForQualityGate 'true'
}
}
stage('打镜像') {
steps {
container('dotnetcore') {
sh '''cat > 源码目录/Dockerfile << EOF
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim
WORKDIR /app
EXPOSE 80
COPY 源码目录/bin/publish/. .
ENTRYPOINT [ "dotnet","项目名.dll" ]
EOF
docker build -f 源码目录/Dockerfile -t 项目名:版本号 .'''
}
}
}
}
}
开启/关闭Devops系统、配置sonarqube等需修改CRD(自定义资源)下的ClusterConfiguration中的ks-install,该修改会导致ks-install服务工作对集群组件进行重新安装。从而导致jenkins的CasC配置和ks-jenkins-agent等配置被冲掉。如果修改了CRD导致集群组件重新安装,需要重新覆盖上述提到的devops系统相关的配置项。