Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a docker promotion jenkins workflow #2288

Merged
merged 7 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions jenkins/promotion/promote-docker-ecr.jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
lib = library(identifier: 'jenkins@20211123', retriever: legacySCM(scm))

pipeline {
options {
timeout(time: 1, unit: 'HOURS')
}
agent {
docker {
label 'Jenkins-Agent-al2-x64-c54xlarge-Docker-Host'
image 'opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk11-v2'
args '-u root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
parameters {
string(
name: 'PROMOTE_PRODUCT',
description: 'Comma separated list of product with its image tag that we want to promote from staging to production. E.g.: opensearch:2.0.1.3910, opensearch-dashboards:2.0.1, data-prepper1.5.0-19.',
trim: true
)
string(
name: 'RELEASE_VERSION',
description: 'Official release version on production repository. E.g.: 2.0.1, 1.5.0.',
trim: true
)
booleanParam(
name: 'DOCKER_HUB_PROMOTE',
defaultValue: true,
description: 'Promote the staging images to Docker Hub with the tag choices.'
)
booleanParam(
name: 'ECR_PROMOTE',
defaultValue: true,
description: 'Promote the staging images to ECR with the tag choices.'
)
booleanParam(
name: 'TAG_LATEST',
defaultValue: true,
description: 'Tag the copied image as latest'
)
booleanParam(
name: 'TAG_MAJOR_VERSION',
defaultValue: true,
description: 'Tag the copied image to be on the latest image for any build within its major version. E.g.: 1.3.2 image will be tagged with 1 in the hub.'
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give proper naming to these params, they are confusing at best for anyone that is not familiar with the release processes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the wording for these params.


stages {
stage('Parameters Check') {
steps {
script {
currentBuild.description = "Promoting ${PROMOTE_PRODUCT} to production hub."
if(PROMOTE_PRODUCT.isEmpty()) {
currentBuild.result = 'ABORTED'
error('Make sure at lease one product is added to be promoted.')
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also make sure any string parameters requires non-empty inputs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add additional checks for RELEASE_VERSION param.

stage('image-promote-to-prod') {
steps {
script {
for (product in PROMOTE_PRODUCT.split(',')) {
def productRepo = product.trim()
println("Promoting \"$productRepo\" from staging to production.")
promoteContainer(
imageRepository: productRepo,
version: RELEASE_VERSION,
dockerPromote: DOCKER_HUB_PROMOTE,
ecrPromote: ECR_PROMOTE,
latestTag: TAG_LATEST,
majorVersionTag: TAG_MAJOR_VERSION
)
}
}
}
}
}
post() {
always {
script {
postCleanup()
sh "docker logout"
}
}
}
}
2 changes: 1 addition & 1 deletion tests/jenkins/TestCopyContainer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestCopyContainer extends BuildPipelineTest {
binding.setVariable('ARTIFACT_PROMOTION_ROLE_NAME', 'sample-agent-AssumeRole')
binding.setVariable('AWS_ACCOUNT_ARTIFACT', '1234567890')
binding.setVariable('DATA_PREPPER_STAGING_CONTAINER_REPOSITORY', 'sample_dataprepper_ecr_url')
helper.registerAllowedMethod('withAWS', [Map, Closure], null)
helper.registerAllowedMethod('withAWS', [Map, Closure], null)
super.setUp()

}
Expand Down
116 changes: 116 additions & 0 deletions tests/jenkins/TestPromoteContainer.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test

class TestPromoteContainer extends BuildPipelineTest {

String PROMOTE_PRODUCT = 'opensearch:2.0.1.2901, opensearch-dashboards:2.0.1-2345, data-prepper:2.0.1.123'
String RELEASE_VERSION = '2.0.1'

@Before
void setUp() {
binding.setVariable('PROMOTE_PRODUCT', PROMOTE_PRODUCT)
binding.setVariable('RELEASE_VERSION', RELEASE_VERSION)
binding.setVariable('DOCKER_USERNAME', 'dummy_docker_username')
binding.setVariable('DOCKER_PASSWORD', 'dummy_docker_password')
binding.setVariable('ARTIFACT_PROMOTION_ROLE_NAME', 'dummy-agent-AssumeRole')
binding.setVariable('AWS_ACCOUNT_ARTIFACT', '1234567890')
binding.setVariable('DATA_PREPPER_STAGING_CONTAINER_REPOSITORY', 'dummy_dataprepper_ecr_url')


helper.registerAllowedMethod('withAWS', [Map, Closure], null)
super.setUp()

}

@Test
public void testPromoteContainerToDocker() {
String dockerPromote = true
String ecrPromote = false
String latestBoolean = false
String majorVersionBoolean = false
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToDocker.jenkinsfile")
}

@Test
public void testPromoteContainerToDockerLatest() {
String dockerPromote = true
String ecrPromote = false
String latestBoolean = true
String majorVersionBoolean = false
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToDockerLatest.jenkinsfile")
}

@Test
public void testPromoteContainerToDockerMajor() {
String dockerPromote = true
String ecrPromote = false
String latestBoolean = false
String majorVersionBoolean = true
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToDockerMajor.jenkinsfile")
}

@Test
public void testPromoteContainerToDockerLatestMajor() {
String dockerPromote = true
String ecrPromote = false
String latestBoolean = true
String majorVersionBoolean = true
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToDockerLatestMajor.jenkinsfile")
}

@Test
public void testPromoteContainerToECRLatestMajor() {
String dockerPromote = false
String ecrPromote = true
String latestBoolean = true
String majorVersionBoolean = true
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToECRLatestMajor.jenkinsfile")
}

@Test
public void testPromoteContainerToDockerECRLatestMajor() {
String dockerPromote = true
String ecrPromote = true
String latestBoolean = true
String majorVersionBoolean = true
binding.setVariable('DOCKER_HUB_PROMOTE', dockerPromote)
binding.setVariable('ECR_PROMOTE', ecrPromote)
binding.setVariable('TAG_LATEST', latestBoolean)
binding.setVariable('TAG_MAJOR_VERSION', majorVersionBoolean)

super.testPipeline("jenkins/promotion/promote-docker-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/promotion/promote-container/promote-container-testPromoteContainerToDockerECRLatestMajor.jenkinsfile")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
promote-docker-ecr.run()
promote-docker-ecr.legacySCM(groovy.lang.Closure)
promote-docker-ecr.library({identifier=jenkins@20211123, retriever=null})
promote-docker-ecr.pipeline(groovy.lang.Closure)
promote-docker-ecr.timeout({time=1, unit=HOURS})
promote-docker-ecr.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk11-v2, reuseNode:false, stages:[:], args:-u root -v /var/run/docker.sock:/var/run/docker.sock, alwaysPull:false, containerPerStageRoot:false, label:Jenkins-Agent-al2-x64-c54xlarge-Docker-Host]])
promote-docker-ecr.stage(Parameters Check, groovy.lang.Closure)
promote-docker-ecr.script(groovy.lang.Closure)
promote-docker-ecr.stage(image-promote-to-prod, groovy.lang.Closure)
promote-docker-ecr.script(groovy.lang.Closure)
promote-docker-ecr.promoteContainer({imageRepository=opensearch:2.0.1.2901, version=2.0.1, dockerPromote=true, ecrPromote=false, latestTag=false, majorVersionTag=false})
promoteContainer.copyContainer({sourceImage=opensearch:2.0.1.2901, sourceRegistry=opensearchstaging, destinationImage=opensearch:2.0.1, destinationRegistry=opensearchproject})
copyContainer.usernamePassword({credentialsId=jenkins-production-dockerhub-credential, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyContainer.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyContainer.sh({returnStdout=true, script=echo DOCKER_PASSWORD | docker login --username DOCKER_USERNAME --password-stdin})
copyContainer.sh(
gcrane cp opensearchstaging/opensearch:2.0.1.2901 opensearchproject/opensearch:2.0.1
docker logout
)
promote-docker-ecr.promoteContainer({imageRepository=opensearch-dashboards:2.0.1-2345, version=2.0.1, dockerPromote=true, ecrPromote=false, latestTag=false, majorVersionTag=false})
promoteContainer.copyContainer({sourceImage=opensearch-dashboards:2.0.1-2345, sourceRegistry=opensearchstaging, destinationImage=opensearch-dashboards:2.0.1, destinationRegistry=opensearchproject})
copyContainer.usernamePassword({credentialsId=jenkins-production-dockerhub-credential, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyContainer.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyContainer.sh({returnStdout=true, script=echo DOCKER_PASSWORD | docker login --username DOCKER_USERNAME --password-stdin})
copyContainer.sh(
gcrane cp opensearchstaging/opensearch-dashboards:2.0.1-2345 opensearchproject/opensearch-dashboards:2.0.1
docker logout
)
promote-docker-ecr.promoteContainer({imageRepository=data-prepper:2.0.1.123, version=2.0.1, dockerPromote=true, ecrPromote=false, latestTag=false, majorVersionTag=false})
promoteContainer.copyContainer({sourceImage=data-prepper:2.0.1.123, sourceRegistry=dummy_dataprepper_ecr_url, destinationImage=data-prepper:2.0.1, destinationRegistry=opensearchproject})
copyContainer.usernamePassword({credentialsId=jenkins-production-dockerhub-credential, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyContainer.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyContainer.sh({returnStdout=true, script=echo DOCKER_PASSWORD | docker login --username DOCKER_USERNAME --password-stdin})
copyContainer.sh(
gcrane cp dummy_dataprepper_ecr_url/data-prepper:2.0.1.123 opensearchproject/data-prepper:2.0.1
docker logout
)
promote-docker-ecr.script(groovy.lang.Closure)
promote-docker-ecr.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
promote-docker-ecr.sh(docker logout)
Loading