K8S学习之基础五十四:jenkins新建测试流水线

jenkins新建测试流水线

新建任务


复制代码
node('testak') {
    stage('第1步:从gitee上下载源代码') {
        git url: "https://gitee.com/akang007/jenkins-sample"
        script {
            build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
        }
    }
    stage('第2步:基于dockerfile文件制作镜像') {
        sh "docker build -t 192.168.40.62/jenkins-demo/jenkins-demo:${build_tag} ."
    }
    stage('第3步:把镜像上传到harbor私有仓库') {
        withCredentials([usernamePassword(credentialsId: 'dockerharbor', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
            sh "docker login 172.16.80.140 -u ${dockerHubUser} -p ${dockerHubPassword}"
            sh "docker push 172.16.80.140/jenkins-demo/jenkins-demo:${build_tag}"
        }
    }
    stage('第4步:把pod部署到开发环境') {
		sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s-dev-harbor.yaml"
        sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s-dev-harbor.yaml"
//        sh "bash running-devlopment.sh"
        sh "kubectl apply -f k8s-dev-harbor.yaml  --validate=false"
	}	
	stage('第5步:把pod部署到测试环境') {	
		def userInput = input(
            id: 'userInput',

            message: '确定部署到测试环境吗?输入yes确定',
            parameters: [
                [
                    $class: 'ChoiceParameterDefinition',
                    choices: "YES\nNO",
                    name: 'Env'
                ]
            ]
        )
        echo "This is a deploy step to ${userInput}"
        if (userInput == "YES") {
            sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s-qa-harbor.yaml"
            sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s-qa-harbor.yaml"
//            sh "bash running-qa.sh"
            sh "kubectl apply -f k8s-qa-harbor.yaml --validate=false"
            sh "sleep 6"
            sh "kubectl get pods -n qatest"
            sh "cd /home/jenkins/agent/workspace/jenkins-harbor"
            sh "/root/Python-3.12.5/python qatest.py"
        } else {
            //exit
        }
    }
	stage('第6步:pod部署到生产环境') {	
		def userInput = input(

            id: 'userInput',
            message: '确定部署到生产环境吗?输入yes确定',
            parameters: [
                [
                    $class: 'ChoiceParameterDefinition',
                    choices: "YES\nNO",
                    name: 'Env'
                ]
            ]
        )
        echo "This is a deploy step to ${userInput}"
        if (userInput == "YES") {
            sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s-prod-harbor.yaml"
            sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s-prod-harbor.yaml"
//            sh "bash running-production.sh"
            sh "cat k8s-prod-harbor.yaml"
            sh "kubectl apply -f k8s-prod-harbor.yaml --record --validate=false"
            sh "cd /home/jenkins/agent/workspace/jenkins-harbor"
            sh "/root/Python-3.12.5/python smtp.py"
        }
    }
}
复制代码
Skip to content
[Jenkins]Jenkins
查找 (⌘+K)
1
2
admin

注销
Dashboard
jenkins-harbor
#11
状态
变更历史
Console Output
View as plain text
编辑构建信息
删除构建 '#11'
Timings
Git Build Data
回放
流水线步骤
Workspaces
上一次构建
Console Output
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Agent test-p2tjd is provisioned from template test
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    kubernetes.jenkins.io/last-refresh: "1742977713779"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "0d1a27dd84b68b7896a5df6f824a36e1721850f6"
    jenkins/label: "testak"
    kubernetes.jenkins.io/controller: "ttp___jenkins-service_jenkins-k8s_svc_cluster_local_8080x"
  name: "test-p2tjd"
  namespace: "jenkins-k8s"
spec:
  containers:
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "test-p2tjd"
    - name: "REMOTING_OPTS"
      value: "-noReconnectAfter 1d"
    - name: "JENKINS_NAME"
      value: "test-p2tjd"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://jenkins-service.jenkins-k8s.svc.cluster.local:8080/"
    image: "172.16.80.140/jenkins/jenkins.agent:v2"
    imagePullPolicy: "IfNotPresent"
    name: "jnlp"
    resources: {}
    securityContext:
      privileged: true
    tty: true
    volumeMounts:
    - mountPath: "/home/jenkins/.kube"
      name: "volume-1"
      readOnly: false
    - mountPath: "/etc/docker/daemon.json"
      name: "volume-4"
      readOnly: false
    - mountPath: "/usr/bin/kubectl"
      name: "volume-3"
      readOnly: false
    - mountPath: "/usr/bin/docker"
      name: "volume-2"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-0"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  serviceAccountName: "jenkins-k8s-sa"
  volumes:
  - hostPath:
      path: "/run/docker.sock"
    name: "volume-0"
  - hostPath:
      path: "/usr/bin/docker"
    name: "volume-2"
  - hostPath:
      path: "/root/.kube"
    name: "volume-1"
  - emptyDir:
      medium: ""
    name: "workspace-volume"
  - hostPath:
      path: "/etc/docker/daemon.json"
    name: "volume-4"
  - hostPath:
      path: "/usr/bin/kubectl"
    name: "volume-3"

Running on test-p2tjd in /home/jenkins/agent/workspace/jenkins-harbor
[Pipeline] {
[Pipeline] stage
[Pipeline] { (第1步:从gitee上下载源代码)
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository https://gitee.com/akang007/jenkins-sample
 > git init /home/jenkins/agent/workspace/jenkins-harbor # timeout=10
Fetching upstream changes from https://gitee.com/akang007/jenkins-sample
 > git --version # timeout=10
 > git --version # 'git version 2.39.5'
 > git fetch --tags --force --progress -- https://gitee.com/akang007/jenkins-sample +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://gitee.com/akang007/jenkins-sample # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision df755ee1bbcf162fbfb6cadf8bb008039d6ccbb6 (refs/remotes/origin/master)
Commit message: "update smtp.py."
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f df755ee1bbcf162fbfb6cadf8bb008039d6ccbb6 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git checkout -b master df755ee1bbcf162fbfb6cadf8bb008039d6ccbb6 # timeout=10
 > git rev-list --no-walk df755ee1bbcf162fbfb6cadf8bb008039d6ccbb6 # timeout=10
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ git rev-parse --short HEAD
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (第2步:基于dockerfile文件制作镜像)
[Pipeline] sh
+ docker build -t 172.16.80.140/jenkins-demo/jenkins-demo:df755ee .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  245.2kB

Step 1/5 : FROM golang:1.10.4-alpine
 ---> bd36346540f3
Step 2/5 : ADD . /go/src/app
 ---> ffc6aa5f3db2
Step 3/5 : WORKDIR /go/src/app
 ---> Running in 9823c707df6a
 ---> Removed intermediate container 9823c707df6a
 ---> 8de16f4e2759
Step 4/5 : RUN  go build -v -o /go/src/app/jenkins-app
 ---> Running in 8383ac606d62
[91mapp
[0m ---> Removed intermediate container 8383ac606d62
 ---> c56dc76e52d1
Step 5/5 : CMD ["./jenkins-app"]
 ---> Running in a9cfa67f329d
 ---> Removed intermediate container a9cfa67f329d
 ---> de8f85fff5b3
Successfully built de8f85fff5b3
Successfully tagged 172.16.80.140/jenkins-demo/jenkins-demo:df755ee
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (第3步:把镜像上传到harbor私有仓库)
[Pipeline] withCredentials
Masking supported pattern matches of $dockerHubPassword
[Pipeline] {
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
		 Affected argument(s) used the following variable(s): [dockerHubPassword]
		 See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ docker login 172.16.80.140 -u admin -p ****
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] sh
+ docker push 172.16.80.140/jenkins-demo/jenkins-demo:df755ee
The push refers to repository [172.16.80.140/jenkins-demo/jenkins-demo]
4a8fd9bc91b9: Preparing
67781d6fba74: Preparing
ac1c7fa88ed0: Preparing
cc5fec2c1edc: Preparing
93448d8c2605: Preparing
c54f8a17910a: Preparing
df64d3292fd6: Preparing
c54f8a17910a: Waiting
df64d3292fd6: Waiting
93448d8c2605: Layer already exists
c54f8a17910a: Layer already exists
ac1c7fa88ed0: Layer already exists
cc5fec2c1edc: Layer already exists
df64d3292fd6: Layer already exists
67781d6fba74: Pushed
4a8fd9bc91b9: Pushed
df755ee: digest: sha256:6c80d0f4d2c2bb82ebb3286b874c21b1917ef381e348bcc600b05d5e7dbf899a size: 1785
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (第4步:把pod部署到开发环境)
[Pipeline] sh
+ sed -i s/<BUILD_TAG>/df755ee/ k8s-dev-harbor.yaml
[Pipeline] sh
+ sed -i s/<BRANCH_NAME>/null/ k8s-dev-harbor.yaml
[Pipeline] sh
+ kubectl apply -f k8s-dev-harbor.yaml --validate=false
deployment.apps/jenkins-demo created
service/demo created
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (第5步:把pod部署到测试环境)
[Pipeline] input
Input requested
Approved by admin
[Pipeline] echo
This is a deploy step to YES
[Pipeline] sh
+ sed -i s/<BUILD_TAG>/df755ee/ k8s-qa-harbor.yaml
[Pipeline] sh
+ sed -i s/<BRANCH_NAME>/null/ k8s-qa-harbor.yaml
[Pipeline] sh
+ kubectl apply -f k8s-qa-harbor.yaml --validate=false
deployment.apps/jenkins-demo created
service/demo created
[Pipeline] sh
+ sleep 6
[Pipeline] sh
+ kubectl get pods -n qatest
NAME                            READY   STATUS    RESTARTS   AGE
jenkins-demo-56fd75f4db-5nbst   1/1     Running   0          8s
[Pipeline] sh
+ cd /home/jenkins/agent/workspace/jenkins-harbor
[Pipeline] sh
+ /root/Python-3.12.5/python qatest.py
邮件发送成功!
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (第6步:pod部署到生产环境)
[Pipeline] input
Input requested
Approved by admin
[Pipeline] echo
This is a deploy step to YES
[Pipeline] sh
+ sed -i s/<BUILD_TAG>/df755ee/ k8s-prod-harbor.yaml
[Pipeline] sh
+ sed -i s/<BRANCH_NAME>/null/ k8s-prod-harbor.yaml
[Pipeline] sh
+ cat k8s-prod-harbor.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-demo
  namespace: production
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-demo
  template:
    metadata:
      labels:
        app: jenkins-demo
    spec:
      containers:
      - image: 172.16.80.140/jenkins-demo/jenkins-demo:df755ee
        imagePullPolicy: IfNotPresent
        name: jenkins-demo
        env:
        - name: branch
          value: null
---
apiVersion: v1
kind: Service
metadata:
   name: jenkins-demo
   namespace: production
spec:
   selector:
     app: jenkins-demo
   type: NodePort
   ports:
   - port: 18888
     targetPort: 18888
     nodePort: 31890
[Pipeline] sh
+ kubectl apply -f k8s-prod-harbor.yaml --record --validate=false
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/jenkins-demo created
service/jenkins-demo created
[Pipeline] sh
+ cd /home/jenkins/agent/workspace/jenkins-harbor
[Pipeline] sh
+ /root/Python-3.12.5/python smtp.py
邮件发送成功!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Jenkins 中文社区
REST API
Jenkins 2.426.3

开发、测试、生产三个环境都部署了pod和svc,均可以通过访问ip+port的方式访问



收到邮件

相关推荐
FAREWELL000752 小时前
C#核心学习(一)面向过程与面向对象编程---初识类和对象
学习·c#·面向对象
开心码农1号2 小时前
K8S基础知识:DaemonSet、Deployment、StatefulSet的用法区别
云原生·容器·kubernetes
Yang三少喜欢撸铁2 小时前
【部署k8s集群时,彻底删除calico、flannel网络插件】
网络·容器·kubernetes
sealaugh322 小时前
aws(学习笔记第三十六课) apigw-http-api-lambda-dynamodb
笔记·学习·aws
空气力学先驱2 小时前
自顶向下学习K8S--部署Agones
docker·云原生·容器·kubernetes·go
*TQK*3 小时前
CSS学习笔记5——渐变属性+盒子模型阶段案例
css·笔记·学习
Logintern093 小时前
分享一个Pyside6实现web数据展示界面的效果图
python·学习·web·数据·pyside6
coding随想3 小时前
macOS (M1 Pro) 上 Docker Desktop 安装失败问题及其解决方案
macos·docker·容器
知识分享小能手3 小时前
CSS3学习教程,从入门到精通,CSS3 媒体查询实现响应式布局语法指南(21)
前端·css·学习·css3·html5·媒体
THMAIL3 小时前
k8s scheduler几种扩展方式的关系及区别
云原生·容器·kubernetes