// Example Jenkinsfile — uploading artifacts to Nexus Repository Manager // // Prerequisites: // 1. In Nexus: create a "raw (hosted)" repository named "artifacts" // (Settings > Repositories > Create Repository > raw (hosted)) // 2. In Jenkins: add Nexus credentials // (Manage Jenkins > Credentials > Add > Username with password, ID: "nexus-credentials") // // Nexus supports several repository formats. Pick the one that fits your project: // // - raw (hosted) — any file (binaries, tarballs, logs). Simplest option. // - maven2 (hosted) — Java/Maven artifacts (.jar, .pom) // - docker (hosted) — Docker images (use `docker push` instead, see below) pipeline { agent { docker { image 'maven:3.9-eclipse-temurin-17' } } environment { NEXUS_URL = 'https://nexus.swave.lol' NEXUS_CREDS = credentials('nexus-credentials') } stages { stage('Build') { steps { sh 'mvn clean package -DskipTests' } } stage('Test') { steps { sh 'mvn test' } } // Option 1: Upload a generic file to a "raw" repository stage('Upload to Nexus (raw)') { steps { sh ''' curl -u "$NEXUS_CREDS" \ --upload-file target/myapp-1.0.jar \ "$NEXUS_URL/repository/artifacts/myapp/${BUILD_NUMBER}/myapp-1.0.jar" ''' } } // Option 2: Deploy a Maven artifact using mvn deploy // Requires in pom.xml pointing to Nexus, // or use -DaltDeploymentRepository on the command line: // // stage('Deploy to Nexus (Maven)') { // steps { // sh ''' // mvn deploy \ // -DskipTests \ // -DaltDeploymentRepository=nexus::default::${NEXUS_URL}/repository/maven-releases/ // -s settings.xml // ''' // } // } } } // ------------------------------------------------------------------- // Docker image example (separate pipeline) // ------------------------------------------------------------------- // To push a Docker image to the Nexus Docker registry, use a pipeline // like this. Requires Docker socket access (DooD) — no Maven needed. // // pipeline { // agent any // // environment { // REGISTRY = 'registry.swave.lol' // IMAGE = "${REGISTRY}/myapp:${BUILD_NUMBER}" // } // // stages { // stage('Build Image') { // steps { // sh "docker build -t ${IMAGE} ." // } // } // // stage('Push to Registry') { // steps { // withCredentials([usernamePassword( // credentialsId: 'nexus-credentials', // usernameVariable: 'USER', // passwordVariable: 'PASS' // )]) { // sh ''' // echo "$PASS" | docker login $REGISTRY -u "$USER" --password-stdin // docker push $IMAGE // docker logout $REGISTRY // ''' // } // } // } // } // }