Skip to content

Instantly share code, notes, and snippets.

@cemtopkaya
Last active May 28, 2023 14:23
Show Gist options
  • Save cemtopkaya/5624a179e2d05acc020f72fc3c76cb3b to your computer and use it in GitHub Desktop.
Save cemtopkaya/5624a179e2d05acc020f72fc3c76cb3b to your computer and use it in GitHub Desktop.
Jenkinsfile içinde repoyu çekmeden önce temizleme, credentials("credId") ile kullanıcı ad ve şifresine erişme, git komutlarını sh ile çalıştırma
@Library('gui_multi_repo@master')_
def RepoUrl = 'https://cem.topkaya@bitbucket.ulakhaberlesme.com.tr:8443/scm/~cem.topkaya/cinar_smsf_gui.git'
pipeline {
agent {
docker {
label "DockerAgent"
image "node:14.21.3"
}
}
parameters {
string(trim: true, name: 'REPO_URL', defaultValue: RepoUrl, 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
* Örneğin verilen credId'nin kullanıcı bilgilerini USERNAME ve PASSWORD değişkenleri içine atar,
* kullanıcı adı ve şifresi olacak ve tüm çıktılarda *** ile gizlenir.
* Bunu withCredentials fonksiyonu içinde de kullanabilirsiniz.
*/
// def gitCredentials = credentials("${credId}")
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}@alanadi.com.tr"
# 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ı"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment