From cf74eba995a9b7c331a4683d0dcee356c65b79c5 Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad Date: Thu, 23 Jun 2022 15:38:21 -0700 Subject: [PATCH] Update uploadIndexFile lib env vars with credentials Signed-off-by: Sayali Gaikawad --- tests/jenkins/TestUploadIndexFile.groovy | 2 +- tests/jenkins/jobs/UploadIndexFile_Jenkinsfile.txt | 14 ++++++++------ .../lib-testers/UploadIndexFileLibTester.groovy | 7 ++++++- vars/uploadIndexFile.groovy | 10 ++++++---- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/jenkins/TestUploadIndexFile.groovy b/tests/jenkins/TestUploadIndexFile.groovy index 90f90dda66..af51feb0a8 100644 --- a/tests/jenkins/TestUploadIndexFile.groovy +++ b/tests/jenkins/TestUploadIndexFile.groovy @@ -14,7 +14,7 @@ class TestUploadIndexFile extends BuildPipelineTest { @Before void setUp() { this.registerLibTester(new UploadIndexFileLibTester('test')) - this.registerLibTester(new UploadToS3LibTester('index.json', 'dummy_bucket_name', 'test/index.json')) + this.registerLibTester(new UploadToS3LibTester('index.json', 'ARTIFACT_BUCKET_NAME', 'test/index.json')) super.setUp() } diff --git a/tests/jenkins/jobs/UploadIndexFile_Jenkinsfile.txt b/tests/jenkins/jobs/UploadIndexFile_Jenkinsfile.txt index 8d1fff03d3..10f21403ca 100644 --- a/tests/jenkins/jobs/UploadIndexFile_Jenkinsfile.txt +++ b/tests/jenkins/jobs/UploadIndexFile_Jenkinsfile.txt @@ -7,9 +7,11 @@ UploadIndexFile_Jenkinsfile.script(groovy.lang.Closure) UploadIndexFile_Jenkinsfile.uploadIndexFile({indexFilePath=test}) uploadIndexFile.writeJSON({file=index.json, json={latest=123}}) - uploadIndexFile.echo(Uploading index.json to s3://dummy_bucket_name/test) - uploadIndexFile.uploadToS3({sourcePath=index.json, bucket=dummy_bucket_name, path=test/index.json}) - uploadToS3.string({credentialsId=jenkins-aws-account-public, variable=AWS_ACCOUNT_PUBLIC}) - uploadToS3.withCredentials([AWS_ACCOUNT_PUBLIC], groovy.lang.Closure) - uploadToS3.withAWS({role=opensearch-bundle, roleAccount=AWS_ACCOUNT_PUBLIC, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure) - uploadToS3.s3Upload({file=index.json, bucket=dummy_bucket_name, path=test/index.json}) + uploadIndexFile.string({credentialsId=jenkins-artifact-bucket-name, variable=ARTIFACT_BUCKET_NAME}) + uploadIndexFile.withCredentials([ARTIFACT_BUCKET_NAME], groovy.lang.Closure) + uploadIndexFile.echo(Uploading index.json to s3://ARTIFACT_BUCKET_NAME/test) + uploadIndexFile.uploadToS3({sourcePath=index.json, bucket=ARTIFACT_BUCKET_NAME, path=test/index.json}) + uploadToS3.string({credentialsId=jenkins-aws-account-public, variable=AWS_ACCOUNT_PUBLIC}) + uploadToS3.withCredentials([AWS_ACCOUNT_PUBLIC], groovy.lang.Closure) + uploadToS3.withAWS({role=opensearch-bundle, roleAccount=AWS_ACCOUNT_PUBLIC, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure) + uploadToS3.s3Upload({file=index.json, bucket=ARTIFACT_BUCKET_NAME, path=test/index.json}) diff --git a/tests/jenkins/lib-testers/UploadIndexFileLibTester.groovy b/tests/jenkins/lib-testers/UploadIndexFileLibTester.groovy index e69512e53f..0cd1ef0e40 100644 --- a/tests/jenkins/lib-testers/UploadIndexFileLibTester.groovy +++ b/tests/jenkins/lib-testers/UploadIndexFileLibTester.groovy @@ -11,7 +11,12 @@ class UploadIndexFileLibTester extends LibFunctionTester { void configure(helper, binding) { binding.setVariable('BUILD_NUMBER', '123') - binding.setVariable('ARTIFACT_PRODUCTION_BUCKET_NAME', 'dummy_bucket_name') + binding.setVariable('ARTIFACT_BUCKET_NAME', 'dummy_bucket_name') + + helper.registerAllowedMethod("withCredentials", [Map, Closure], { args, closure -> + closure.delegate = delegate + return helper.callClosure(closure) + }) helper.registerAllowedMethod("writeJSON", [Map]) } diff --git a/vars/uploadIndexFile.groovy b/vars/uploadIndexFile.groovy index cc3e4e2234..efaf0a34f3 100644 --- a/vars/uploadIndexFile.groovy +++ b/vars/uploadIndexFile.groovy @@ -2,11 +2,13 @@ void call(Map args = [:]) { def latestBuildData = ['latest': "${BUILD_NUMBER}"] writeJSON file: 'index.json', json: latestBuildData - echo "Uploading index.json to s3://${ARTIFACT_PRODUCTION_BUCKET_NAME}/${args.indexFilePath}" + withCredentials([string(credentialsId: 'jenkins-artifact-bucket-name', variable: 'ARTIFACT_BUCKET_NAME')]) { + echo "Uploading index.json to s3://${ARTIFACT_BUCKET_NAME}/${args.indexFilePath}" - uploadToS3( + uploadToS3( sourcePath: 'index.json', bucket: "${ARTIFACT_BUCKET_NAME}", path: "${args.indexFilePath}/index.json" - ) -} \ No newline at end of file + ) + } +}