Skip to content

Commit

Permalink
tag image as latest
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Gupta <[email protected]>
  • Loading branch information
abhinavGupta16 committed Apr 20, 2022
1 parent ec1e4dd commit da02b77
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
22 changes: 22 additions & 0 deletions jenkins/docker-ecr/docker-ecr-promote.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ pipeline {
name: 'PLATFORM',
description: 'Where do you want to push the images?'
)
booleanParam(
defaultValue: true,
name: 'TAG_LATEST',
description: 'Tag the copied image as latest'
)
}

stages {
Expand All @@ -42,6 +47,14 @@ pipeline {
destinationType: "docker",
destinationCredentialIdentifier: "jenkins-staging-docker-prod-token"
)
if(TAG_LATEST == "true"){
copyContainer(
sourceImagePath: "opensearchstaging/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationImagePath: "opensearchproject/${IMAGE_REPOSITORY}:latest",
destinationType: "docker",
destinationCredentialIdentifier: "jenkins-staging-docker-prod-token"
)
}
}
}
}
Expand All @@ -58,6 +71,15 @@ pipeline {
destinationCredentialIdentifier: "public.ecr.aws/p5f6l6i3",
accountName: "${AWS_ACCOUNT_ARTIFACT}"
)
if(TAG_LATEST == "true"){
copyContainer(
sourceImagePath: "opensearchstaging/${IMAGE_REPOSITORY}:${IMAGE_TAG}",
destinationImagePath: "public.ecr.aws/p5f6l6i3/${IMAGE_REPOSITORY}:latest",
destinationType: "ecr",
destinationCredentialIdentifier: "public.ecr.aws/p5f6l6i3",
accountName: "${AWS_ACCOUNT_ARTIFACT}"
)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/jenkins/TestDockerPromoteWithDockerhubOnlyJob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TestDockerPromoteWithDockerhubOnlyJob extends BuildPipelineTest {
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'docker-hub')
binding.setVariable('TAG_LATEST', 'false')

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestDockerPromoteWithEcrAndDockerhubJob extends BuildPipelineTest {
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'docker-hub + ECR')
binding.setVariable('TAG_LATEST', 'false')

}

Expand Down
1 change: 1 addition & 0 deletions tests/jenkins/TestDockerPromoteWithEcrOnlyJob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestDockerPromoteWithEcrOnlyJob extends BuildPipelineTest {
binding.setVariable('IMAGE_TAG', imageTag)
binding.setVariable('AWS_ACCOUNT_ARTIFACT', accountName)
binding.setVariable('PLATFORM', 'ECR')
binding.setVariable('TAG_LATEST', 'false')

}

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

class TestDockerPromoteWithLatestTagDockerHubJob extends BuildPipelineTest {

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

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

this.registerLibTester(new CopyContainerLibTester(sourceImagePath,
"opensearchproject/${imageRepository}:latest",
'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')
binding.setVariable('TAG_LATEST', 'true')

}

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

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

class TestDockerPromoteWithLatestTagEcrOnlyJob extends BuildPipelineTest {

@Before
void setUp() {
String imageRepository = 'ci-runner-staging'
String imageTag = '1.3.0'
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))

this.registerLibTester(new CopyContainerLibTester(sourceImagePath,
"public.ecr.aws/p5f6l6i3/${imageRepository}:latest",
'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')
binding.setVariable('TAG_LATEST', 'true')

}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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:1.3.0, destinationImagePath=public.ecr.aws/p5f6l6i3/ci-runner-staging:1.3.0, 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:1.3.0 public.ecr.aws/p5f6l6i3/ci-runner-staging:1.3.0
)
docker-ecr-promote.copyContainer({sourceImagePath=opensearchstaging/ci-runner-staging:1.3.0, 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:1.3.0 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,31 @@
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:1.3.0, destinationImagePath=opensearchproject/ci-runner-staging:1.3.0, 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:1.3.0 opensearchproject/ci-runner-staging:1.3.0
)
docker-ecr-promote.copyContainer({sourceImagePath=opensearchstaging/ci-runner-staging:1.3.0, 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:1.3.0 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 da02b77

Please sign in to comment.