-1

I have this Jenkinsfile which I want to use to build a pipeline:

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
            }
        }
    }
}

How I can deploy the docker image build by Jenkins server and pushed in Nexus repository? If possible I want to use service account with token?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • You would really need to use the Docker plugin for Jenkins Pipeline. Otherwise I am unsure how you could store the resultant Docker image artifact as an object in the code. – Matthew Schuchard Jan 19 '23 at 13:38

1 Answers1

1

Instead of using 'nexusArtifactUploader', why don´t you use docker push, like you do to build the image?

I guess nexusArtifactUploader uses Nexus API and doesn´t work with docker images, but you can access the registry using docker and the exposed port (defaults to 5000)

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

You may also change docker build command to build the image using your registry name (or tag it after building, see How to push a docker image to a private repository)

Atxulo
  • 466
  • 3
  • 7