jenkinsfile的使用

本文介绍了如何在Jenkinsfile中利用yaml语法定义Kubernetes Pod模板,特别是设置jnlp容器并从私有仓库拉取镜像。同时,通过引用configmap获取配置信息,如harbor的用户名,确保敏感信息的安全管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

podTemplate(cloud: 'kubernetes', yaml: """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: jnlp
    image: 'ops-reg.xxxx.com/ops/rancher/jenkins-jnlp-slave:3.10-1-alpine'
    args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
  - name: docker-test
    image: ops-reg.xxxx.com/ops/docker:dind
    command: ['cat']
    tty: true
    env:
    - name: HARBORUSERNAME
      valueFrom:
        configMapKeyRef:
          name: harbor-username-pwd
          key: username
    - name: HARBORPWD
      valueFrom:
        configMapKeyRef:
          name: harbor-username-pwd
          key: password
    volumeMounts:
    - name: dockersock
      mountPath: /var/run/docker.sock
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
  imagePullSecrets:
  - name: registry-secret
"""
  ) {
  def GITLAB_REGISTRY = 'ops-reg.xxxx.com'
  node(POD_LABEL) {
    stage('Build Docker image') {
      container('docker-test') {
            withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPwd', usernameVariable: 'harborUsername')]) {
                sh "docker login -u ${harborUsername} -p ${harborPwd} ${GITLAB_REGISTRY}"
            }
            def docker_repo = 'cenarius'
            git changelog: false, credentialsId: 'c0255365-13dd-4b18-aa16-49c8b9de9a50', poll: false, url: 'https://oa-git.xxxx.com/JDS/Cenarius.git'  //clone代码到docker-test容器中
            // def GIT_COMMIT_INFO = checkout scm  //获取本次commit信息,其中包括commit hash值.
            // echo "$GITLAB_REGISTRY/3rd/${docker_repo}:${GIT_COMMIT_INFO.GIT_COMMIT}"
            def app = docker.build("$GITLAB_REGISTRY/3rd/${docker_repo}")
            def TAG = sh(returnStdout: true, script: 'sh get_version.sh')
            app.push("${TAG}")
            def imageID = app.id
            sh "docker rmi ${imageID}" 
      }
    }
  }
}

在podTemplate中使用yaml语法定义namejnlp的pod(注意,这里的name必须为jnlp,Jenkins将通过yaml中定义的pod name来决定那个容器做什么用,而kubernetes-plugin默认slave的使用的容器名称为jnlp),jenkins将默认使用该pod作为slave的基础镜像。因为jnlp镜像比较大,所以从私库拉取镜像是更好的方法。同样在yaml中定义私库地址,并用imagePullSecrets指出登录私库所用secret。

env:
    - name: HARBORUSERNAME
      valueFrom:
        configMapKeyRef:
          name: harbor-username-pwd
          key: username

以上yaml表示通过读取configmap,获取相关配置信息(在该处,读取名为harbor-username-pwd的configmap的username属性,并将该属性的值设置为名为HARBORUSERNAME的容器环境变量)。

withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPwd', usernameVariable: 'harborUsername')]) {
                sh "docker login -u ${harborUsername} -p ${harborPwd} ${GITLAB_REGISTRY}"
            }

以上配置使用在jenkins中添加的凭据,这样做的好处在于,将隐藏所有敏感信息,例如harbor的登录用户名及密码。
参考:
https://github.com/jenkinsci/kubernetes-plugin
The default jnlp agent image used can be customized by adding it to the template 部分。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值