0

Is it possible to get a shared library to retune a docker run command?

I have the following,

scr/docker_run.groovy

def ubuntu() {
  echo "docker run --rm " +
         '--env APP_PATH="`pwd`" ' +
         '--env RELEASE=true ' +
         "-v \"`pwd`:`pwd`\" " +
         "-v /var/run/docker.sock:/var/run/docker.sock " +
         "ubuntu" 
}

Jenkinsfile

@Library('pipeline-library-demo') _

pipeline {
    agent {
        node {
            label params.SLAVE
        }
    }

    parameters {
        string(name: 'SLAVE', defaultValue: 'so_slave')
    }

    stages {
        stage('ubuntu') {
            steps {
                script {
                    sh docker_run.ubuntu ls -lah
                }
            }
        }
    }
}

I have tried different things inside the groovy file, such as echo, sh, call, all returning errors.

Any help would be great

user3292394
  • 609
  • 2
  • 11
  • 24

1 Answers1

0

If you are using DSL style pipeline you should define Steps in your shared library not directly a function in the src/ directory. Should follow the directory structure defined here https://jenkins.io/doc/book/pipeline/shared-libraries/ Then you can define a Step like

vars/mystep.groovy

def call() {
    this.sh("docker_run.ubuntu ls -lah");
}

And calling from you pipeline as:

@Library('pipeline-library-demo') _

pipeline {
    agent {
        node {
            label params.SLAVE
        }
    }

    parameters {
        string(name: 'SLAVE', defaultValue: 'so_slave')
    }

    stages {
        stage('ubuntu') {
            steps {
                mystep
            }
        }
    }
}

Best

joliva
  • 330
  • 3
  • 10