From c754ff2d60b6f72f8d754216d660cc64ab2f18a0 Mon Sep 17 00:00:00 2001 From: Nana Janashia Date: Sun, 20 Dec 2020 17:25:56 +0100 Subject: [PATCH] fix ecr authentication config --- Jenkinsfile | 17 +++++++++++------ terraform/ecr-repo.tf | 2 ++ terraform/outputs.tf | 9 +++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f75c258..db16717 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -29,7 +29,14 @@ pipeline { script: "terraform output cluster_url", returnStdout: true ) - sh "ls" + env.REPO_USER = sh( + script: "terraform output ecr_user_name", + returnStdout: true + ) + env.REPO_PWD = sh( + script: "terraform output ecr_user_password", + returnStdout: true + ) env.KUBECONFIG="./kubeconfig.yaml" sh "kubectl get node" } @@ -61,11 +68,9 @@ pipeline { steps { script { echo "building the docker image..." - withCredentials([usernamePassword(credentialsId: 'ecr-credentials', passwordVariable: 'PASS', usernameVariable: 'USER')]) { - sh "docker build -t ${DOCKER_REPO_URL}:${IMAGE_NAME} ." - sh "echo $PASS | docker login -u $USER --password-stdin ${DOCKER_REPO_URL}" - sh "docker push ${DOCKER_REPO_URL}:${IMAGE_NAME}" - } + sh "docker build -t ${DOCKER_REPO_URL}:${IMAGE_NAME} ." + sh "echo ${REPO_PWD} | docker login -u ${REPO_USER} --password-stdin ${DOCKER_REPO_URL}" + sh "docker push ${DOCKER_REPO_URL}:${IMAGE_NAME}" } } } diff --git a/terraform/ecr-repo.tf b/terraform/ecr-repo.tf index b7b5aed..45154fd 100644 --- a/terraform/ecr-repo.tf +++ b/terraform/ecr-repo.tf @@ -7,3 +7,5 @@ resource "aws_ecr_repository" "myapp-repo" { scan_on_push = true } } + +data "aws_ecr_authorization_token" "token" {} \ No newline at end of file diff --git a/terraform/outputs.tf b/terraform/outputs.tf index ee02a4c..8772c5c 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -9,3 +9,12 @@ output cluster_url { output kubeconfig { value = "module.eks.kubeconfig" } + +output ecr_user_name { + value = data.aws_ecr_authorization_token.token.user_name +} + +output ecr_user_password { + value = data.aws_ecr_authorization_token.token.password + sensitive = true +} \ No newline at end of file