Skip to content

Commit

Permalink
CopyContainer documentation (#1609)
Browse files Browse the repository at this point in the history
Add additional documentation for the copyContainer function to make it clear what source/destinations can be used.

Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied authored Feb 9, 2022
1 parent 98fd3b7 commit 18f6e51
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 76 deletions.
2 changes: 1 addition & 1 deletion jenkins/docker-ecr/docker-build-with-ecr.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pipeline {
stage('docker-ecr-staging') {
steps {
script {
copyDockerImage(
copyContainer(
sourceImagePath: "opensearchstaging/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationImagePath: "public.ecr.aws/m0o1u6w1/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationType: "ecr",
Expand Down
4 changes: 2 additions & 2 deletions jenkins/docker-ecr/docker-ecr-promote.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipeline {
stage('dockerhub-promote-to-prod') {
steps {
script {
copyDockerImage(
copyContainer(
sourceImagePath: "opensearchstaging/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationImagePath: "opensearchproject/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationType: "docker",
Expand All @@ -40,7 +40,7 @@ pipeline {
stage('ecr-promote-to-prod') {
steps {
script {
copyDockerImage(
copyContainer(
sourceImagePath: "opensearchstaging/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationImagePath: "public.ecr.aws/p5f6l6i3/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationType: "ecr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import jenkins.tests.BuildPipelineTest
import org.junit.*


class TestCopyDockerImage extends BuildPipelineTest {
class TestCopyContainer extends BuildPipelineTest {

@Before
void setUp() {

this.registerLibTester(new CopyDockerImageLibTester('opensearchstaging/ci-runner:latest',
this.registerLibTester(new CopyContainerLibTester('opensearchstaging/ci-runner:latest',
'opensearchproject/ci-runner:latest',
'ecr',
'public.ecr.aws/p5f6l6i3',
'DUMMY_ACCOUNT_NAME'))

this.registerLibTester(new CopyDockerImageLibTester('opensearchstaging/ci-runner:latest',
this.registerLibTester(new CopyContainerLibTester('opensearchstaging/ci-runner:latest',
'opensearchproject/ci-runner:latest',
'docker',
'jenkins-staging-docker-prod-token'))
Expand All @@ -32,6 +32,6 @@ class TestCopyDockerImage extends BuildPipelineTest {

@Test
public void testForDockerhub() {
super.testPipeline("tests/jenkins/jobs/CopyDockerImage_docker_Jenkinsfile")
super.testPipeline("tests/jenkins/jobs/CopyContainer_docker_Jenkinsfile")
}
}
2 changes: 1 addition & 1 deletion tests/jenkins/TestDockerBuildWithEcrJob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestDockerBuildWithEcrJob extends BuildPipelineTest {
String accountName = 'aws_account_public'


this.registerLibTester(new CopyDockerImageLibTester("opensearchstaging/${imageRepository}:${imageTag}",
this.registerLibTester(new CopyContainerLibTester("opensearchstaging/${imageRepository}:${imageTag}",
"public.ecr.aws/m0o1u6w1/${imageRepository}:${imageTag}",
'ecr',
'public.ecr.aws/m0o1u6w1',
Expand Down
4 changes: 2 additions & 2 deletions tests/jenkins/TestDockerPromoteJob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class TestDockerPromoteJob extends BuildPipelineTest {
String accountName = 'aws_account_artifact'
String sourceImagePath = "opensearchstaging/${imageRepository}:${imageTag}"

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

this.registerLibTester(new CopyDockerImageLibTester(sourceImagePath,
this.registerLibTester(new CopyContainerLibTester(sourceImagePath,
"opensearchproject/${imageRepository}:${imageTag}",
'docker',
'jenkins-staging-docker-prod-token'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
)
docker-build-with-ecr.stage(docker-ecr-staging, groovy.lang.Closure)
docker-build-with-ecr.script(groovy.lang.Closure)
docker-build-with-ecr.copyDockerImage({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})
copyDockerImage.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyDockerImage.sh(docker logout)
copyDockerImage.withAWS({role=Upload_ECR_Image, roleAccount=aws_account_public, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure)
copyDockerImage.sh(
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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
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.copyDockerImage({sourceImagePath=opensearchstaging/ci-runner-staging:latest, destinationImagePath=opensearchproject/ci-runner-staging:latest, destinationType=docker, destinationCredentialIdentifier=jenkins-staging-docker-prod-token})
copyDockerImage.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyDockerImage.sh(docker logout)
copyDockerImage.usernamePassword({credentialsId=jenkins-staging-docker-prod-token, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyDockerImage.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyDockerImage.sh(
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.stage(ecr-promote-to-prod, groovy.lang.Closure)
docker-ecr-promote.script(groovy.lang.Closure)
docker-ecr-promote.copyDockerImage({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})
copyDockerImage.sh({script=test -f /usr/local/bin/gcrane && echo '1' || echo '0' , returnStdout=true})
copyDockerImage.sh(docker logout)
copyDockerImage.withAWS({role=Upload_ECR_Image, roleAccount=aws_account_artifact, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure)
copyDockerImage.sh(
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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ pipeline {
stage('copyImage') {
steps {
script {
copyDockerImage(
copyContainer(
sourceImagePath: 'opensearchstaging/ci-runner:latest',
destinationImagePath: 'opensearchproject/ci-runner:latest',
destinationType: 'docker',
destinationCredentialIdentifier: 'jenkins-staging-docker-prod-token'
)

copyDockerImage(
copyContainer(
sourceImagePath: 'opensearchstaging/ci-runner:latest',
destinationImagePath: 'opensearchproject/ci-runner:latest',
destinationType: 'ecr',
Expand Down
22 changes: 22 additions & 0 deletions tests/jenkins/jobs/CopyContainer_docker_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CopyContainer_docker_Jenkinsfile.run()
CopyContainer_docker_Jenkinsfile.pipeline(groovy.lang.Closure)
CopyContainer_docker_Jenkinsfile.echo(Executing on agent [label:none])
CopyContainer_docker_Jenkinsfile.stage(copyImage, groovy.lang.Closure)
CopyContainer_docker_Jenkinsfile.script(groovy.lang.Closure)
CopyContainer_docker_Jenkinsfile.copyContainer({sourceImagePath=opensearchstaging/ci-runner:latest, destinationImagePath=opensearchproject/ci-runner: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:latest opensearchproject/ci-runner:latest
)
CopyContainer_docker_Jenkinsfile.copyContainer({sourceImagePath=opensearchstaging/ci-runner:latest, destinationImagePath=opensearchproject/ci-runner:latest, destinationType=ecr, destinationCredentialIdentifier=public.ecr.aws/p5f6l6i3, accountName=DUMMY_ACCOUNT_NAME})
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=DUMMY_ACCOUNT_NAME, 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:latest opensearchproject/ci-runner:latest
)
22 changes: 0 additions & 22 deletions tests/jenkins/jobs/CopyDockerImage_docker_Jenkinsfile.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,67 @@ import static org.hamcrest.CoreMatchers.equalTo
import static org.hamcrest.CoreMatchers.notNullValue
import static org.hamcrest.MatcherAssert.assertThat

class CopyDockerImageLibTester extends LibFunctionTester {
class CopyContainerLibTester extends LibFunctionTester {

private String sourceImagePath
private String destinationImagePath
private String destinationType
private String destinationCredentialIdentifier
private String accountName
private final String sourceImagePath
private final String destinationImagePath
private final String destinationType
private final String destinationCredentialIdentifier
private final String accountName

public CopyDockerImageLibTester(String sourceImagePath, String destinationImagePath, String destinationType,
String destinationCredentialIdentifier, String accountName=null) {
CopyContainerLibTester(
String sourceImagePath,
String destinationImagePath,
String destinationType,
String destinationCredentialIdentifier,
String accountName=null) {
this.sourceImagePath = sourceImagePath
this.destinationImagePath = destinationImagePath
this.destinationType = destinationType
this.destinationCredentialIdentifier = destinationCredentialIdentifier
this.accountName = accountName
}

String libFunctionName(){
return 'copyDockerImage'
String libFunctionName() {
return 'copyContainer'
}

void configure(helper, binding){

binding.setVariable('DOCKER_USERNAME', 'dummy_docker_username')
binding.setVariable('DOCKER_PASSWORD', 'dummy_docker_password')

helper.registerAllowedMethod("withAWS", [Map, Closure], { args, closure ->
helper.registerAllowedMethod('withAWS', [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
}

void parameterInvariantsAssertions(call){
void parameterInvariantsAssertions(call) {
assertThat(call.args.sourceImagePath.first(), notNullValue())
assertThat(call.args.destinationImagePath.first(), notNullValue())
assertThat(call.args.destinationType.first(), notNullValue())
assertThat(call.args.destinationType.first(), anyOf(equalTo('ecr'), equalTo('docker')))
assertThat(call.args.destinationCredentialIdentifier.first(), notNullValue())
if(call.args.destinationType.first().toString() == "docker") {
assertThat(call.args.destinationCredentialIdentifier.first(), anyOf(equalTo('jenkins-staging-docker-prod-token'),
if (call.args.destinationType.first().toString() == 'docker') {
assertThat(
call.args.destinationCredentialIdentifier.first(),
anyOf(
equalTo('jenkins-staging-docker-prod-token'),
equalTo('jenkins-staging-docker-staging-credential')))
}
if(call.args.destinationType.first().toString() == "ecr"){
if (call.args.destinationType.first().toString() == 'ecr') {
assertThat(call.args.accountName.first(), notNullValue())
assertThat(call.args.destinationCredentialIdentifier.first(), anyOf(equalTo('public.ecr.aws/p5f6l6i3'),
assertThat(call.args.destinationCredentialIdentifier.first(),
anyOf(
equalTo('public.ecr.aws/p5f6l6i3'),
equalTo('public.ecr.aws/m0o1u6w1')))
}
}

boolean expectedParametersMatcher(call){
boolean expectedParametersMatcher(call) {
boolean accountNameFound = true

if(call.args.destinationType.first() == "ecr"){
if (call.args.destinationType.first() == 'ecr') {
accountNameFound = call.args.accountName.first().toString() == accountName
}

Expand Down
Loading

0 comments on commit 18f6e51

Please sign in to comment.