Jenkinfile DSL how to specify target directory

╄→гoц情女王★ 提交于 2019-12-05 11:13:12

问题


I'm exploring Jenkins 2.0 pipelines. So far my file is pretty simple.

node {
    stage "checkout"
    git([url:"https://github.com/luxengine/math.git"])

    stage "build"
    echo "Building from pipeline"
}

I can't seem to find any way to set the directory that git will checkout to. I also can't find any kind of documentation related to that. I found https://jenkinsci.github.io/job-dsl-plugin/ but it doesn't seem to match what I see on other tutorials.


回答1:


Clarification

Looks like you are trying to configure Pipeline job (formerly known as Workflow). This type of job is very distinct from Job DSL.

The purpose of Pipeline job is to:

Orchestrates long-running activities that can span multiple build slaves. Suitable for building pipelines (formerly known as workflows) and/or organizing complex activities that do not easily fit in free-style job type.

Where as Job DSL:

...allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.

Solution

If you want to checkout your code to specific directory then replace git step with more general SCM checkout step. Final Pipeline configuration should look like that:

node {
    stage "checkout"
    //git([url:"https://github.com/luxengine/math.git"])
    checkout([$class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'RelativeTargetDirectory', 
            relativeTargetDir: 'checkout-directory']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[url: 'https://github.com/luxengine/math.git']]])

    stage "build"
    echo "Building from pipeline"
}

As a future reference for Jenkins 2.0 and Pipeline DSL please use built-in Snippet Generator or documentation.




回答2:


This can be done by using the directive of dir:

def exists = fileExists '<your target dir>'
if (!exists){
    new File('<your target dir>').mkdir()
}
dir ('<your target dir>') {
  git url: '<your git repo address>'
}



回答3:


First make clear that you are using Jenkins Job DSL.

You can do this like this:

    scm {
        git {
            wipeOutWorkspace(true)
            shallowClone(true);
            remote {
                url("xxxx....")
                relativeTargetDir('checkout-folder')
            }
        }
    }
  • https://jenkinsci.github.io/job-dsl-plugin/

This above address gives you the chance simply to type in upper left aread for example 'scm' and than it will show in which relationships 'scm' can be used. Than you can select 'scm-freestylejob' and afterwards click on the '***' than you can see the details.

The general start point for Jenkins Job DSL is here:

  • https://github.com/jenkinsci/job-dsl-plugin/wiki

You can of course ask here on SO or on Google Forum:

  • https://groups.google.com/forum/#!forum/job-dsl-plugin



回答4:


You are using the Pipeline Plugin, not the Job DSL Plugin. In the Pipeline Plugin, if you want to define something, where there is not yet a function available in the Pipeline syntax, you can define it yourself.




回答5:


    pipeline {

      agent any
      stages{
        stage("Checkout") {
            steps {
                    dir('def exists = fileNotExists \'git\'') {
                      bat label: '', script: 'sh "mkdir.sh'
                    }
                    dir ('cm') {
                        git branch: 'dev',
                        credentialsId: '<your credential id>',
                        url: '<yours git url>'
                    }
                }
            } //End of Checkout stage
        stage("TestShellScript") {
            steps {
                bat label: '', script: 'sh "PrintNumber.sh"'
            }
          }
        }//End of stages
    } // End of pipeline


Note: cat mkdir.sh
#!/bin/bash
#Create a directory
mkdir git


来源:https://stackoverflow.com/questions/36288723/jenkinfile-dsl-how-to-specify-target-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!