Skip to content

Commit

Permalink
Update new docker jobs to make ECR optional (opensearch-project#1859)
Browse files Browse the repository at this point in the history
* update docker jobs with boolean ECR option

Signed-off-by: Abhinav Gupta <[email protected]>

* change expression

Signed-off-by: Abhinav Gupta <[email protected]>

* update when condition

Signed-off-by: Abhinav Gupta <[email protected]>

* update when condition

Signed-off-by: Abhinav Gupta <[email protected]>

* update description

Signed-off-by: Abhinav Gupta <[email protected]>

* updated job to have dropdown for ecr and dockerhub

Signed-off-by: Abhinav Gupta <[email protected]>

* update default

Signed-off-by: Abhinav Gupta <[email protected]>

* update default

Signed-off-by: Abhinav Gupta <[email protected]>

* updated the job timeout to be 1 hour

Signed-off-by: Abhinav Gupta <[email protected]>

* update regression files

Signed-off-by: Abhinav Gupta <[email protected]>
  • Loading branch information
abhinavGupta16 authored Apr 5, 2022
1 parent ff4a457 commit 39446d4
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 12 deletions.
13 changes: 12 additions & 1 deletion jenkins/docker-ecr/docker-build-with-ecr.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib = library(identifier: 'jenkins@20211123', retriever: legacySCM(scm))

pipeline {
options {
timeout(time: 3, unit: 'HOURS')
timeout(time: 1, unit: 'HOURS')
}
agent {
docker {
Expand Down Expand Up @@ -39,6 +39,11 @@ pipeline {
description: 'Image tag on staging. Eg: 1.3.0',
trim: true
)
choice(
choices: ['docker-hub + ECR', 'docker-hub', 'ECR'],
name: 'PLATFORM',
description: 'Where do you want to push the images?'
)
}
stages {
stage('check-params') {
Expand All @@ -53,6 +58,9 @@ pipeline {
}
}
stage('docker-build-dockerhub-staging') {
when{
expression { PLATFORM == 'docker-hub + ECR' || PLATFORM == 'docker-hub' }
}
steps {
script {
git url: DOCKER_BUILD_GIT_REPOSITORY, branch: DOCKER_BUILD_GIT_REPOSITORY_REFERENCE
Expand All @@ -67,6 +75,9 @@ pipeline {
}
}
stage('docker-ecr-staging') {
when {
expression { PLATFORM == 'docker-hub + ECR' || PLATFORM == 'ECR' }
}
steps {
script {
copyContainer(
Expand Down
13 changes: 12 additions & 1 deletion jenkins/docker-ecr/docker-ecr-promote.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib = library(identifier: 'jenkins@20211123', retriever: legacySCM(scm))

pipeline {
options {
timeout(time: 3, unit: 'HOURS')
timeout(time: 1, unit: 'HOURS')
}
agent {
docker {
Expand All @@ -22,10 +22,18 @@ pipeline {
description: 'Image tag on staging. Eg: 1.3.0',
trim: true
)
choice(
choices: ['docker-hub + ECR', 'docker-hub', 'ECR'],
name: 'PLATFORM',
description: 'Where do you want to push the images?'
)
}

stages {
stage('dockerhub-promote-to-prod') {
when{
expression { PLATFORM == 'docker-hub + ECR' || PLATFORM == 'docker-hub' }
}
steps {
script {
copyContainer(
Expand All @@ -38,6 +46,9 @@ pipeline {
}
}
stage('ecr-promote-to-prod') {
when {
expression { PLATFORM == 'docker-hub + ECR' || PLATFORM == 'ECR' }
}
steps {
script {
copyContainer(
Expand Down
35 changes: 35 additions & 0 deletions tests/jenkins/TestDockerBuildWithDockerhubOnlyJob.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test

class TestDockerBuildWithDockerhubOnlyJob extends BuildPipelineTest {

@Before
void setUp() {

String imageRepository = 'ci-runner-staging'
String imageTag = 'latest'
String accountName = 'aws_account_public'

super.setUp()
binding.setVariable('IMAGE_REPOSITORY', imageRepository)
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('DOCKER_BUILD_SCRIPT_WITH_COMMANDS', 'dummy_command')
binding.setVariable('AWS_ACCOUNT_PUBLIC', accountName)
binding.setVariable('PLATFORM', 'docker-hub')

}

@Test
public void testDockerWithDockerhubOnlyJobStaging(){

binding.setVariable('DOCKER_USERNAME ', 'docker_username')
binding.setVariable('DOCKER_PASSWORD', 'docker_password')

helper.registerAllowedMethod("git", [Map])

super.testPipeline("jenkins/docker-ecr/docker-build-with-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-build-with-dockerhub-only.jenkinsfile")
}

}
20 changes: 16 additions & 4 deletions tests/jenkins/TestDockerBuildWithEcrJob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,31 @@ class TestDockerBuildWithEcrJob extends BuildPipelineTest {
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('DOCKER_BUILD_SCRIPT_WITH_COMMANDS', 'dummy_command')
binding.setVariable('AWS_ACCOUNT_PUBLIC', accountName)

}

@Test
public void testDockerForEcrJobStaging(){
public void testDockerForEcrOnlyJobStaging() {

binding.setVariable('PLATFORM', 'ECR')

helper.registerAllowedMethod("git", [Map])

super.testPipeline("jenkins/docker-ecr/docker-build-with-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-build-with-only-ecr.jenkinsfile")
}

@Test
public void testDockerForEcrAndDockerhubJobStaging() {

binding.setVariable('PLATFORM', 'docker-hub + ECR')
binding.setVariable('DOCKER_USERNAME ', 'docker_username')
binding.setVariable('DOCKER_PASSWORD', 'docker_password')

helper.registerAllowedMethod("git", [Map])

super.testPipeline("jenkins/docker-ecr/docker-build-with-ecr.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-build-with-ecr.jenkinsfile")

}
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-build-with-ecr-dockerhub.jenkinsfile")
}

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

class TestDockerPromoteWithDockerhubOnlyJob extends BuildPipelineTest {

@Before
void setUp() {
String imageRepository = 'ci-runner-staging'
String imageTag = 'latest'
String accountName = 'aws_account_artifact'
String sourceImagePath = "opensearchstaging/${imageRepository}:${imageTag}"

this.registerLibTester(new CopyContainerLibTester(sourceImagePath,
"opensearchproject/${imageRepository}:${imageTag}",
'docker',
'jenkins-staging-docker-prod-token'))

super.setUp()

binding.setVariable('IMAGE_REPOSITORY', imageRepository)
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'docker-hub')

}

@Test
public void testDockerForEcrJobProductionWithDockerhubOnly(){
super.testPipeline("jenkins/docker-ecr/docker-ecr-promote.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-ecr-promote-with-dockerhub-only.jenkinsfile")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test

class TestDockerPromoteJob extends BuildPipelineTest {
class TestDockerPromoteWithEcrAndDockerhubJob extends BuildPipelineTest {

@Before
void setUp() {
Expand All @@ -21,19 +21,20 @@ class TestDockerPromoteJob extends BuildPipelineTest {
"opensearchproject/${imageRepository}:${imageTag}",
'docker',
'jenkins-staging-docker-prod-token'))

super.setUp()

binding.setVariable('IMAGE_REPOSITORY', imageRepository)
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'docker-hub + ECR')

}

@Test
public void testDockerForEcrJobProduction(){
public void testDockerForEcrAndDockerhubJobProductionWithECR(){
super.testPipeline("jenkins/docker-ecr/docker-ecr-promote.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-ecr-promote.jenkinsfile")
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-ecr-promote-with-ECR-dockerhub.jenkinsfile")
}

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

class TestDockerPromoteWithEcrOnlyJob extends BuildPipelineTest {

@Before
void setUp() {
String imageRepository = 'ci-runner-staging'
String imageTag = 'latest'
String accountName = 'aws_account_artifact'
String sourceImagePath = "opensearchstaging/${imageRepository}:${imageTag}"

this.registerLibTester(new CopyContainerLibTester(sourceImagePath,
"public.ecr.aws/p5f6l6i3/${imageRepository}:${imageTag}",
'ecr',
'public.ecr.aws/p5f6l6i3',
accountName))

super.setUp()

binding.setVariable('IMAGE_REPOSITORY', imageRepository)
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'ECR')

}

@Test
public void testDockerForEcrAndDockerhubJobProductionWithECR(){
super.testPipeline("jenkins/docker-ecr/docker-ecr-promote.jenkinsfile",
"tests/jenkins/jenkinsjob-regression-files/docker-ecr/docker-ecr-promote-with-ECR-only.jenkinsfile")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
docker-build-with-ecr.run()
docker-build-with-ecr.legacySCM(groovy.lang.Closure)
docker-build-with-ecr.library({identifier=jenkins@20211123, retriever=null})
docker-build-with-ecr.pipeline(groovy.lang.Closure)
docker-build-with-ecr.timeout({time=1, unit=HOURS})
docker-build-with-ecr.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, 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]])
docker-build-with-ecr.stage(check-params, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.stage(docker-build-dockerhub-staging, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.git({url=https://github.com/opensearch-project/opensearch-build, branch=main})
docker-build-with-ecr.usernamePassword({credentialsId=jenkins-staging-docker-staging-credential, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
docker-build-with-ecr.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
docker-build-with-ecr.sh(
set -e
docker logout && docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD && eval $DOCKER_BUILD_SCRIPT_WITH_COMMANDS
)
docker-build-with-ecr.echo(Skipping stage docker-ecr-staging)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
docker-build-with-ecr.sh(docker logout && docker image prune -f --all)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
docker-build-with-ecr.legacySCM(groovy.lang.Closure)
docker-build-with-ecr.library({identifier=jenkins@20211123, retriever=null})
docker-build-with-ecr.pipeline(groovy.lang.Closure)
docker-build-with-ecr.timeout({time=3, unit=HOURS})
docker-build-with-ecr.timeout({time=1, unit=HOURS})
docker-build-with-ecr.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, 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]])
docker-build-with-ecr.stage(check-params, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
docker-build-with-ecr.run()
docker-build-with-ecr.legacySCM(groovy.lang.Closure)
docker-build-with-ecr.library({identifier=jenkins@20211123, retriever=null})
docker-build-with-ecr.pipeline(groovy.lang.Closure)
docker-build-with-ecr.timeout({time=1, unit=HOURS})
docker-build-with-ecr.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, 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]])
docker-build-with-ecr.stage(check-params, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.echo(Skipping stage docker-build-dockerhub-staging)
docker-build-with-ecr.stage(docker-ecr-staging, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.copyContainer({sourceImagePath=opensearchstaging/ci-runner-staging:latest, destinationImagePath=public.ecr.aws/m0o1u6w1/ci-runner-staging:latest, destinationType=ecr, destinationCredentialIdentifier=public.ecr.aws/m0o1u6w1, accountName=aws_account_public})
copyContainer.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyContainer.sh(docker logout)
copyContainer.withAWS({role=Upload_ECR_Image, roleAccount=aws_account_public, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure)
copyContainer.sh(
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/m0o1u6w1
gcrane cp opensearchstaging/ci-runner-staging:latest public.ecr.aws/m0o1u6w1/ci-runner-staging:latest
)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
docker-build-with-ecr.sh(docker logout && docker image prune -f --all)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
docker-ecr-promote.legacySCM(groovy.lang.Closure)
docker-ecr-promote.library({identifier=jenkins@20211123, retriever=null})
docker-ecr-promote.pipeline(groovy.lang.Closure)
docker-ecr-promote.timeout({time=3, unit=HOURS})
docker-ecr-promote.timeout({time=1, unit=HOURS})
docker-ecr-promote.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, reuseNode:false, stages:[:], args:-u root, alwaysPull:false, containerPerStageRoot:false, label:Jenkins-Agent-al2-x64-c54xlarge-Docker-Host]])
docker-ecr-promote.stage(dockerhub-promote-to-prod, groovy.lang.Closure)
docker-ecr-promote.script(groovy.lang.Closure)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
docker-ecr-promote.run()
docker-ecr-promote.legacySCM(groovy.lang.Closure)
docker-ecr-promote.library({identifier=jenkins@20211123, retriever=null})
docker-ecr-promote.pipeline(groovy.lang.Closure)
docker-ecr-promote.timeout({time=1, unit=HOURS})
docker-ecr-promote.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, reuseNode:false, stages:[:], args:-u root, alwaysPull:false, containerPerStageRoot:false, label:Jenkins-Agent-al2-x64-c54xlarge-Docker-Host]])
docker-ecr-promote.echo(Skipping stage dockerhub-promote-to-prod)
docker-ecr-promote.stage(ecr-promote-to-prod, groovy.lang.Closure)
docker-ecr-promote.script(groovy.lang.Closure)
docker-ecr-promote.copyContainer({sourceImagePath=opensearchstaging/ci-runner-staging:latest, destinationImagePath=public.ecr.aws/p5f6l6i3/ci-runner-staging:latest, destinationType=ecr, destinationCredentialIdentifier=public.ecr.aws/p5f6l6i3, accountName=aws_account_artifact})
copyContainer.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyContainer.sh(docker logout)
copyContainer.withAWS({role=Upload_ECR_Image, roleAccount=aws_account_artifact, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure)
copyContainer.sh(
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/p5f6l6i3
gcrane cp opensearchstaging/ci-runner-staging:latest public.ecr.aws/p5f6l6i3/ci-runner-staging:latest
)
docker-ecr-promote.script(groovy.lang.Closure)
docker-ecr-promote.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
docker-ecr-promote.sh(docker logout)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
docker-ecr-promote.run()
docker-ecr-promote.legacySCM(groovy.lang.Closure)
docker-ecr-promote.library({identifier=jenkins@20211123, retriever=null})
docker-ecr-promote.pipeline(groovy.lang.Closure)
docker-ecr-promote.timeout({time=1, unit=HOURS})
docker-ecr-promote.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0-awscli1.22-jdk14, reuseNode:false, stages:[:], args:-u root, alwaysPull:false, containerPerStageRoot:false, label:Jenkins-Agent-al2-x64-c54xlarge-Docker-Host]])
docker-ecr-promote.stage(dockerhub-promote-to-prod, groovy.lang.Closure)
docker-ecr-promote.script(groovy.lang.Closure)
docker-ecr-promote.copyContainer({sourceImagePath=opensearchstaging/ci-runner-staging:latest, destinationImagePath=opensearchproject/ci-runner-staging:latest, destinationType=docker, destinationCredentialIdentifier=jenkins-staging-docker-prod-token})
copyContainer.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyContainer.sh(docker logout)
copyContainer.usernamePassword({credentialsId=jenkins-staging-docker-prod-token, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyContainer.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyContainer.sh(
docker login -u DOCKER_USERNAME -p DOCKER_PASSWORD
gcrane cp opensearchstaging/ci-runner-staging:latest opensearchproject/ci-runner-staging:latest
)
docker-ecr-promote.echo(Skipping stage ecr-promote-to-prod)
docker-ecr-promote.script(groovy.lang.Closure)
docker-ecr-promote.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
docker-ecr-promote.sh(docker logout)

0 comments on commit 39446d4

Please sign in to comment.