py_test/Jenkinsfile
2025-10-29 16:06:09 -03:00

71 lines
2.5 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"
'''
}
}
}
stage('Build Image') {
steps {
sh "echo ${AWS_REGISTRY}:${IMAGE_TAG}"
script {
docker.build("${AWS_REGISTRY}:${IMAGE_TAG}", '-f Dockerfile . --no-cache --pull')
}
}
}
stage('Run Container') {
steps {
echo 'Running container...'
// --rm cleans up; this will print "Hello, World!" to the console
sh 'docker run --rm --name my-python-app-${BUILD_NUMBER} my-python-app:${BUILD_NUMBER}'
}
}
stage('Push Image') {
steps {
sh 'docker push "${AWS_REGISTRY}:${IMAGE_TAG}"'
}
}
}
}