Jenkinfile DSL how to specify target directory

后端 未结 5 1061
予麋鹿
予麋鹿 2021-02-18 14:17

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\"])

           


        
相关标签:
5条回答
  • 2021-02-18 14:35

    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
    0 讨论(0)
  • 2021-02-18 14:42

    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>'
    }
    
    0 讨论(0)
  • 2021-02-18 14:44

    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.

    0 讨论(0)
  • 2021-02-18 14:48
        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
    
    0 讨论(0)
  • 2021-02-18 14:50

    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.

    0 讨论(0)
提交回复
热议问题