Jenkins Declarative Pipeline calling Method stages - docker

Goal
I'm trying to create multiple docker images that have slightly different configurations, these will form the basis of different builds further down stream. To avoid code duplication, I'm trying to use a method inside my Declarative pipeline.
Problem
I can't work out how to have a method that has multiple stages {} inside of it.
Example
pipeline{
agent any
stages{
stage('Create Images'){
parallel {
stage('Image Foo'){
environment{
dockerImage=''
}
stages{
stage("Build image"){
steps{
scripted{
dockerImage=docker.build(...)
}
}
}
stage("Configure image"){
configImage(dockerImage)
}
stage('Push to artifactory'){
steps{
scripted{
dockerImage.push()
dockerImage.push("latest")
}
}
}
}
}
stage('Image Bar'){
environment{
dockerImage=''
}
stages{
stage("Build image"){
steps{
scripted{
dockerImage=docker.build(...)
}
}
}
stage("Configure image"){
configImage(dockerImage)
}
stage('Push to artifactory'){
steps{
scripted{
dockerImage.push()
dockerImage.push("latest")
}
}
}
}
}
}
}
}
}
void configImage(dockerImage){
stages{
stage("Grab some files"){
steps{
echo "I'm getting some files"
}
}
stage("Install some stuff"){
steps{
echo "Install all the things"
}
}
}
}

Related

How to configure SCM (gitHub) checkout re-try in multibranch declarative Jenkins pipeline?

My multibranch declarative Jenkins pipeline is failing very frequently during SCM checkout with timeout error and it works after one or two retries. is there anyway to automate the retry the SCM checkout?
Jenkinsfile
agent {
label "agent1"
}
stages {
stage('Test') {
parallel {
stage('Test1') {
steps { sh 'echo Test 1 passed' }
}
stage('Test2') {
steps {
sh 'echo Test2 is passed'
}
} stage('Test3') {
steps {
sh 'echo Test 3 passed'
}
}
}
}
}
You can add wrapper steps such as "retry" to retry the SCM checkout step.
agent {
label "agent1"
}
stages {
stage('Test') {
parallel {
stage('Test1') {
steps {
retry(3) {
echo "Test 1 passed"
}
}
}
stage('Test2') {
steps {
retry(3) {
echo "Test 1 passed"
}
}
}
}
}
}

JenkinsFile | Declarative stage for multiple environment

I have a JenkinsFile declarative pipeline below is the structure-
pipeline{
agent{}
stages{
stage(dev_one){
steps{
}
}
stage(dev_two){
steps{
}
}
stage(test_one){
steps{
}
}
stage(test_two){
steps{
}
}
}
}
I see there is redundancy in the JPL script, as you see stage(dev_one) and stage(test_one) will have the same steps.
How can I reuse the stage(dev_one), so that one stage can be used for different environments.
You can wrap the repeat code into a function, then invoke the function in multiple stages with different params.
pipeline{
agent{}
stages{
stage(dev_one){
steps{
workOnEnv(this, 'dev_one', <next param>)
}
}
stage(dev_two){
steps{
workOnEnv(this, 'dev_two', <next param>)
}
}
stage(test_one){
steps{
}
}
stage(test_two){
steps{
}
}
}
}
void workOnEnv(Script script, String env, String param2, ...) {
script.echo "work on env: $env"
script.sh ""
script.step()
}
void ENV_DEV_Deploy_and_Test(){
sshagent(credentials: ['iamcredentials']) {
sh '''
my script
'''
}
}
pipeline{
agent{}
stages{
stage('ENV-DEV Deploy and Test') {
steps {
ENV_DEV_Deploy_and_Test()
}
}
stage(dev_two){
steps{
}
}
stage(test_one){
steps{
}
}
stage(test_two){
steps{
}
}
}
}

Jenkinsfile nested stage(s) throwing error

I have the following Jenkinsfile which I believe is setup correctly. I used https://jenkins.io/doc/book/pipeline/syntax/#sequential-stages as an example but for some reason when I run this in jenkins I am receiving,
WorkflowScript: 11: Unknown stage section "stages". Starting with
version 0.5, steps in a stage must be in a steps block
Can someone tell me what I am missing or doing wrong?
pipeline {
agent {label 'windows'}
stages {
stage('Quick Build') {
steps {
echo 'Building'
}
}
stage('Deploy to Dev') {
// when {
// branch 'develop'
// }
stages {
stage('Building Distributable Package') {
steps {
echo 'Building'
}
}
stage('Archiving Package') {
steps {
echo 'Archiving Aritfacts'
archiveArtifacts artifacts: '/*.zip', fingerprint: true
}
}
stage('Deploying Dev') {
steps {
echo 'Deploying'
timeout(time:3, unit:'DAYS') {
input message: "Approve build?"
}
}
}
}
}
stage('Deploy to Test') {
when {
branch 'develop'
}
steps {
echo 'deploying..'
timeout(time:3, unit:'DAYS') {
input message: "Approve build?"
}
}
}
stage('Deploy to Prod') {
when {
branch 'release'
}
steps {
timeout(time:3, unit:'DAYS') {
input message: "Deploy to Prod?"
}
echo 'Deploying....'
}
}
}
}
Thanks in advance!
This ended up being a problem in version 2.107.3. Once upgraded to 2.121.2 this functionality started working.

How to declare many agents in a Jenkinsfile with Declarative syntax?

In the Pipeline scripted syntax we use this syntax to declare specific steps to specific nodes:
steps{
node('node1'){
// sh
}
node('node2'){
// sh
}
}
But I want to use the Pipeline Declarative Syntax, can I put many agents?
Sure you can. Just take a look at example (from docs):
pipeline {
agent none
stages {
stage('Build') {
agent any
steps {
checkout scm
sh 'make'
stash includes: '**/target/*.jar', name: 'app'
}
}
stage('Test on Linux') {
agent {
label 'linux'
}
steps {
unstash 'app'
sh 'make check'
}
post {
always {
junit '**/target/*.xml'
}
}
}
stage('Test on Windows') {
agent {
label 'windows'
}
steps {
unstash 'app'
bat 'make check'
}
post {
always {
junit '**/target/*.xml'
}
}
}
}
}

Nested docker agents in a Jenkinspipeline

I have the following setup:
Jenkinsmaster, no docker installed
Jenkinsslave, docker is installed, label dockerslave
When I run the following pipeline:
pipeline {
agent { node { label 'dockerslave' } }
stages {
stage('Example Build') {
agent { docker { image 'maven:3-alpine' } }
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent { docker { image 'openjdk:8-jre' } }
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}
I get the following logoutput:
[Pipeline] node
Running on dockerslave in /home/jenkins/workspace/docker-
declarative
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example Build)
[Pipeline] node
Still waiting to schedule task
There are no nodes with the label ?latest?
The job doesn't proceed and hangs.
What is the problem here?
The problem was the missing:
reuseNode true
The fixed example:
pipeline {
agent {
node { label 'dockerslave' } }
stages {
stage('Example Build') {
agent {
docker {
reuseNode true
image 'maven:3-alpine'
}
}
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent {
docker {
reuseNode true
image 'openjdk:8-jre'
}
}
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}

Resources