From f3c4a6f8e54aec1ad36b6c035923f3decac3f9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20V=C3=A1squez?= Date: Wed, 29 Oct 2025 16:04:53 -0300 Subject: [PATCH] update --- Jenkinsfile | 67 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e10b79b..ec74155 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,23 @@ pipeline { agent any + options { skipDefaultCheckout(true) } + + environment { + IMAGE_NAME = 'testing-python-app' + IMAGE_TAG = 'latest' + CONTAINER_NAME = "${IMAGE_NAME}-container" + + AWS_URL = '231670719202.dkr.ecr.sa-east-1.amazonaws.com' + AWS_USER = 'liesa' + AWS_REGION = 'sa-east-1' + AWS_CREDS_ID = 'aws-jenkins' + AWS_REGISTRY = "${AWS_URL}/${AWS_USER}/${IMAGE_NAME}" + AWS_IMAGE = "${AWS_REGISTRY}:${IMAGE_TAG}" + + FORGEJO_URL = 'ssh://git@forgejo.test.dev.it.liesa.com.ar/' + FORGEJO_OWNER = 'ramon.vasquez' + FORGEJO_REPO = 'py_test.git' + } stages { stage('Checkout') { @@ -7,12 +25,32 @@ pipeline { checkout scm } } + stage('AWS ECR Login') { + steps { + withAWS(credentials: env.AWS_CREDS_ID, region: env.AWS_REGION) { + script { + def accountId = sh( + script: 'set -euo pipefail; aws sts get-caller-identity --query Account --output text', + returnStdout: true + ).trim() + env.ECR_REGISTRY = "${accountId}.dkr.ecr.${env.AWS_REGION}.amazonaws.com" + } + sh ''' + set -euo pipefail + echo "Logging in to AWS ECR: ${ECR_REGISTRY}" + aws ecr get-login-password --region "$AWS_REGION" \ + | docker login --username AWS --password-stdin "$ECR_REGISTRY" + ''' + } + } + } stage('Build Image') { steps { - echo 'Building image...' - // --pull ensures base layer is fresh; tag includes BUILD_NUMBER to avoid cache clashes - sh 'docker build --pull -t my-python-app:${BUILD_NUMBER} .' + sh "echo ${AWS_REGISTRY}:${IMAGE_TAG}" + script { + docker.build("${AWS_REGISTRY}:${IMAGE_TAG}", '-f . --no-cache --pull') + } } } @@ -24,29 +62,10 @@ pipeline { } } - stage('(Optional) Verify Output') { + stage('Push Image') { steps { - script { - // Re-run capturing output to assert expected behavior (example) - def out = sh( - script: 'docker run --rm my-python-app:${BUILD_NUMBER}', - returnStdout: true - ).trim() - if (out != 'Hello, World!') { - error "Unexpected output: '${out}'" - } - } + sh 'docker push "${AWS_REGISTRY}:${IMAGE_TAG}"' } } } - - post { - always { - // Best-effort cleanup of any leftover container/image for this build tag - sh ''' - docker rm -f my-python-app-${BUILD_NUMBER} 2>/dev/null || true - docker image rm my-python-app:${BUILD_NUMBER} 2>/dev/null || true - ''' - } - } }