Skip to content

Instantly share code, notes, and snippets.

@rkivisto
Created September 23, 2020 15:26
Show Gist options
  • Save rkivisto/6fc98dbbc194882d2608edf967d2bd08 to your computer and use it in GitHub Desktop.
Save rkivisto/6fc98dbbc194882d2608edf967d2bd08 to your computer and use it in GitHub Desktop.
upstream and downstream Pipelines that the upstream can choose the Docker image for the agent pod of the downstream Pipeline
Upstream:
pipeline {
agent {
kubernetes {
// Rather than inline YAML, in a multibranch Pipeline you could use: yamlFile 'jenkins-pod.yaml'
// Or, to avoid YAML:
// containerTemplate {
// name 'shell'
// image 'ubuntu'
// command 'sleep'
// args 'infinity'
// }
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: shell
image: ubuntu
command:
- sleep
args:
- infinity
'''
// Can also wrap individual steps:
// container('shell') {
// sh 'hostname'
// }
defaultContainer 'shell'
}
}
stages {
stage('Main') {
steps {
sh 'cat /etc/os-release'
build job: 'downstream', parameters: [string(name: 'IMAGE', value: 'ubuntu:18.04')]
}
}
}
}
Downstream:
pipeline {
parameters {
string defaultValue: 'ubuntu:latest', description: 'Docker image and tag to use', name: 'IMAGE', trim: true
}
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: shell
image: $IMAGE
command:
- sleep
args:
- infinity
"""
defaultContainer 'shell'
}
}
stages {
stage('Main') {
steps {
sh 'cat /etc/os-release'
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment