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

6/11/2020

Jenkins is running in AWS EKS cluster under a jenkins-ci namespace. When multibranch pipeline job "Branch-A" started the build, it is picking up correct configurations (KubernetesPod.yaml) and ran successfully and when job "Branch-B" has started the build it is using job A configurations like docker image and buildurl.

Gitlab Configuration:

Branch-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"

Branch-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 Branch-A console output:

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 Branch-B console output:

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"
-- Kanth
amazon-eks
docker-registry
jenkins
kubernetes
multibranch-pipeline

1 Answer

6/17/2020

Whenever the build gets triggered it is using same label name in Jenkinsfile. I am posting below part of my jenkinsfile script. The below solution solved my problem.

Before:

pipeline {

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

After:

pipeline {

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