From e0786006d7fbff051246faeb4d4e4ceb3e25701b Mon Sep 17 00:00:00 2001 From: Arne Limburg Date: Thu, 24 Aug 2023 18:45:12 +0200 Subject: [PATCH] update apps to pipeline --- address-validation-service/Jenkinsfile | 71 ++++++++++----- address-validation-service/pom.xml | 87 ++---------------- .../meecrowave/conf/meecrowave.properties | 5 ++ billing-service/Jenkinsfile | 71 ++++++++++----- billing-service/pom.xml | 81 +---------------- .../sample/address/domain/AddressLine.java | 3 +- .../meecrowave/conf/meecrowave.properties | 5 ++ .../address/BillingAddressServiceTest.java | 6 +- customer-service/Dockerfile | 1 + customer-service/Jenkinsfile | 54 ++++++++---- customer-service/pom.xml | 8 ++ .../meecrowave/conf/meecrowave.properties | 5 ++ delivery-service/Dockerfile | 1 + delivery-service/Jenkinsfile | 88 ++++++++++++++----- delivery-service/pom.xml | 12 ++- .../meecrowave/conf/meecrowave.properties | 5 ++ .../address/DeliveryAddressServiceTest.java | 5 +- 17 files changed, 264 insertions(+), 244 deletions(-) create mode 100644 address-validation-service/src/main/meecrowave/conf/meecrowave.properties create mode 100644 billing-service/src/main/meecrowave/conf/meecrowave.properties create mode 100644 customer-service/src/main/meecrowave/conf/meecrowave.properties create mode 100644 delivery-service/src/main/meecrowave/conf/meecrowave.properties diff --git a/address-validation-service/Jenkinsfile b/address-validation-service/Jenkinsfile index 65ed856..b82ef0a 100644 --- a/address-validation-service/Jenkinsfile +++ b/address-validation-service/Jenkinsfile @@ -2,6 +2,10 @@ pipeline { agent any + parameters { + booleanParam(name: 'verifyPacts', defaultValue: false, description: 'should this job just run to verify pacts from a consumer') + } + options { disableConcurrentBuilds() } @@ -12,6 +16,7 @@ pipeline { PERFORM_RELEASE = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') && env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ')}" RELEASE_VERSION = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') ? env.SNAPSHOT_VERSION.substring(0, env.SNAPSHOT_VERSION.lastIndexOf('-SNAPSHOT')) : SNAPSHOT_VERSION}" VERSION = "${env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ') ? env.RELEASE_VERSION : env.SNAPSHOT_VERSION}" + ROOT_DIRECTORY = "${params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT") ? 'target/checkout' : '.'}" } triggers { @@ -26,19 +31,29 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { echo "Building version ${env.VERSION}" script { - if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { + if (params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT")) { + int previousRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) - 1 + if (previousRevision >= 0) { + previousVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + previousRevision + sh "mvn scm:checkout -DscmVersionType=tag -DscmVersion=${previousVersion}" + echo "Testing against version ${previousVersion}" + } + } else if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn versions:set -DnewVersion=${env.RELEASE_VERSION} -B" - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.RELEASE_VERSION}/g' deployment/overlays/prod/kustomization.yaml" - } else { - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.GIT_COMMIT}/g' deployment/overlays/test/kustomization.yaml" } } - sh 'mvn clean test-compile -B' + sh """ + cd ${ROOT_DIRECTORY} + mvn clean test-compile -B + """ } } stage ('Test') { @@ -48,10 +63,16 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { - sh "mvn test -DpactBroker.url=http://pact-service:8080 -Dpact.verifier.publishResults=true -B" + sh """ + cd ${ROOT_DIRECTORY} + mvn test -DpactBroker.url=http://pact:9292 -Dpact.verifier.publishResults=true -B + """ } } stage ('Package') { @@ -62,10 +83,12 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh 'mvn package -DskipTests -B' - sh 'docker build -t address-validation .' + sh 'mvn package meecrowave:bundle -DskipTests -B' } } stage ('Push') { @@ -76,16 +99,13 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh """ - docker tag address-validation localhost:30010/address-validation:${env.VERSION} - docker tag address-validation localhost:30010/address-validation:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker tag address-validation localhost:30010/address-validation:${env.GIT_COMMIT} - docker push localhost:30010/address-validation:${env.VERSION} - docker push localhost:30010/address-validation:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker push localhost:30010/address-validation:${env.GIT_COMMIT} - """ + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/address-validation-service-meecrowave-distribution.zip jenkins@test:~/' + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/address-validation-service-meecrowave-distribution.zip jenkins@prod:~/' script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh 'git config --global user.name "Jenkins"' @@ -94,7 +114,6 @@ pipeline { sh "mvn scm:tag -Dtag=${env.RELEASE_VERSION} -B" int nextRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) + 1 nextVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + nextRevision + "-SNAPSHOT" - sh "sed -i 's/${env.RELEASE_VERSION}/${nextVersion}/g' deployment/overlays/prod/kustomization.yaml" sh "mvn versions:set scm:checkin -DnewVersion=${nextVersion} -Dmessage='update version to ${nextVersion}' -B" } } @@ -102,6 +121,9 @@ pipeline { } stage ('Deploy') { when { + expression { + params.verifyPacts == false + } anyOf { not { branch 'main' @@ -113,10 +135,19 @@ pipeline { script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn scm:checkout -DscmVersion=${env.RELEASE_VERSION} -DscmVersionType=tag -B" - sh 'kubectl apply -k deployment/overlays/prod' + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@prod "~/address-validation-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/address-validation-service-distribution\ + && unzip ~/address-validation-service-meecrowave-distribution.zip\ + && ~/address-validation-service-distribution/bin/meecrowave.sh start" + """ } else { - sh 'kubectl apply -k deployment/overlays/test' - sh "sed -i 's/${env.GIT_COMMIT}/${env.SNAPSHOT_VERSION}/g' deployment/overlays/test/kustomization.yaml" + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@test "~/address-validation-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/address-validation-service-distribution\ + && unzip ~/address-validation-service-meecrowave-distribution.zip\ + && ~/address-validation-service-distribution/bin/meecrowave.sh start" + """ } } } diff --git a/address-validation-service/pom.xml b/address-validation-service/pom.xml index 5ca5e40..7d48800 100644 --- a/address-validation-service/pom.xml +++ b/address-validation-service/pom.xml @@ -12,8 +12,8 @@ 4.0.0 de.openkonwledge.sample.shop - ${service.name} - 1.0.0-SNAPSHOT + address-validation-service + 1.1.0-SNAPSHOT scm:git:http://openknowledge:workshop@gogs:3000/openknowledge/address-validation-service.git @@ -21,11 +21,11 @@ - online-shop 11 11 false UTF-8 + http://localhost:5000 1.2.15 1.9.6 5.8.2 @@ -39,55 +39,11 @@ ${meecrowave.version} provided - - org.apache.meecrowave - meecrowave-core - ${meecrowave.version} - provided - - - org.apache.geronimo.specs - geronimo-validation_1.0_spec - 1.1 - - - org.eclipse.microprofile.config - microprofile-config-api - 1.4 - - - org.apache.geronimo.config - geronimo-config-impl - 1.2.2 - - - org.eclipse.microprofile.jwt - microprofile-jwt-auth-api - 1.2.1 - - - org.apache.geronimo - geronimo-jwt-auth - 1.0.4 - - - org.apache.deltaspike.modules - deltaspike-security-module-api - ${deltaspike.version} - compile - - - org.apache.deltaspike.modules - deltaspike-security-module-impl - ${deltaspike.version} - runtime - org.apache.commons commons-lang3 3.9 - org.junit.jupiter junit-jupiter-api @@ -106,11 +62,6 @@ 3.22.0 test - - org.mockito - mockito-core - 2.28.2 - au.com.dius.pact.provider junit5 @@ -123,22 +74,9 @@ ${meecrowave.version} test - - rocks.limburg.cdimock - cdimock - 1.0.4 - test - - - com.tngtech.archunit - archunit - 1.0.1 - test - - ${project.artifactId} org.apache.maven.plugins @@ -147,6 +85,7 @@ ${project.version} + ${pactBroker.url} @@ -159,24 +98,8 @@ maven-checkstyle-plugin 3.2.0 - true - ${project.basedir}/src/main/checkstyle/java.header - true - true + ${project.basedir}/src/main/meecrowave/conf - - - compile - compile - - checkstyle - - - ${project.basedir}/src/main/checkstyle/configuration.xml - true - - - diff --git a/address-validation-service/src/main/meecrowave/conf/meecrowave.properties b/address-validation-service/src/main/meecrowave/conf/meecrowave.properties new file mode 100644 index 0000000..fc05e42 --- /dev/null +++ b/address-validation-service/src/main/meecrowave/conf/meecrowave.properties @@ -0,0 +1,5 @@ +# This file contains the meecrowave default configuration +# More on http://openwebbeans.apache.org/meecrowave/meecrowave-core/cli.html + +tomcat-access-log-pattern = %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" +http=8084 diff --git a/billing-service/Jenkinsfile b/billing-service/Jenkinsfile index f1ec156..94f8257 100644 --- a/billing-service/Jenkinsfile +++ b/billing-service/Jenkinsfile @@ -2,6 +2,10 @@ pipeline { agent any + parameters { + booleanParam(name: 'verifyPacts', defaultValue: false, description: 'should this job just run to verify pacts from a consumer') + } + options { disableConcurrentBuilds() } @@ -12,6 +16,7 @@ pipeline { PERFORM_RELEASE = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') && env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ')}" RELEASE_VERSION = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') ? env.SNAPSHOT_VERSION.substring(0, env.SNAPSHOT_VERSION.lastIndexOf('-SNAPSHOT')) : SNAPSHOT_VERSION}" VERSION = "${env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ') ? env.RELEASE_VERSION : env.SNAPSHOT_VERSION}" + ROOT_DIRECTORY = "${params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT") ? 'target/checkout' : '.'}" } triggers { @@ -26,19 +31,29 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { echo "Building version ${env.VERSION}" script { - if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { + if (params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT")) { + int previousRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) - 1 + if (previousRevision >= 0) { + previousVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + previousRevision + sh "mvn scm:checkout -DscmVersionType=tag -DscmVersion=${previousVersion}" + echo "Testing against version ${previousVersion}" + } + } else if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn versions:set -DnewVersion=${env.RELEASE_VERSION} -B" - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.RELEASE_VERSION}/g' deployment/overlays/prod/kustomization.yaml" - } else { - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.GIT_COMMIT}/g' deployment/overlays/test/kustomization.yaml" } } - sh 'mvn clean test-compile -B' + sh """ + cd ${ROOT_DIRECTORY} + mvn clean test-compile -B + """ } } stage ('Test') { @@ -48,10 +63,16 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { - sh "mvn test -DpactBroker.url=http://pact-service:8080 -Dpact.verifier.publishResults=true -B" + sh """ + cd ${ROOT_DIRECTORY} + mvn test -DpactBroker.url=http://pact:9292 -Dpact.verifier.publishResults=true -B + """ } } stage ('Package') { @@ -62,10 +83,12 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh 'mvn package -Dmaven.test.skip=true -B' - sh 'docker build -t billing .' + sh 'mvn package meecrowave:bundle -DskipTests -B' } } stage ('Push') { @@ -76,16 +99,13 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh """ - docker tag billing localhost:30010/billing:${env.VERSION} - docker tag billing localhost:30010/billing:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker tag billing localhost:30010/billing:${env.GIT_COMMIT} - docker push localhost:30010/billing:${env.VERSION} - docker push localhost:30010/billing:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker push localhost:30010/billing:${env.GIT_COMMIT} - """ + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/billing-service-meecrowave-distribution.zip jenkins@test:~/' + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/billing-service-meecrowave-distribution.zip jenkins@prod:~/' script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh 'git config --global user.name "Jenkins"' @@ -94,7 +114,6 @@ pipeline { sh "mvn scm:tag -Dtag=${env.RELEASE_VERSION} -B" int nextRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) + 1 nextVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + nextRevision + "-SNAPSHOT" - sh "sed -i 's/${env.RELEASE_VERSION}/${nextVersion}/g' deployment/overlays/prod/kustomization.yaml" sh "mvn versions:set scm:checkin -DnewVersion=${nextVersion} -Dmessage='update version to ${nextVersion}' -B" } } @@ -102,6 +121,9 @@ pipeline { } stage ('Deploy') { when { + expression { + params.verifyPacts == false + } anyOf { not { branch 'main' @@ -113,10 +135,19 @@ pipeline { script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn scm:checkout -DscmVersion=${env.RELEASE_VERSION} -DscmVersionType=tag -B" - sh 'kubectl apply -k deployment/overlays/prod' + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@prod "~/billing-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/billing-service-distribution\ + && unzip ~/billing-service-meecrowave-distribution.zip\ + && ~/billing-service-distribution/bin/meecrowave.sh start" + """ } else { - sh 'kubectl apply -k deployment/overlays/test' - sh "sed -i 's/${env.GIT_COMMIT}/${env.SNAPSHOT_VERSION}/g' deployment/overlays/test/kustomization.yaml" + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@test "~/billing-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/billing-service-distribution\ + && unzip ~/billing-service-meecrowave-distribution.zip\ + && ~/billing-service-distribution/bin/meecrowave.sh start" + """ } } } diff --git a/billing-service/pom.xml b/billing-service/pom.xml index f112812..a54a77c 100644 --- a/billing-service/pom.xml +++ b/billing-service/pom.xml @@ -12,7 +12,7 @@ 4.0.0 de.openkonwledge.sample.shop - ${service.name} + billing-service 1.0.0-SNAPSHOT @@ -21,11 +21,11 @@ - online-shop 11 11 false UTF-8 + http://localhost:5000 1.2.15 1.9.6 5.8.2 @@ -39,55 +39,16 @@ ${meecrowave.version} provided - - org.apache.meecrowave - meecrowave-core - ${meecrowave.version} - provided - org.apache.geronimo.specs geronimo-validation_1.0_spec 1.1 - - org.eclipse.microprofile.config - microprofile-config-api - 1.4 - - - org.apache.geronimo.config - geronimo-config-impl - 1.2.2 - - - org.eclipse.microprofile.jwt - microprofile-jwt-auth-api - 1.2.1 - - - org.apache.geronimo - geronimo-jwt-auth - 1.0.4 - - - org.apache.deltaspike.modules - deltaspike-security-module-api - ${deltaspike.version} - compile - - - org.apache.deltaspike.modules - deltaspike-security-module-impl - ${deltaspike.version} - runtime - org.apache.commons commons-lang3 3.9 - org.junit.jupiter junit-jupiter-api @@ -106,12 +67,6 @@ 3.22.0 test - - org.mockito - mockito-core - 2.28.2 - test - au.com.dius.pact.provider junit5 @@ -124,22 +79,9 @@ ${meecrowave.version} test - - rocks.limburg.cdimock - cdimock - 1.0.4 - test - - - com.tngtech.archunit - archunit - 1.0.1 - test - - ${project.artifactId} org.apache.maven.plugins @@ -148,6 +90,7 @@ ${project.version} + ${pactBroker.url} @@ -160,24 +103,8 @@ maven-checkstyle-plugin 3.2.0 - true - ${project.basedir}/src/main/checkstyle/java.header - true - true + ${project.basedir}/src/main/meecrowave/conf - - - compile - compile - - checkstyle - - - ${project.basedir}/src/main/checkstyle/configuration.xml - true - - - diff --git a/billing-service/src/main/java/de/openknowledge/sample/address/domain/AddressLine.java b/billing-service/src/main/java/de/openknowledge/sample/address/domain/AddressLine.java index 864e588..94c5ee6 100644 --- a/billing-service/src/main/java/de/openknowledge/sample/address/domain/AddressLine.java +++ b/billing-service/src/main/java/de/openknowledge/sample/address/domain/AddressLine.java @@ -22,6 +22,8 @@ import de.openknowledge.sample.address.domain.AddressLine.Adapter; +import static org.apache.commons.lang3.Validate.notNull; + @JsonbTypeAdapter(Adapter.class) public class AddressLine { @@ -66,7 +68,6 @@ public String toString() { return line; } - @Override public int hashCode() { return line.hashCode(); diff --git a/billing-service/src/main/meecrowave/conf/meecrowave.properties b/billing-service/src/main/meecrowave/conf/meecrowave.properties new file mode 100644 index 0000000..fc05e42 --- /dev/null +++ b/billing-service/src/main/meecrowave/conf/meecrowave.properties @@ -0,0 +1,5 @@ +# This file contains the meecrowave default configuration +# More on http://openwebbeans.apache.org/meecrowave/meecrowave-core/cli.html + +tomcat-access-log-pattern = %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" +http=8084 diff --git a/billing-service/src/test/java/de/openknowledge/sample/address/BillingAddressServiceTest.java b/billing-service/src/test/java/de/openknowledge/sample/address/BillingAddressServiceTest.java index 72d4bd5..d89a2da 100644 --- a/billing-service/src/test/java/de/openknowledge/sample/address/BillingAddressServiceTest.java +++ b/billing-service/src/test/java/de/openknowledge/sample/address/BillingAddressServiceTest.java @@ -15,6 +15,8 @@ */ package de.openknowledge.sample.address; +import static java.util.Optional.ofNullable; + import org.apache.meecrowave.Meecrowave; import org.apache.meecrowave.junit5.MonoMeecrowaveConfig; import org.apache.meecrowave.testing.ConfigurationInject; @@ -41,13 +43,13 @@ public class BillingAddressServiceTest { @BeforeEach public void setUp(PactVerificationContext context) { - context.setTarget(new HttpTestTarget("localhost", config.getHttpPort(), "/")); + ofNullable(context).ifPresent(c -> c.setTarget(new HttpTestTarget("localhost", config.getHttpPort(), "/"))); } @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { - context.verifyInteraction(); + ofNullable(context).ifPresent(PactVerificationContext::verifyInteraction); } @State("Three customers") diff --git a/customer-service/Dockerfile b/customer-service/Dockerfile index d56b6b7..49b9309 100644 --- a/customer-service/Dockerfile +++ b/customer-service/Dockerfile @@ -15,3 +15,4 @@ RUN unzip customer-service.zip && rm customer-service.zip EXPOSE 8080 ENTRYPOINT ["/opt/customer-service-distribution/bin/meecrowave.sh", "run"] +k diff --git a/customer-service/Jenkinsfile b/customer-service/Jenkinsfile index 738887d..4164c36 100644 --- a/customer-service/Jenkinsfile +++ b/customer-service/Jenkinsfile @@ -33,9 +33,6 @@ pipeline { script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn versions:set -DnewVersion=${env.RELEASE_VERSION} -B" - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.RELEASE_VERSION}/g' deployment/overlays/prod/kustomization.yaml" - } else { - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.GIT_COMMIT}/g' deployment/overlays/test/kustomization.yaml" } } sh 'mvn clean test-compile -B' @@ -51,7 +48,27 @@ pipeline { } } steps { - sh 'mvn test pact:publish -DpactBroker.url=http://pact-service:8080 -B' + sh 'mvn test pact:publish -DpactBroker.url=http://pact:9292 -Dpact.verifier.publishResults=true -B' + } + } + stage ('Test providers') { + when { + anyOf { + not { + branch 'main' + } + environment name: 'PERFORM_RELEASE', value: 'true' + } + } + steps { + parallel( + 'delivery-service': { + build job: "delivery-service/${env.BRANCH_NAME}", parameters: [booleanParam(name: 'verifyPacts', value: true)] + }, + 'billing-service': { + build job: "billing-service/${env.BRANCH_NAME}", parameters: [booleanParam(name: 'verifyPacts', value: true)] + } + ) } } stage ('Package') { @@ -64,8 +81,7 @@ pipeline { } } steps { - sh 'mvn package -DskipTests -B' - sh 'docker build -t customer .' + sh 'mvn package meecrowave:bundle -DskipTests -B' } } stage ('Push') { @@ -78,14 +94,8 @@ pipeline { } } steps { - sh """ - docker tag customer localhost:30010/customer:${env.VERSION} - docker tag customer localhost:30010/customer:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker tag customer localhost:30010/customer:${env.GIT_COMMIT} - docker push localhost:30010/customer:${env.VERSION} - docker push localhost:30010/customer:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker push localhost:30010/customer:${env.GIT_COMMIT} - """ + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/customer-service-meecrowave-distribution.zip jenkins@test:~/' + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/customer-service-meecrowave-distribution.zip jenkins@prod:~/' script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh 'git config --global user.name "Jenkins"' @@ -94,7 +104,6 @@ pipeline { sh "mvn scm:tag -Dtag=${env.RELEASE_VERSION} -B" int nextRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) + 1 nextVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + nextRevision + "-SNAPSHOT" - sh "sed -i 's/${env.RELEASE_VERSION}/${nextVersion}/g' deployment/overlays/prod/kustomization.yaml" sh "mvn versions:set scm:checkin -DnewVersion=${nextVersion} -Dmessage='update version to ${nextVersion}' -B" } } @@ -113,10 +122,19 @@ pipeline { script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn scm:checkout -DscmVersion=${env.RELEASE_VERSION} -DscmVersionType=tag -B" - sh 'kubectl apply -k deployment/overlays/prod' + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@prod "~/customer-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/customer-service-distribution\ + && unzip ~/customer-service-meecrowave-distribution.zip\ + && ~/customer-service-distribution/bin/meecrowave.sh start" + """ } else { - sh 'kubectl apply -k deployment/overlays/test' - sh "sed -i 's/${env.GIT_COMMIT}/${env.SNAPSHOT_VERSION}/g' deployment/overlays/test/kustomization.yaml" + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@test "~/customer-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/customer-service-distribution\ + && unzip ~/customer-service-meecrowave-distribution.zip\ + && ~/customer-service-distribution/bin/meecrowave.sh start" + """ } } } diff --git a/customer-service/pom.xml b/customer-service/pom.xml index 777eed0..e021c44 100644 --- a/customer-service/pom.xml +++ b/customer-service/pom.xml @@ -198,6 +198,14 @@ ${pactBroker.url} + + org.apache.meecrowave + meecrowave-maven-plugin + ${meecrowave.version} + + ${project.basedir}/src/main/meecrowave/conf + + diff --git a/customer-service/src/main/meecrowave/conf/meecrowave.properties b/customer-service/src/main/meecrowave/conf/meecrowave.properties new file mode 100644 index 0000000..fc05e42 --- /dev/null +++ b/customer-service/src/main/meecrowave/conf/meecrowave.properties @@ -0,0 +1,5 @@ +# This file contains the meecrowave default configuration +# More on http://openwebbeans.apache.org/meecrowave/meecrowave-core/cli.html + +tomcat-access-log-pattern = %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" +http=8084 diff --git a/delivery-service/Dockerfile b/delivery-service/Dockerfile index 4318462..cf385e7 100644 --- a/delivery-service/Dockerfile +++ b/delivery-service/Dockerfile @@ -15,3 +15,4 @@ RUN unzip delivery-service.zip && rm delivery-service.zip EXPOSE 8080 ENTRYPOINT ["/opt/delivery-service-distribution/bin/meecrowave.sh", "run"] + diff --git a/delivery-service/Jenkinsfile b/delivery-service/Jenkinsfile index 589bf08..97adc51 100644 --- a/delivery-service/Jenkinsfile +++ b/delivery-service/Jenkinsfile @@ -2,6 +2,10 @@ pipeline { agent any + parameters { + booleanParam(name: 'verifyPacts', defaultValue: false, description: 'should this job just run to verify pacts from a consumer') + } + options { disableConcurrentBuilds() } @@ -12,6 +16,7 @@ pipeline { PERFORM_RELEASE = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') && env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ')}" RELEASE_VERSION = "${env.SNAPSHOT_VERSION.contains('-SNAPSHOT') ? env.SNAPSHOT_VERSION.substring(0, env.SNAPSHOT_VERSION.lastIndexOf('-SNAPSHOT')) : SNAPSHOT_VERSION}" VERSION = "${env.BRANCH_NAME == 'main' && !env.LAST_COMMIT_MESSAGE.startsWith('update version to ') ? env.RELEASE_VERSION : env.SNAPSHOT_VERSION}" + ROOT_DIRECTORY = "${params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT") ? 'target/checkout' : '.'}" } triggers { @@ -26,19 +31,29 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { echo "Building version ${env.VERSION}" script { - if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { + if (params.verifyPacts == true && env.BRANCH_NAME == 'main' && env.SNAPSHOT_VERSION.endsWith("-SNAPSHOT")) { + int previousRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) - 1 + if (previousRevision >= 0) { + previousVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + previousRevision + sh "mvn scm:checkout -DscmVersionType=tag -DscmVersion=${previousVersion}" + echo "Testing against version ${previousVersion}" + } + } else if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn versions:set -DnewVersion=${env.RELEASE_VERSION} -B" - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.RELEASE_VERSION}/g' deployment/overlays/prod/kustomization.yaml" - } else { - sh "sed -i 's/${env.SNAPSHOT_VERSION}/${env.GIT_COMMIT}/g' deployment/overlays/test/kustomization.yaml" } } - sh 'mvn clean test-compile -B' + sh """ + cd ${ROOT_DIRECTORY} + mvn clean test-compile -B + """ } } stage ('Test') { @@ -48,10 +63,33 @@ pipeline { branch 'main' } environment name: 'PERFORM_RELEASE', value: 'true' + expression { + params.verifyPacts == true + } } } steps { - sh "mvn test pact:publish -DpactBroker.url=http://pact-service:8080 -Dpact.verifier.publishResults=true -B" + sh """ + cd ${ROOT_DIRECTORY} + mvn test -DpactBroker.url=http://pact:9292 -Dpact.verifier.publishResults=true -B + """ + } + } + stage ('Test providers') { + when { + anyOf { + not { + branch 'main' + } + environment name: 'PERFORM_RELEASE', value: 'true' + } + expression { + params.verifyPacts == false + } + } + steps { + sh 'mvn pact:publish -DpactBroker.url=http://pact-service:8080 -Dpact.verifier.publishResults=true -B' + build job: "address-validation-service/${env.BRANCH_NAME}", parameters: [booleanParam(name: 'verifyPacts', value: true)] } } stage ('Package') { @@ -62,10 +100,12 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh 'mvn package -DskipTests -B' - sh 'docker build -t delivery .' + sh 'mvn package meecrowave:bundle -DskipTests -B' } } stage ('Push') { @@ -76,16 +116,13 @@ pipeline { } environment name: 'PERFORM_RELEASE', value: 'true' } + expression { + params.verifyPacts == false + } } steps { - sh """ - docker tag delivery localhost:30010/delivery:${env.VERSION} - docker tag delivery localhost:30010/delivery:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker tag delivery localhost:30010/delivery:${env.GIT_COMMIT} - docker push localhost:30010/delivery:${env.VERSION} - docker push localhost:30010/delivery:${env.BRANCH_NAME == 'main' ? 'stable' : 'latest'} - docker push localhost:30010/delivery:${env.GIT_COMMIT} - """ + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/delivery-service-meecrowave-distribution.zip jenkins@test:~/' + sh 'sshpass -p jenkins scp -o StrictHostKeyChecking=no -P 2222 target/delivery-service-meecrowave-distribution.zip jenkins@prod:~/' script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh 'git config --global user.name "Jenkins"' @@ -94,7 +131,6 @@ pipeline { sh "mvn scm:tag -Dtag=${env.RELEASE_VERSION} -B" int nextRevision = Integer.parseInt(env.RELEASE_VERSION.substring(env.RELEASE_VERSION.lastIndexOf(".") + 1)) + 1 nextVersion = RELEASE_VERSION.substring(0, env.RELEASE_VERSION.lastIndexOf(".")) + "." + nextRevision + "-SNAPSHOT" - sh "sed -i 's/${env.RELEASE_VERSION}/${nextVersion}/g' deployment/overlays/prod/kustomization.yaml" sh "mvn versions:set scm:checkin -DnewVersion=${nextVersion} -Dmessage='update version to ${nextVersion}' -B" } } @@ -102,6 +138,9 @@ pipeline { } stage ('Deploy') { when { + expression { + params.verifyPacts == false + } anyOf { not { branch 'main' @@ -113,10 +152,19 @@ pipeline { script { if (env.PERFORM_RELEASE.equals('true') && !env.RELEASE_VERSION.equals(env.SNAPSHOT_VERSION)) { sh "mvn scm:checkout -DscmVersion=${env.RELEASE_VERSION} -DscmVersionType=tag -B" - sh 'kubectl apply -k deployment/overlays/prod' + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@prod "~/delivery-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/delivery-service-distribution\ + && unzip ~/delivery-service-meecrowave-distribution.zip\ + && ~/delivery-service-distribution/bin/meecrowave.sh start" + """ } else { - sh 'kubectl apply -k deployment/overlays/test' - sh "sed -i 's/${env.GIT_COMMIT}/${env.SNAPSHOT_VERSION}/g' deployment/overlays/test/kustomization.yaml" + sh """ + sshpass -p jenkins ssh -o StrictHostKeyChecking=no -p 2222 jenkins@test "~/delivery-service-distribution/bin/meecrowave.sh stop\ + && rm -rf ~/delivery-service-distribution\ + && unzip ~/delivery-service-meecrowave-distribution.zip\ + && ~/delivery-service-distribution/bin/meecrowave.sh start" + """ } } } diff --git a/delivery-service/pom.xml b/delivery-service/pom.xml index 7470a7c..4848b4f 100644 --- a/delivery-service/pom.xml +++ b/delivery-service/pom.xml @@ -16,8 +16,8 @@ 1.0.0-SNAPSHOT - scm:git:http://openknowledge:workshop@gogs-service:3000/openknowledge/delivery-service.git - scm:git:http://openknowledge:workshop@gogs-service:3000/openknowledge/delivery-service.git + scm:git:http://openknowledge:workshop@gogs:3000/openknowledge/delivery-service.git + scm:git:http://openknowledge:workshop@gogs:3000/openknowledge/delivery-service.git @@ -242,6 +242,14 @@ ${pactBroker.url} + + org.apache.meecrowave + meecrowave-maven-plugin + ${meecrowave.version} + + ${project.basedir}/src/main/meecrowave/conf + + diff --git a/delivery-service/src/main/meecrowave/conf/meecrowave.properties b/delivery-service/src/main/meecrowave/conf/meecrowave.properties new file mode 100644 index 0000000..fc05e42 --- /dev/null +++ b/delivery-service/src/main/meecrowave/conf/meecrowave.properties @@ -0,0 +1,5 @@ +# This file contains the meecrowave default configuration +# More on http://openwebbeans.apache.org/meecrowave/meecrowave-core/cli.html + +tomcat-access-log-pattern = %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" +http=8084 diff --git a/delivery-service/src/test/java/de/openknowledge/sample/address/DeliveryAddressServiceTest.java b/delivery-service/src/test/java/de/openknowledge/sample/address/DeliveryAddressServiceTest.java index 185cf27..fafa383 100644 --- a/delivery-service/src/test/java/de/openknowledge/sample/address/DeliveryAddressServiceTest.java +++ b/delivery-service/src/test/java/de/openknowledge/sample/address/DeliveryAddressServiceTest.java @@ -17,6 +17,7 @@ import static de.openknowledge.sample.infrastructure.DatabaseCleanup.with; import static de.openknowledge.sample.infrastructure.ScriptExecutor.executeWith; +import static java.util.Optional.ofNullable; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doThrow; @@ -66,13 +67,13 @@ public class DeliveryAddressServiceTest { public void setUp(PactVerificationContext context) { doThrow(new ValidationException("City not found")) .when(addressValidationService).validate(argThat(a -> a.getCity().toString().contains("London"))); - context.setTarget(new HttpTestTarget("localhost", config.getHttpPort(), "/")); + ofNullable(context).ifPresent(c -> c.setTarget(new HttpTestTarget("localhost", config.getHttpPort(), "/"))); } @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { - context.verifyInteraction(); + ofNullable(context).ifPresent(PactVerificationContext::verifyInteraction); } @State("Three customers")