From 577af1adf9cc3d0f53d6c8a89233445f3eaed2fc Mon Sep 17 00:00:00 2001 From: Divya Madala <113469545+Divyaasm@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:58:38 -0700 Subject: [PATCH] Add Jenkins job for Docker Patch jenkins shared library (#3937) Signed-off-by: Divya Madala --- jenkins/docker/docker-re-release.jenkinsfile | 54 +++++++++++ tests/jenkins/TestDockerReRelease.groovy | 91 +++++++++++++++++++ .../docker/docker-re-release.jenkinsfile.txt | 49 ++++++++++ 3 files changed, 194 insertions(+) create mode 100644 jenkins/docker/docker-re-release.jenkinsfile create mode 100644 tests/jenkins/TestDockerReRelease.groovy create mode 100644 tests/jenkins/jenkinsjob-regression-files/docker/docker-re-release.jenkinsfile.txt diff --git a/jenkins/docker/docker-re-release.jenkinsfile b/jenkins/docker/docker-re-release.jenkinsfile new file mode 100644 index 0000000000..8264fb9abf --- /dev/null +++ b/jenkins/docker/docker-re-release.jenkinsfile @@ -0,0 +1,54 @@ +lib = library(identifier: 'jenkins@5.7.1', retriever: modernSCM([ + $class: 'GitSCMSource', + remote: 'https://github.com/opensearch-project/opensearch-build-libraries.git', +])) + +pipeline { + options { + timeout(time: 2, unit: 'HOURS') + } + agent none + parameters { + choice( + name: 'PRODUCT', + choices: ['opensearch', 'opensearch-dashboards'], + description: "Choose the product type among OpenSearch / OpenSearch-Dashboards." + ) + choice( + name: 'TAG', + choices: ['2', '1'], + description: "Choose the tag of the Product.", + ) + } + stages { + stage("Docker re-release") { + agent { + docker { + label 'Jenkins-Agent-Ubuntu2004-X64-M52xlarge-Docker-Builder' + image 'opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.9.1-qemu5.0-awscli1.22-jdk11-v1' + args '-u root -v /var/run/docker.sock:/var/run/docker.sock' + registryUrl 'https://public.ecr.aws/' + alwaysPull true + } + } + stages { + stage('Patch Docker Image') { + steps { + script { + patchDockerImage( + product: "${PRODUCT}", + tag: "${TAG}", + re_release: true + ) + } + } + post { + always { + postCleanup() + } + } + } + } + } + } +} diff --git a/tests/jenkins/TestDockerReRelease.groovy b/tests/jenkins/TestDockerReRelease.groovy new file mode 100644 index 0000000000..97771452ed --- /dev/null +++ b/tests/jenkins/TestDockerReRelease.groovy @@ -0,0 +1,91 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + + +import jenkins.tests.BuildPipelineTest +import org.junit.Before +import org.junit.Test +import org.yaml.snakeyaml.Yaml +import static org.hamcrest.CoreMatchers.hasItem +import static org.hamcrest.MatcherAssert.assertThat +import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library +import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library +import static com.lesfurets.jenkins.unit.global.lib.GitSource.gitSource +import static com.lesfurets.jenkins.unit.global.lib.GitSource.gitSource +import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString + + +class TestDockerReRelease extends BuildPipelineTest { + + @Override + @Before + void setUp() { + + helper.registerSharedLibrary( + library().name('jenkins') + .defaultVersion('5.7.1') + .allowOverride(true) + .implicit(true) + .targetPath('vars') + .retriever(gitSource('https://github.com/opensearch-project/opensearch-build-libraries.git')) + .build() + ) + super.setUp() + + // Variables + addParam('PRODUCT', 'opensearch') + addParam('TAG', '1') + + def inputManifest = "tests/jenkins/data/opensearch-1.3.0.yml" + helper.registerAllowedMethod('readYaml', [Map.class], { args -> + return new Yaml().load((inputManifest as File).text) + }) + helper.addShMock("""docker inspect --format '{{ index .Config.Labels "org.label-schema.version"}}' opensearchproject/opensearch:1""") { script -> + return [stdout: "1.3.0", exitValue: 0] + } + helper.addShMock("""docker inspect --format '{{ index .Config.Labels "org.label-schema.description"}}' opensearchproject/opensearch:1""") { script -> + return [stdout: "7756", exitValue: 0] + } + helper.addShMock("""docker inspect --format '{{ index .Config.Labels "org.label-schema.build-date"}}' opensearchproject/opensearch:1""") { script -> + return [stdout: "2023-06-19T19:12:59Z", exitValue: 0] + } + helper.addShMock("""docker inspect --format '{{ index .Config.Labels "org.label-schema.version"}}' opensearchproject/opensearch:latest""") { script -> + return [stdout: "2.5.0", exitValue: 0] + } + + } + + @Test + void testReRelease() { + super.testPipeline('jenkins/docker/docker-re-release.jenkinsfile', + 'tests/jenkins/jenkinsjob-regression-files/docker/docker-re-release.jenkinsfile') + } + + @Test + void checkForTriggeredJobs(){ + runScript('jenkins/docker/docker-re-release.jenkinsfile') + assertThat(getCommandExecutions('build', ''), hasItem('{job=docker-build, propagate=true, wait=true, parameters=[null, null, null]}')) + assertThat(getCommandExecutions('build', ''), hasItem('{job=docker-scan, propagate=true, wait=true, parameters=[null]}')) + assertThat(getCommandExecutions('build', ''), hasItem('{job=docker-promotion, propagate=true, wait=true, parameters=[null, null, null]}')) + + } + + def getCommandExecutions(methodName, command) { + def shCommands = helper.callStack.findAll { + call -> + call.methodName == methodName + }. + collect { + call -> + callArgsToString(call) + }.findAll { + shCommand -> + shCommand.contains(command) + } + + return shCommands + } + +} diff --git a/tests/jenkins/jenkinsjob-regression-files/docker/docker-re-release.jenkinsfile.txt b/tests/jenkins/jenkinsjob-regression-files/docker/docker-re-release.jenkinsfile.txt new file mode 100644 index 0000000000..1801789ecb --- /dev/null +++ b/tests/jenkins/jenkinsjob-regression-files/docker/docker-re-release.jenkinsfile.txt @@ -0,0 +1,49 @@ + docker-re-release.run() + docker-re-release.modernSCM({$class=GitSCMSource, remote=https://github.com/opensearch-project/opensearch-build-libraries.git}) + docker-re-release.library({identifier=jenkins@5.7.1, retriever=null}) + docker-re-release.pipeline(groovy.lang.Closure) + docker-re-release.timeout({time=2, unit=HOURS}) + docker-re-release.echo(Executing on agent [label:none]) + docker-re-release.stage(Patch Docker Image, groovy.lang.Closure) + docker-re-release.script(groovy.lang.Closure) + docker-re-release.patchDockerImage({product=opensearch, tag=1, re_release=true}) + patchDockerImage.legacySCM(groovy.lang.Closure) + patchDockerImage.library({identifier=jenkins@5.7.1, retriever=null}) + patchDockerImage.sh(#!/bin/bash + set -e + set +x + docker pull opensearchproject/opensearch:1 + docker pull opensearchproject/opensearch:latest + ) + patchDockerImage.sh({script=docker inspect --format '{{ index .Config.Labels "org.label-schema.version"}}' opensearchproject/opensearch:1, returnStdout=true}) + patchDockerImage.sh({script=docker inspect --format '{{ index .Config.Labels "org.label-schema.build-date"}}' opensearchproject/opensearch:1, returnStdout=true}) + patchDockerImage.sh({script=docker inspect --format '{{ index .Config.Labels "org.label-schema.description"}}' opensearchproject/opensearch:1, returnStdout=true}) + patchDockerImage.sh({script=docker inspect --format '{{ index .Config.Labels "org.label-schema.version"}}' opensearchproject/opensearch:latest, returnStdout=true}) + patchDockerImage.readYaml({file=manifests/1.3.0/opensearch-1.3.0.yml}) + InputManifest.asBoolean() + patchDockerImage.buildDockerImage({inputManifest=manifests/1.3.0/opensearch-1.3.0.yml, buildNumber=7756, buildDate=20230619, buildOption=re_release_docker_image, artifactUrlX64=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/7756/linux/x64/tar/dist/opensearch/opensearch-1.3.0-linux-x64.tar.gz, artifactUrlArm64=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/7756/linux/arm64/tar/dist/opensearch/opensearch-1.3.0-linux-arm64.tar.gz}) + buildDockerImage.legacySCM(groovy.lang.Closure) + buildDockerImage.library({identifier=jenkins@5.7.1, retriever=null}) + buildDockerImage.readYaml({file=manifests/1.3.0/opensearch-1.3.0.yml}) + InputManifest.asBoolean() + buildDockerImage.echo(Triggering docker-build) + buildDockerImage.string({name=DOCKER_BUILD_GIT_REPOSITORY, value=https://github.com/opensearch-project/opensearch-build}) + buildDockerImage.string({name=DOCKER_BUILD_GIT_REPOSITORY_REFERENCE, value=main}) + buildDockerImage.string({name=DOCKER_BUILD_SCRIPT_WITH_COMMANDS, value=id && pwd && cd docker/release && curl -sSL https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/7756/linux/x64/tar/dist/opensearch/opensearch-1.3.0-linux-x64.tar.gz -o opensearch-x64.tgz && curl -sSL https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/7756/linux/arm64/tar/dist/opensearch/opensearch-1.3.0-linux-arm64.tar.gz -o opensearch-arm64.tgz && bash build-image-multi-arch.sh -v 1.3.0 -f ./dockerfiles/opensearch.al2.dockerfile -p opensearch -a 'x64,arm64' -r opensearchstaging/opensearch -t 'opensearch-x64.tgz,opensearch-arm64.tgz' -n 7756}) + buildDockerImage.build({job=docker-build, propagate=true, wait=true, parameters=[null, null, null]}) + buildDockerImage.echo(Triggering docker create tag with build number) + buildDockerImage.string({name=SOURCE_IMAGE_REGISTRY, value=opensearchstaging}) + buildDockerImage.string({name=SOURCE_IMAGE, value=opensearch:1.3.0}) + buildDockerImage.string({name=DESTINATION_IMAGE_REGISTRY, value=opensearchstaging}) + buildDockerImage.string({name=DESTINATION_IMAGE, value=opensearch:1.3.0.7756.20230619}) + buildDockerImage.build({job=docker-copy, propagate=true, wait=true, parameters=[null, null, null, null]}) + buildDockerImage.echo(Triggering docker-scan for opensearch version 1.3.0) + buildDockerImage.string({name=IMAGE_FULL_NAME, value=opensearchstaging/opensearch:1.3.0}) + buildDockerImage.build({job=docker-scan, propagate=true, wait=true, parameters=[null]}) + patchDockerImage.echo(Triggering docker-promotion) + patchDockerImage.string({name=SOURCE_IMAGES, value=opensearch:1.3.0.7756.20230619}) + patchDockerImage.string({name=RELEASE_VERSION, value=1.3.0}) + patchDockerImage.booleanParam({name=TAG_LATEST, value=false}) + patchDockerImage.build({job=docker-promotion, propagate=true, wait=true, parameters=[null, null, null]}) + docker-re-release.postCleanup() + postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})