Skip to content

Instantly share code, notes, and snippets.

@gedharizka
Created September 29, 2024 16:03
Show Gist options
  • Save gedharizka/0552a4e2e4f83d7b8c421e56ba479fc5 to your computer and use it in GitHub Desktop.
Save gedharizka/0552a4e2e4f83d7b8c421e56ba479fc5 to your computer and use it in GitHub Desktop.
Jenkins-create-repo gitlab
node {
// Definisikan environment variables
def gitlabApiUrl = 'https://gitlab.com/api/v4/projects'
def gitlabGroupId = 'xxxxx' // ID atau nama grup di GitLab
def repoName = 'repo-test' // Nama repository yang ingin dibuat
def visibility = 'private' // Bisa diubah ke 'public' jika diperlukan
stage('Create GitLab Repository') {
try {
// Menjalankan shell command untuk membuat repo di GitLab
// credentialsId -> from credentials jenkins, variable: create variable to use
withCredentials([string(credentialsId: 'gitlab-cred', variable: 'gitlab_token')]){
sh """
curl --request POST \
--header "PRIVATE-TOKEN: ${gitlab_token}" \
--header "Content-Type: application/json" \
--data '{"name": "${repoName}", "namespace_id": "${gitlabGroupId}", "visibility": "${visibility}"}' \
${gitlabApiUrl}
"""
}
echo "Repository ${repoName} created successfully."
} catch (Exception e) {
// Jika terjadi error, tampilkan pesan error
echo "Failed to create repository: ${e.message}"
error("Pipeline failed due to error in repository creation.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment