-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
138 lines (124 loc) · 5.36 KB
/
Copy pathJenkinsfile
File metadata and controls
138 lines (124 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
pipeline {
agent any
environment {
AWS_REGION = 'eu-west-1'
AWS_ACCOUNT_ID = '538079272432'
ECR_REGISTRY = '538079272432.dkr.ecr.eu-west-1.amazonaws.com'
IMAGE_NAME = 'gitops-stack'
IMAGE_TAG = "${BUILD_NUMBER}"
}
stages {
stage('Test') {
steps {
echo 'Ejecutando tests...'
sh 'pip install -r app/requirements.txt --break-system-packages'
sh 'pytest tests/'
}
}
stage('Build imagen Docker') {
steps {
echo 'Construyendo imagen Docker...'
sh "docker build -t ${IMAGE_NAME}:${IMAGE_TAG} ."
}
}
stage('Push a ECR') {
steps {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials'
]]) {
echo 'Publicando imagen en ECR...'
sh """
aws ecr get-login-password --region ${AWS_REGION} | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${ECR_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
docker push ${ECR_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
"""
}
}
}
stage('Deploy con Terraform') {
steps {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials'
]]) {
echo 'Desplegando infraestructura con Terraform...'
dir('terraform') {
sh 'terraform init'
sh 'terraform plan'
input message: '¿Aplicar cambios en producción?'
sh 'terraform apply -auto-approve'
}
}
}
}
stage('Configurar nodos con Ansible via SSM') {
steps {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials'
]]) {
echo 'Configurando nodos con Ansible via SSM...'
sh """
export NODE1_ID=\$(aws ec2 describe-instances \
--region ${AWS_REGION} \
--filters \
"Name=tag:eks:cluster-name,Values=gitops-stack-prod" \
"Name=instance-state-name,Values=running" \
--query 'Reservations[0].Instances[0].InstanceId' \
--output text)
export NODE2_ID=\$(aws ec2 describe-instances \
--region ${AWS_REGION} \
--filters \
"Name=tag:eks:cluster-name,Values=gitops-stack-prod" \
"Name=instance-state-name,Values=running" \
--query 'Reservations[1].Instances[0].InstanceId' \
--output text)
echo "Nodo 1 ID: \$NODE1_ID"
echo "Nodo 2 ID: \$NODE2_ID"
pip install ansible-ssm --break-system-packages || true
cd ansible
ansible-playbook site.yml \
-e NODE1_ID=\$NODE1_ID \
-e NODE2_ID=\$NODE2_ID
"""
}
}
}
stage('Actualizar Kubernetes') {
steps {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials'
]]) {
echo 'Conectando a EKS y desplegando...'
sh """
# Asumir el rol de admin EKS
CREDS=\$(aws sts assume-role \
--role-arn arn:aws:iam::${AWS_ACCOUNT_ID}:role/eks-admin-role \
--role-session-name jenkins-deploy-${BUILD_NUMBER} \
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
--output text)
export AWS_ACCESS_KEY_ID=\$(echo \$CREDS | awk '{print \$1}')
export AWS_SECRET_ACCESS_KEY=\$(echo \$CREDS | awk '{print \$2}')
export AWS_SESSION_TOKEN=\$(echo \$CREDS | awk '{print \$3}')
aws eks update-kubeconfig --region ${AWS_REGION} --name gitops-stack-prod
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl set image deployment/pipeline-demo pipeline-demo=${ECR_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
kubectl rollout status deployment/pipeline-demo --timeout=120s
"""
}
}
}
}
post {
success {
echo 'Pipeline completado con éxito'
}
failure {
echo 'Pipeline fallido — revisar logs'
}
}
}