美文网首页
Jenkins Pipeline 之 ( checkout sc

Jenkins Pipeline 之 ( checkout sc

作者: 偷油考拉 | 来源:发表于2023-08-16 16:48 被阅读0次

一、checkout scm 在 Jenkins Pipeline Syntax 中的解释

checkout scm 只在 使用 Multibranch Pipeline or Pipeline script from SCM 的时候生效。如下图:

image.png

Pipeline Syntax (流水线语法参考)Global Variable Reference (全局变量参考部分),对 SCM 作如下说明:
-- Represents the SCM configuration in a multibranch project build. Use checkout scm to check out sources matching Jenkinsfile.
-- 表示多分支项目生成中的SCM配置。使用checkout scm签出与Jenkinsfile匹配的源。
-- You may also use this in a standalone project configured with Pipeline from SCM, though in that case the checkout will just be of the latest revision in the branch, possibly newer than the revision from which the Pipeline was loaded.
-- 在使用 Pipeline from SCM 的独立项目中也可以使用此选项,但是在这种情况下签出的是该分支中的最新版本,可能比Pipeline加载的版本新。

https://plugins.jenkins.io/workflow-scm-step/
https://doc.nuxeo.com/corg/jenkins-pipeline-usage/
https://www.jenkins.io/doc/pipeline/steps/workflow-scm-step/#pipeline-scm-step
https://linuxhint.com/jenkinsfile-checkout-scm/

二、How to Customize Checkout for Pipeline Multibranch? - CloudBees

node {
    checkout scm
}
node {
    checkout([
         $class: 'GitSCM',
         branches: scm.branches,
         doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
         extensions: scm.extensions,
         userRemoteConfigs: scm.userRemoteConfigs
    ])
 }

三、范例 https://naiveskill.com/jenkins-pipeline-github/

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git url: 'https://github.com/naiveskill/devops.git', branch: 'main'
                // Change file permisson
                sh "chmod +x -R ./jenkins"
                // Run shell script
                sh "./jenkins/script/scripted_pipeline_ex_2.sh"
            }
        }
    }
}
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Get code from a GitHub repository
                git url: 'https://github.com/naiveskill/devops_cred.git', branch: 'main',
                 credentialsId: 'github_creds'
            }
        }
    }
}
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
              checkout([$class: 'GitSCM', 
                branches: [[name: '*/main']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [[$class: 'CleanCheckout']],
                submoduleCfg: [], 
                userRemoteConfigs: [[url: 'https://github.com/naiveskill/devops.git']]])
              sh "ls -ltr"
          }
        }
    }
}
pipeline {
    agent any
    options {
        skipDefaultCheckout true
    }

    stages {
        stage('Clone Repository') {
            steps {
                git branch: "${env.BRANCH_NAME}", url: "${env.REPO_URL}"
            }
        }

        stage('Build') {
            steps {
                sh './gradlew build'
            }
        }

        stage('Test') {
            steps {
                sh './gradlew test'
            }
        }

        stage('Deploy') {
            steps {
                sh './deploy.sh'
            }
        }
    }

    post {
        always {
            githubStatus context: 'continuous-integration/jenkins', state: 'success'
            if (env.CHANGE_ID) {
                githubComment message: "The pipeline completed successfully!"
                githubLabel labels: ['approved']
            }
        }
    }
}

四、范例

checkout([ 
  $class: 'GitSCM', 
  branches: [[name: '*/develop']], 
  doGenerateSubmoduleConfigurations: false, 
  extensions: [
    [ 
      $class: 'SubmoduleOption', 
      depth: 1, 
      disableSubmodules: false, 
      parentCredentials: true, 
      recursiveSubmodules: true, 
      reference: '', 
      shallow: true, 
      trackingSubmodules: false
    ], 
    [$class: 'GitLFSPull']
  ], 
  submoduleCfg: [], 
  userRemoteConfigs: [[ credentialsId: '$creds', url: '$repo']]
]);

相关文章

网友评论

      本文标题:Jenkins Pipeline 之 ( checkout sc

      本文链接:https://www.haomeiwen.com/subject/rtqqpdtx.html