参考答案:
在 Jenkins 中,通过 Git 插件 可以轻松地克隆 Git 存储库并在流水线中使用。以下是通过 Jenkins 克隆 Git 存储库的步骤:
配置全局 Git 设置:
/usr/bin/git
或 C:\Program Files\Git\bin\git.exe
)。设置凭据(如果需要):
https://github.com/username/repository.git
git@github.com:username/repository.git
git
步骤来克隆 Git 仓库。1pipeline { 2 agent any 3 4 stages { 5 stage('Clone Repository') { 6 steps { 7 git branch: 'main', url: 'https://github.com/username/repository.git' 8 } 9 } 10 } 11}
main
或 develop
)。1git credentialsId: 'my-credential-id', branch: 'main', url: 'https://github.com/username/repository.git'
1pipeline { 2 agent any 3 4 environment { 5 GIT_REPO = 'https://github.com/username/repository.git' // Git 存储库地址 6 BRANCH = 'main' // 分支 7 } 8 9 stages { 10 stage('Checkout Code') { 11 steps { 12 git credentialsId: 'my-credential-id', url: "${GIT_REPO}", branch: "${BRANCH}" 13 } 14 } 15 16 stage('Build') { 17 steps { 18 echo 'Building project...' 19 sh 'mvn clean install' 20 } 21 } 22 23 stage('Test') { 24 steps { 25 echo 'Running tests...' 26 sh 'mvn test' 27 } 28 } 29 30 stage('Deploy') { 31 steps { 32 echo 'Deploying project...' 33 sh './deploy.sh' 34 } 35 } 36 } 37 38 post { 39 always { 40 echo 'Cleaning up...' 41 // 例如清理工作、发送通知等 42 } 43 } 44}
url
指定 Git 存储库的 URL,branch
用来指定要克隆的分支。最近更新时间:2024-12-25