jenkins 从Nexus注册表部署Docker映像

rks48beu  于 2023-01-20  发布在  Jenkins
关注(0)|答案(1)|浏览(172)

我有一个Jenkinsfile,我想用它来构建一个管道:

pipeline {
    agent any
    environment {
        NEXUS_VERSION = "nexus3"
        NEXUS_PROTOCOL = "http"
        NEXUS_URL = "you-ip-addr-here:8081"
        NEXUS_REPOSITORY = "maven-nexus-repo"
        NEXUS_CREDENTIAL_ID = "nexus-user-credentials"
    }
    stages {
        stage('Download Helm Charts') {
            steps {
                sh "echo 'Downloading Helm Charts from Bitbucket repository...'"
                // configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
                // not sure do I need to point the root folder of the Helm repository or only the single chart
                
                checkout scmGit(
                  branches: [[name: 'master']],
                  userRemoteConfigs: [[credentialsId: 'c2672602-dfd5-4158-977c-5009065c867e',
                    url: 'http://192.168.1.30:7990/scm/jen/helm.git']])
            }
        }
        stage('Test Kubernetes version') {
            steps {
                sh "echo 'Checking Kubernetes version..'"
                // How to do remote test of kubernetes version
            }
        }
        stage('Push Helm Charts to Kubernetes') {
            steps {
                sh "echo 'building..'"
                // push here helm chart from Jenkins server to Kubernetes cluster
            }
        }     
        stage('Build Image') {
            steps {
                sh "echo 'building..'"
                // configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
                git credentialsId: 'bitbucket-server:50001e738fa6dafbbe7e336853ced1fcbc284fb18ea8cda8b54dbfa3a7bc87b9', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'

                // execute Java -jar ... and build docker image
                ./gradlew build && java -jar build/libs/gs-spring-boot-docker-0.1.0.jar

                docker build -t springio/gs-spring-boot-docker .
            }
        }
        stage('Push Image into Nexus registry') {
            steps {
                sh "echo 'building..'"
                // push compiled docker image into Nexus repository
                
                script {
                    pom = readMavenPom file: "pom.xml";
                    filesByGlob = findFiles(glob: "target/*.${pom.packaging}");
                    echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"
                    artifactPath = filesByGlob[0].path;
                    artifactExists = fileExists artifactPath;
                    if(artifactExists) {
                        echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";
                        nexusArtifactUploader(
                            nexusVersion: NEXUS_VERSION,
                            protocol: NEXUS_PROTOCOL,
                            nexusUrl: NEXUS_URL,
                            groupId: pom.groupId,
                            version: pom.version,
                            repository: NEXUS_REPOSITORY,
                            credentialsId: NEXUS_CREDENTIAL_ID,
                            artifacts: [
                                [artifactId: pom.artifactId,
                                classifier: '',
                                file: artifactPath,
                                type: pom.packaging],
                                [artifactId: pom.artifactId,
                                classifier: '',
                                file: "pom.xml",
                                type: "pom"]
                            ]
                        );
                    } else {
                        error "*** File: ${artifactPath}, could not be found";
                    }
                }
            }
        }
        stage('Deploy Image from Nexus registry into Kubernetes') {
            steps {
                sh "echo 'building..'"
            }
        }
        stage('Test'){
            steps {
                sh "echo 'Testing...'"
                // implement a check here is it deployed sucessfully
            }
        }
    }
}

如何部署Jenkins服务器构建的Docker映像并将其推入Nexus存储库?如果可能,我希望使用带令牌的服务帐户?

sbdsn5lh

sbdsn5lh1#

与其使用“nexusArtifactUploader”,为什么不像构建映像那样使用Docker push呢?
我猜nexusArtifactUploader使用Nexus API,不能与Docker图像一起工作,但是可以使用Docker和暴露的端口(默认为5000)访问注册表

withCredentials([string(credentialsId: NEXUS_CREDENTIAL_ID, variable: 'registryToken')]) {
  sh 'docker push --creds default:${registryToken} your-registry-url/image-name:image-tag'
}

您也可以更改docker build命令以使用您的注册表名构建映像(或在构建后对其进行标记,请参见How to push a docker image to a private repository

相关问题