簡體   English   中英

使用 jenkins 管道腳本標記 git repo

[英]Tagging a git repo using jenkins pipeline script

我正在嘗試使用 Jenkins 管道腳本標記 git repo。 我對詹金斯管道腳本很陌生。

是否有像下面這樣的命令來標記分支,用於簽出分支

git branch: 'master',
  credentialsId: '12345-1234-4696-af25-123455',
  url: 'ssh://git@bitbucket.org:company/repo.git'

git命令是checkout步驟的簡寫 它只能從 repo 克隆。

如果要執行通用 git 命令,則需要設置連接憑據。 對於 SSH,最簡單的方法是使用SSH 代理插件 對於某些操作,您還需要配置本地 git 用戶屬性。

例子:

# configure git
sh '''
  git config --global user.email 'my@company.org'
  git config --global user.name 'This Is Me'
  git tag this-very-version
'''

# enable remote connection
sshagent (credentials: ['your-credentials-to-bitbucket']) {
  sh 'git push origin this-very-version'
}
#!groovy
import java.text.SimpleDateFormat
def gitRepo = "${env.REPO_NAME}"
def gitBranch = "${env.BRANCH_NAME}"
def gitCredentialsId = "b5aeddba-e2d2-4415-aab3-9cb3ec62fd65"
def snapshotVersion = ''
pipeline {
    agent { label "Jenkins-Node-1" }
    stages {
        stage('Pull code from GIT') {
            steps {
                script {
                    cleanWs()
                    steps.git branch: gitBranch, url:gitRepo, credentialsId: gitCredentialsId
                    stash includes: '**', name: 'workspace'
                }
            }
        }
        stage('Build Code') {
            steps {
                script {
                    unstash 'workspace'
                    sh '''
                        export JAVA_HOME="/usr/lib/jvm/java-11-amazon-corretto.x86_64"
                        mvn clean install
                    '''
                }
            }
        }
        stage('Initialize GIT') {
            steps {
                script {
                def remoteOrigin =gitRepo.replace('https://','')
                snapshotVersion = sh(script:"""xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml""", returnStdout:true).trim()
                withCredentials([usernamePassword(credentialsId: gitCredentialsId, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                        def uri ="""'https://${GIT_USERNAME}:${GIT_PASSWORD}@${remoteOrigin}'"""
                        sh('git config --global user.email "shikhas@ayraa.io"')
                        sh('git config --global user.name "shikhasingh"')
                        sh("git remote -v")
                    }
                }
            }
        }
        stage('Create release tag') {
            steps {
                script {
                def date = new Date()
                sdf = new SimpleDateFormat("dd-MM-yyyy")
                //snapshotVersion = sh(script:"""xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml""", returnStdout:true).trim()
                println("Date is: "+sdf.format(date))
                def TAG="tag-${sdf.format(date)}"
                echo "TAG is : ${TAG}"
                sh """
                     echo "TAG is : ${TAG}"
                     git tag -a ${TAG} -m "tag: ${TAG} is created"
                     echo "*** Created tag ${TAG} in ${gitBranch}"
                     git push origin ${TAG}

                """
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM