Jenkins 构建作业 A 使用其他构建作业 B docker 图像配置

Jenkins build job A uses other build job B docker image configuration

Jenkins 运行 在 AWS EKS 集群中的 jenkins-ci 命名空间下。当 multib运行ch 管道作业 "Branch-A" 开始构建时,它正在获取正确的配置 (KubernetesPod.yaml) 并且 运行 成功,当作业 "Branch-B" 开始构建它是使用作业 A 配置,如 docker 图像和 buildurl。

Gitlab配置:

B运行ch-A -- KubernetesPod.yaml

apiVersion: v1
kind: Pod
spec:
  serviceAccount: jenkins
  nodeSelector:
    env: jenkins-build
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            matchExpressions:
              - key: env
                operator: In
                values:
                - jenkins-build
  tolerations:
  - key: "highcpu"
    operator: "Equal"
    value: "true"
    effect: "NoSchedule"

  volumes:
  - name: dev
    hostPath:
      path: /dev

  imagePullSecrets:
  - name: gitlab

  containers:
    - name: build
      image: registry.gitlab.com/mycompany/sw-group/docker/ycp:docker-buildtest-1
      imagePullPolicy: IfNotPresent
      command:
        - cat
      securityContext:
        privileged: true
      volumeMounts:
      - mountPath: /dev
        name: dev
      tty: true
      resources:
        requests:
          memory: "4000Mi"
          cpu: "3500m"
        limits:
          memory: "4000Mi"
          cpu: "3500m"

B运行ch-B -- KubernetesPod.yaml

apiVersion: v1
kind: Pod
spec:
  serviceAccount: jenkins
  nodeSelector:
    env: jenkins-build
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            matchExpressions:
              - key: env
                operator: In
                values:
                - jenkins-build
  tolerations:
  - key: "highcpu"
    operator: "Equal"
    value: "true"
    effect: "NoSchedule"

  volumes:
  - name: dev
    hostPath:
      path: /dev

  imagePullSecrets:
  - name: gitlab

  containers:
    - name: build
      image: registry.gitlab.com/mycompany/sw-group/docker/ycp:docker-buildtest-2
      imagePullPolicy: IfNotPresent
      command:
        - cat
      securityContext:
        privileged: true
      volumeMounts:
      - mountPath: /dev
        name: dev
      tty: true
      resources:
        requests:
          memory: "4000Mi"
          cpu: "3500m"
        limits:
          memory: "4000Mi"
          cpu: "3500m"

Jenkins B运行ch-A 控制台输出:

Seen branch in repository origin/unknownMishariBranch
Seen branch in repository origin/vikg/base
Seen 471 remote branches
Obtained Jenkinsfile.kubernetes from 85b8ab296342b98be52cbef26acf20b15503c273
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] readTrusted
Obtained KubernetesPod.yaml from 85b8ab296342b98be52cbef26acf20b15503c273
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Still waiting to schedule task
Waiting for next available executor
Agent company-pod-8whw9-wxflb is provisioned from template Kubernetes Pod Template
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "https://jenkins.mycompany.com/job/multibranch/job/branch-A/3/"
  labels:
    jenkins: "slave"
    jenkins/mycompany-pod: "true"
  name: "mycompany-pod-8whw9-wxflb"
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - key: "env"
            operator: "In"
            values:
            - "jenkins-build"
        weight: 1
  containers:
  - command:
    - "cat"
    image: "registry.gitlab.com/mycompany/sw-group/docker/ycp:docker-buildtest-1"
    imagePullPolicy: "IfNotPresent"
    name: "build"
    resources:
      limits:
        memory: "4000Mi"
        cpu: "3500m"
      requests:
        memory: "4000Mi"
        cpu: "3500m"

Jenkins B运行ch-B 控制台输出:

Seen branch in repository origin/unknownMishariBranch
Seen branch in repository origin/viking/base
Seen 479 remote branches
Obtained Jenkinsfile.kubernetes from 38ace636171311ef35dc14245bf7a36f49f24e11
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] readTrusted
Obtained KubernetesPod.yaml from 38ace636171311ef35dc14245bf7a36f49f24e11
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Still waiting to schedule task
Waiting for next available executor
Agent mycompany-pod-qddx4-08xtm is provisioned from template Kubernetes Pod Template
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "https://jenkins.mycompany.com/job/multibranch/job/branch-A/3/"
  labels:
    jenkins: "slave"
    jenkins/mycompany-pod: "true"
  name: "mycompany-pod-qddx4-08xtm"
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - key: "env"
            operator: "In"
            values:
            - "jenkins-build"
        weight: 1
  containers:
  - command:
    - "cat"
    image: "registry.gitlab.com/mycompany/sw-group/docker/ycp:docker-buildtest-1"
    imagePullPolicy: "IfNotPresent"
    name: "build"
    resources:
      limits:
        memory: "4000Mi"
        cpu: "3500m"
      requests:
        memory: "4000Mi"
        cpu: "3500m"

每当构建被触发时,它都会在 Jenkinsfile 中使用相同的标签名称。 我在下面发布我的 jenkinsfile 脚本的一部分。 以下解决方案解决了我的问题。

之前:

pipeline {

  agent {
    kubernetes {
      label "sn-optimus"
      defaultContainer "jnlp"
      yamlFile "KubernetesPod.yaml"
    }
  }

之后:

pipeline {

  agent {
    kubernetes {
      label "sn-optimus-${currentBuild.startTimeInMillis}"
      defaultContainer "jnlp"
      yamlFile "KubernetesPod.yaml"
    }
  }