V1
```tex
//说明:在新建项目时,Jenkins需要新增两项参数-字符参数进行构建
//1.Branch:默认值:Master 此次构建要拉取代码的分支版本
//2.ImageTag:默认值:v1.0.0 此次构建生产的镜像版本
// Docker镜像仓库地址
def registry = "harbor.nercoa.com"
// 项目信息
def project = "winjay"
def app_name = "demotest"
def image_name = "${registry}/${project}/${app_name}:${ImageTag}"
def git_address = "https://github.com/WinJayX/JavaDemoTest.git"
// 认证信息
def secret_name = "registry-pull-secret"
def docker_registry_auth = "d675c1e6-e109-4db0-942d-07c0b451432f"
def git_auth = "59f9845d-104a-4813-9018-49d2152f4149"
def k8s_auth = "f03eb670-1480-4cd2-8fb0-efdecf319472"
podTemplate(label: 'jenkins-agent', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "${registry}/winjay/jenkins-agent:jdk15"
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],
)
{
node("jenkins-agent"){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('代码编译'){
sh "mvn clean package -Dmaven.test.skip=true"
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM tomcat
MAINTAINER WinJayX <WinJayX@Gmail.com>
LABEL description="This image is Jenkins Agent with JDK 15"
LABEL version="jenkins-agent:jdk15"
USER root
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}' ${registry}
docker push ${image_name}
"""
}
}
// 第四步
stage('部署到K8S平台'){
sh """
sed -i 's#\$IMAGE_NAME#${image_name}#' deploy.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' deploy.yml
"""
kubernetesDeploy configs: 'deploy.yml', kubeconfigId: "${k8s_auth}"
}
}
}
```
V2
/* MAINTAINER WinJayX <WinJayX@Gmail.com>
* LABEL description="This is DotNetCore2.2 Project"
* LABEL version="1.0"
* 1.Git地址
* 2.分支信息
* 3.解决方案
* 4.执行文件
* 5.镜像地址*2
* 6.Dokcer名称
* 7.Docker端口
*/
//==========通用变量信息=========
// Docker镜像仓库地址
def Registry = "hub.nercoa.com"
// 项目信息
def Project = "winjay"
def AppName = "java-demo-test"
def ImageName = "${Registry}/${Project}/${AppName}:${ImageTag}"
def GitAddr = "https://github.com/WinJayX/JavaDemoTest.git"
// 认证信息
def Secret_name = "Registry-pull-secret"
def HubAuth = "34826729-147c-43c1-8c25-09f1357b36aa"
def GitAuth = "973c9a08-4d62-4f8a-8792-7a214b7ffe66"
def K8S_Auth = "1440a42f-0ee1-4eb2-9de4-29915386d25f"
//====================以上变量可在新建Job任务时使用字符参数进行插入=====================
podTemplate(label: 'jenkins-agent', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "${Registry}/winjay/k8s-jks-agent_alpine:openjdk15"
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],
)
{
node("jenkins-agent"){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${GitAuth}", url: "${GitAddr}"]]])
}
// 第二步
stage('代码编译'){
sh "mvn clean package -Dmaven.test.skip=true"
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${HubAuth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM tomcat
MAINTAINER WinJayX <WinJayX@Gmail.com>
LABEL description="This image is Jenkins Agent with JDK 15"
LABEL version="jenkins-agent:jdk15"
USER root
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t ${ImageName} .
docker login -u ${username} -p '${password}' ${Registry}
docker push ${ImageName}
"""
}
}
// 第四步
stage('部署到K8S平台'){
sh """
sed -i 's#\$ImageName#${ImageName}#' deploy.yml
sed -i 's#\$SECRET_NAME#${Secret_name}#' deploy.yml
"""
kubernetesDeploy configs: 'deploy.yml', kubeconfigId: "${K8S_Auth}"
}
}
}
V 3
/* 说明:在新建项目时,Jenkins需要新增几项参数-字符参数进行构建
* 1.Branch:默认值:Master 此次构建要拉取代码的分支版本
* 2.ImageTag:默认值:v1.0.0 此次构建生产的镜像版本
* 3.ProjectFile:默认值:src/XXX/XXX/FZ.IdentityServer.csproj
* 4.MAIN_PROGRAM:默认值:XXX.dll
* 5.app_name: 默认值:为构建的微服务名称,必须全为小写
*/
//==========认证信息===========
def secret_name = "registry-pull-secret"
def docker_registry_auth = "34826729-147c-43c1-8c25-09f1357b36aa"
def git_auth = "973c9a08-4d62-4f8a-8792-7a214b7ffe66"
def k8s_auth = "1440a42f-0ee1-4eb2-9de4-29915386d25f"
def prod_k8s_auth = " 8210bc14-109a-45df-b8b5-0bb901491115"
//==========Docker镜像仓库地址===========
def registry = "harbor.nercoa.com"
//==========项目变量信息===========
def app_name = "pubsub"
def project = "fz_mp_services"
def image_name = "${registry}/${project}/${app_name}:${ImageTag}"
def git_address = "http://git.nercoa.com/mp/fz.mp.microservice.git"
podTemplate(label: 'TestEnv', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "${registry}/winjay/jenkins-agent:netcore3-centos"
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],
)
{
node("TestEnv"){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('代码编译'){
sh "dotnet nuget add source http://202.205.161.102:9001/nuget -n nerc.org && dotnet publish -c Release -o /publish ${ProjectFile}"
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
cd /publish
echo '
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
MAINTAINER WinJayX <WinJayX@Gmail.com>
LABEL description="This is .NetCore Test Project"
LABEL version="1.0"
USER root
### 对连接sqlServer协议进行降级
RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf
WORKDIR /app
COPY . /app
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "${MAIN_PROGRAM}"]
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}' ${registry}
docker push ${image_name}
"""
}
}
// 第四步
/* stage('部署到K8S平台'){
* sh """
* sed -i 's#\$IMAGE_NAME#${image_name}#' deploy-netcore.yml
* sed -i 's#\$SECRET_NAME#${secret_name}#' deploy-netcore.yml
* """
* kubernetesDeploy configs: 'deploy-netcore.yml', kubeconfigId: "${k8s_auth}"
* }
*/
stage('Deploying To The Production Environment'){
timeout(time: 1, unit: 'DAYS') {
input message: 'Deploying To The Production Server?', ok: 'Deployment'
}
}
// 第五步
stage('Deploy To K8S Prod Environment'){
sh """
kubectl get nodes
sed -i 's#\$IMAGE_NAME#${image_name}#' deploy-netcore.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' deploy-netcore.yml
"""
kubernetesDeploy configs: 'deploy-netcore.yml', kubeconfigId: "${prod_k8s_auth}"
}
}
}
V4
//说明:在新建项目时,Jenkins需要新增几项参数-字符参数进行构建
//1.Branch:默认值:Master 此次构建要拉取代码的分支版本
//2.ImageTag:默认值:v1.0.0 此次构建生产的镜像版本
//3.ProjectFile:默认值:src/XXX/XXX/FZ.IdentityServer.csproj
//4.MAIN_PROGRAM:默认值:XXX.dll
//==========认证信息===========
def secret_name = "registry-pull-secret"
def docker_registry_auth = "34826729-147c-43c1-8c25-09f1357b36aa"
def git_auth = "6d48a6ed-7e33-499b-b78d-d323699253c5"
def k8s_auth = "1440a42f-0ee1-4eb2-9de4-29915386d25f"
//==========Docker镜像仓库地址===========
def registry = "harbor.nercoa.com"
//==========项目信息===========
def project = "winjay"
def app_name = "demotest"
def image_name = "${registry}/${project}/${app_name}:${ImageTag}"
def git_address = "http://git.nercoa.com/mp/fz.mp.microservice.git"
podTemplate(label: 'jenkins-agent', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "${registry}/winjay/jenkins-agent:netcore3-centos"
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],
)
{
node("jenkins-agent"){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('代码编译'){
sh "dotnet nuget add source http://202.205.161.102:9001/nuget -n nerc.org && dotnet publish -c Release -o /publish ${ProjectFile}"
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
cd /publish
echo '
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
MAINTAINER WinJayX <WinJayX@Gmail.com>
LABEL description="This is .NetCore Test Project"
LABEL version="1.0"
USER root
WORKDIR /app
COPY . /app
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "${MAIN_PROGRAM}"]
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}' ${registry}
docker push ${image_name}
"""
}
}
// 第四步
stage('部署到K8S平台'){
sh """
sed -i 's#\$IMAGE_NAME#${image_name}#' deploy-netcore.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' deploy-netcore.yml
"""
kubernetesDeploy configs: 'deploy-netcore.yml', kubeconfigId: "${k8s_auth}"
}
}
}
评论区