py_test/Jenkinsfile
2025-10-29 16:14:36 -03:00

80 lines
2.8 KiB
Groovy

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') {
steps {
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"
'''
sh '''
aws ecr describe-repositories --repository-names "liesa/testing-python-app" || \
aws ecr create-repository --repository-name "liesa/testing-python-app"
'''
}
}
}
stage('Build Image') {
steps {
sh "echo ${AWS_REGISTRY}:${IMAGE_TAG}"
script {
docker.build("${AWS_REGISTRY}:${IMAGE_TAG}", '-f Dockerfile . --no-cache --pull')
}
}
}
stage('Push Image') {
steps {
sh 'docker push "${AWS_REGISTRY}:${IMAGE_TAG}"'
}
}
stage('Pull Image') {
steps {
sleep time: 30, unit: 'SECONDS'
sh 'docker pull "${AWS_REGISTRY}:${IMAGE_TAG}"'
}
}
stage('Run Container') {
steps {
echo 'Running container...'
sh 'docker run --rm --name ${IMAGE_NAME} ${AWS_REGISTRY}:${IMAGE_TAG}'
}
}
}
}