Jenkinsfile集成制品库nexus

IT
IT
IT
405
文章
1
评论
2021年3月25日22:29:20 评论 2,877 2902字阅读9分40秒

上传制品

我们需要在nexus中创建一个新的仓库maven-hostd,同时我们需要在maven的配置文件settings.xml中配置maven-hostd认证。

[root@jenkins ~]# cd /usr/local/apache-maven-3.6.3/conf/
[root@jenkins conf]# ls
logging settings.xml settings.xmlback toolchains.xml
[root@jenkins conf]# vim settings.xml
 <server>
<id>devtest</id>
<username>admin</username>
<password>admin123</password>
</server>

Jenkinsfile集成制品库nexus

  • 编写jenkinsfile
#!groovy

@Library('jenkinslibrary@master') _

//func from shareibrary
def build = new org.devops.build()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def nexus = new org.devops.nexus()

def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"



if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    
    currentBuild.description = "Trigger by ${userName} ${branch}"
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
    env.runOpts = "GitlabPush"

    
} else {
   userEmail = "lucky@centoscn.vip"
}


//pipeline
pipeline{
    agent { node { label "master"}}
    
    
    stages{

        stage("检出代码"){
            steps{
                script{
                   
                    
                    println("${branchName}")
                
                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], 
                                      doGenerateSubmoduleConfigurations: false, 
                                      extensions: [], 
                                      submoduleCfg: [], 
                                      userRemoteConfigs: [[credentialsId: 'ef208191-527c-4630-a677-b81696446562', url: "${srcUrl}"]]])

                }
            }
        }
        stage("编译打包"){
            steps{
                script{
                
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)
                    nexus.main("nexus")
                }
            }
       }
        stage("代码扫描"){
            steps {
                 script{
                     tools.PrintMes("代码扫描","green")
                     sonar.SonarScan("${JOB_NAME}","${JOB_NAME}","src")
                }
            } 
        }          
    }
    post {
        always{
            script{
                println("always")
            }
        }
        
        success{
            script{
                println("success")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                }
                toemail.Email("流水线成功",userEmail)
            
            }
        
        }
        failure{
            script{
                println("failure")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
                }
                toemail.Email("流水线失败了!",userEmail)
            }
        }
        
        aborted{
            script{
                println("aborted")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
                }
               toemail.Email("流水线被取消了!",userEmail)
            }
        
        }
    
    }
    
    
}
  • 集成共享库
pakcage org.devops


//获取POM中的坐标
def GetGav(){
   //上传制品
    def jarName = sh returnStdout: true, script: "cd target;ls *.jar"
    env.jarName = jarName - "\n"
    
    def pom = readMavenPom file: 'pom.xml'
    env.pomVersion = "${pom.version}"
    env.pomArtifact = "${pom.artifactId}"
    env.pomPackaging = "${pom.packaging}"
    env.pomGroupId = "${pom.groupId}"
    
    println("${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}")

    return ["${pomGroupId}","${pomArtifact}","${pomVersion}","${pomPackaging}"]
}


//Nexus plugin deploy
def NexusUpload(){
    def mvnHome = tool "M2"
    sh  """ 
        cd target/
        ${mvnHome}/bin/mvn deploy:deploy-file -Dmaven.test.skip=true  \
                                -Dfile=${jarName} -DgroupId=${pomGroupId} \
                                -DartifactId=${pomArtifact} -Dversion=${pomVersion}  \
                                -Dpackaging=${pomPackaging} -DrepositoryId=devtest \
                                -Durl=http://192.168.90.4:8081/repository/devtest 
        """
}


def main(uploadType){
    GetGav()
    if ("${uploadType}" == "nexus"){
        NexusUpload()
    }
}
继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2021年3月25日22:29:20
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
jenkinsfile集成阿里云镜像仓库 jenkins-ci/cd

jenkinsfile集成阿里云镜像仓库

创建Jenkins认证授权 ID也配置成aliyun-registry-admin 阿里云镜像库有个问题,在设置固定密码后,使用的是本账号名字。本账号的密码和镜像的密码也就不一样了。所以要么单独创建一...

您必须才能发表评论!