Use imagePullSecrets in Jenkins declarative pipeline

2.5k views Asked by At

Here's a snippet from my Jenkinsfile I use to create a dynamic pod

agent {
        kubernetes {
            label "hello-stage"
            cloud "some-cloud"
            defaultContainer "jnlp"
            idleMinutes 1
            containerTemplate {
                name 'jnlp'
                image 'some-image'
                alwaysPullImage true
                ttyEnabled true
                resourceRequestMemory '1Gi'
                resourceLimitMemory '2Gi' 
            }
            imagePullSecrets '["secret-name"]'
        } // kubernetes
    } // agent

Everything works except for the imagePullSecrets. I tried finding documentation with no luck. I'm new to using Jenkins with Kubernetes. Here's the error I get

WorkflowScript: 23: Invalid config option "imagePullSecrets" for agent type "kubernetes". Valid config options are [activeDeadlineSeconds, cloud, containerTemplate, containerTemplates, customWorkspace, defaultContainer, idleMinutes, inheritFrom, instanceCap, label, namespace, nodeSelector, podRetention, serviceAccount, slaveConnectTimeout, supplementalGroups, workingDir, yaml, yamlFile, yamlMergeStrategy] @ line 23, column 13.
            imagePullSecrets '["quay-operator-updates"]'

Could someone please help me figure out how to use imagePullSecrets with Jenkins declarative pipeline?

Thanks

1

There are 1 answers

1
ChamodyaDias On

containerTemplate is now deprecated as mentioned here

Use the yaml syntax to pass the imagePullSecrets as follows :

pipeline {
    agent {
        kubernetes {
            yaml """\
        apiVersion: v1
        kind: Pod
        metadata:
            name: 'jenkins-slave'
            namespace: 'your-namespace'
        spec:
            containers:
            - name: jnlp
              image: jenkins/inbound-agent:4.3-9-alpine
              tty: true
            - name: demo-application
              image: some-image
              imagePullSecrets:
                  - name: your-secret
              tty: true

        """.stripIndent()
        }
    }
    stages {
        stage('run app') {
            steps {
                container('demo-application') {
                    echo POD_CONTAINER
                }
            }
        }
    }
}