summaryrefslogtreecommitdiff
path: root/doc/Jenkinsfile.nexus-example
blob: c973def7b5bae1f9feb4bca591849d51860a840e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 <distributionManagement> 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
//                     '''
//                 }
//             }
//         }
//     }
// }