One of my jobs I used this:
def gitCheckoutExtensions = []
if (cleanWorkspace) {
println '>>> workspace will be cleaned and retrieved all codes again..!'
gitCheckoutExtensions.push([$class: 'CleanBeforeCheckout'])
}
gitCheckoutExtensions.push([$class: 'LocalBranch', localBranch: srcBranch])
checkout([
$class: 'GitSCM',
branches: [[name: "*/${srcBranch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: gitCheckoutExtensions,
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: credId,
url: url
]
]
])
Longer form of stage is:
@Library('gui_multi_repo@master')_
pipeline {
agent {
docker {
label "DockerAgent"
image "node:14.21.3"
}
}
parameters {
string(trim: true, name: 'REPO_URL', defaultValue: "http://user.name@server_domain_or_ipAddres", description: 'Repo adresi')
string(trim: true, name: 'REPO_CRED_ID', defaultValue: RepoCredId, description: 'GIT Repo bağlantısı olacaksa CRED_ID kullanılacak')
string(trim: true, name: 'REPO_SOURCE_BRANCH_NAME', defaultValue: 'refs/remotes/origin/developer', description: 'Kodları hangi BRANCH üstünden çekeceğini belirtiyoruz')
string(trim: true, name: 'REPO_TARGET_BRANCH_NAME', defaultValue: 'refs/remotes/origin/master', description: 'Push ile kodun gönderileceği branch')
}
stages {
stage("Git İşlemleri") {
steps {
script {
def credId="${params.REPO_CRED_ID}"
def repoUrl="${params.REPO_URL}"
def srcBranch="${params.REPO_SOURCE_BRANCH_NAME}"
def targetBranch="${params.REPO_TARGET_BRANCH_NAME}"
def tagName="cem13"
def tagMessage="naber13"
echo "credId: $credId"
echo "repoUrl: $repoUrl"
echo "srcBranch: $srcBranch"
echo "targetBranch: $targetBranch"
echo "tagName: $tagName"
echo "tagMessage: $tagMessage"
/**
* CredId ile tanımlanan Git kimlik bilgilerini alır
* Artık GIT_USERNAME ve GIT_PASSWORD değişkenleri içinde, verilen credId'nin
* kullanıcı adı ve şifresi olacak ve tüm çıktılarda *** ile gizlenecek
*/
checkout([$class: 'GitSCM',
branches: [[name: "${srcBranch}"]],
extensions: [
[$class: 'CleanBeforeCheckout']
],
userRemoteConfigs: [[
// credId veya gitCredentials.id ile tanımlanan kullanıcı bilgilerinin id değeri verilir
credentialsId: "${credId}",
url: "${repoUrl}"
]]
])
withCredentials([usernamePassword(credentialsId: "${credId}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
def gitUrlWithCredentials = repoUrl.replaceFirst(/(http[s]:\/\/).*@(.*)/,"\$1${GIT_USERNAME}:${GIT_PASSWORD}@\$2")
echo "gitUrlWithCredentials: ${gitUrlWithCredentials}"
sh """
#!/bin/sh -e
git config --global credential.helper cache
git config --global push.default simple
# Kullanıcı e-posta adresini ayarlar
git config --global user.email "${GIT_USERNAME}@domain.com"
# Kullanıcı adını ayarlar
git config --global user.name "${GIT_USERNAME.replace('.', ' '}"
# SSL Doğrulama yapmaz
git config --global http.sslverify 'false'
# Git URL'sinde parolayı kullanarak güncellenmiş URL'yi oluşturun
git tag -a ${tagName} -m '${tagMessage}'
git push ${gitUrlWithCredentials} ${tagName}
git checkout ${targetBranch}
# sonra birleştireceğimiz 'kaynak dalını' isteyelim
git merge ${srcBranch}
git push ${gitUrlWithCredentials} ${targetBranch}
"""
}
}
}
}
}
post {
success{
echo "** Süreç başarıyla tamamlandı"
}
failure {
echo "** Süreç hatalı tamamlandı"
}
cleanup{
echo "** Süreç tamamlandı cleanup zamanı"
}
always {
echo "** Süreç günahıyla sevabıyla tamamlandı"
}
}
}