diff --git a/infrastructure/services/Jenkinsfile b/infrastructure/services/Jenkinsfile index a807532..f3c54b1 100644 --- a/infrastructure/services/Jenkinsfile +++ b/infrastructure/services/Jenkinsfile @@ -254,11 +254,11 @@ with app.test_client() as client: } stage('đŸŗ Build & Push Docker Image') { when { - equals expected: 'deploy', actual: params.ACTION + equals expected: 'deploy', actual: params.ACTION } steps { withCredentials([ - [$class: 'AmazonWebServicesCredentialsBinding', + [$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-ci', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'], @@ -278,45 +278,50 @@ with app.test_client() as client: echo "ECR Repository: ${ECR_REPO_URI}" echo "Image Tag: ${IMAGE_TAG}" - # Login to ECR - echo "🔐 Logging into ECR..." - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY - - # Check if ECR repository exists, create if not - echo "đŸ“Ļ Checking ECR repository..." - if ! aws ecr describe-repositories --repository-names $ECR_REPO_NAME --region $AWS_REGION 2>/dev/null; then - echo "đŸ—ī¸ Creating ECR repository..." - aws ecr create-repository --repository-name $ECR_REPO_NAME --region $AWS_REGION + # Check if ECR repository exists FIRST (before login) + echo "đŸ“Ļ Checking if ECR repository exists..." + if aws ecr describe-repositories --repository-names ${ECR_REPO_NAME} --region ${AWS_REGION} 2>/dev/null; then + echo "✅ ECR repository exists - proceeding with Docker operations" + + # Login to ECR only if repository exists + echo "🔐 Logging into ECR..." + aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY} + + # Build Docker image + echo "🔨 Building Docker image..." + docker build -t ${ECR_REPO_NAME}:${IMAGE_TAG} . + docker build -t ${ECR_REPO_NAME}:build-${BUILD_NUMBER} . + + # Tag for ECR + docker tag ${ECR_REPO_NAME}:${IMAGE_TAG} ${ECR_REPO_URI}:${IMAGE_TAG} + docker tag ${ECR_REPO_NAME}:${IMAGE_TAG} ${ECR_REPO_URI}:build-${BUILD_NUMBER} + + # Push to ECR + echo "🚀 Pushing to ECR..." + docker push ${ECR_REPO_URI}:${IMAGE_TAG} + docker push ${ECR_REPO_URI}:build-${BUILD_NUMBER} + + # Clean up local images + docker rmi ${ECR_REPO_NAME}:${IMAGE_TAG} || true + docker rmi ${ECR_REPO_NAME}:build-${BUILD_NUMBER} || true + docker rmi ${ECR_REPO_URI}:${IMAGE_TAG} || true + docker rmi ${ECR_REPO_URI}:build-${BUILD_NUMBER} || true + + echo "✅ Docker build and push completed" + echo "📍 Image available at: ${ECR_REPO_URI}:${IMAGE_TAG}" + else + echo "âš ī¸ ECR repository '${ECR_REPO_NAME}' doesn't exist yet" + echo "📝 Terraform will create it in the Apply stage" + echo "â­ī¸ Skipping Docker build/push for this initial deployment" + echo "" + echo "â„šī¸ NOTE: After this pipeline completes, the ECR repository will exist." + echo " Run the pipeline again to build and push the Docker image." fi - - # Build Docker image - echo "🔨 Building Docker image..." - docker build -t $ECR_REPO_NAME:${IMAGE_TAG} . - docker build -t $ECR_REPO_NAME:build-${BUILD_NUMBER} . - - # Tag for ECR - docker tag $ECR_REPO_NAME:${IMAGE_TAG} $ECR_REPO_URI:${IMAGE_TAG} - docker tag $ECR_REPO_NAME:${IMAGE_TAG} $ECR_REPO_URI:build-${BUILD_NUMBER} - - # Push to ECR - echo "🚀 Pushing to ECR..." - docker push $ECR_REPO_URI:${IMAGE_TAG} - docker push $ECR_REPO_URI:build-${BUILD_NUMBER} - - # Clean up local images - docker rmi $ECR_REPO_NAME:${IMAGE_TAG} || true - docker rmi $ECR_REPO_NAME:build-${BUILD_NUMBER} || true - docker rmi $ECR_REPO_URI:${IMAGE_TAG} || true - docker rmi $ECR_REPO_URI:build-${BUILD_NUMBER} || true - - echo "✅ Docker build and push completed" - echo "📍 Image available at: $ECR_REPO_URI:${IMAGE_TAG}" ''' } } } - } - + } stage('🚀 Infrastructure Bootstrap') { when { not { equals expected: 'destroy', actual: params.ACTION }