|
| 1 | +podTemplate(containers: [ |
| 2 | + containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'), |
| 3 | + containerTemplate(name: 'buildkit', image: 'moby/buildkit:master', ttyEnabled: true, privileged: true), |
| 4 | + ], volumes: [ |
| 5 | + secretVolume(secretName: '[[ .ImageRepoDockerSecret ]]', mountPath: '/root/.docker') |
| 6 | + ]) { |
| 7 | + node(POD_LABEL) { |
| 8 | + stage("Get Project") { |
| 9 | + checkout scm |
| 10 | + } |
| 11 | + stage('Run Maven test') { |
| 12 | + gitlabCommitStatus("test") { |
| 13 | + container('maven') { |
| 14 | + stage('run mvn test') { |
| 15 | + sh 'mvn -B test' |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + stage("Build Docker image") { |
| 21 | + gitlabCommitStatus("build image") { |
| 22 | + container('buildkit') { |
| 23 | + stage('build a Maven project') { |
| 24 | + String opts = "" |
| 25 | + String imageRepo = "[[ .imageRepo.user ]]/[[ .AppName ]]" |
| 26 | + String imageURL = "[[ .imageRepo.url ]]" |
| 27 | + if (imageURL) { |
| 28 | + imageRepo = "${imageURL}/${imageRepo}" |
| 29 | + } |
| 30 | + if (imageRepo.contains("http://")) { |
| 31 | + opts = ",registry.insecure=true" |
| 32 | + imageRepo = imageRepo.replace("http://", "") |
| 33 | + } |
| 34 | + String version |
| 35 | + if (env.GIT_COMMIT) { |
| 36 | + version = env.GIT_COMMIT.substring(0, 8) |
| 37 | + } else { |
| 38 | + sh "git config --global --add safe.directory '*'" |
| 39 | + String gitCommitLang = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true) |
| 40 | + version = gitCommitLang.substring(0, 8) |
| 41 | + } |
| 42 | + sh """ |
| 43 | + buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:latest,push=true${opts} |
| 44 | + buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${version},push=true${opts} |
| 45 | + """ |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments