TP Jenkins
TP Jenkins
Jenkins
S3 bucket
Bucket S3 déjà alloué pour stocker les fichiers du projet.
Aws credentials
Un compte AWS
avec les permissions pour créer des instances EC2, interagir avec S3 et utiliser
l'API EC2.
S3 file creation
Create credentials on jenkins:
stages {
stage('get_code') {
steps {
script {
// Configuration des variables
def repoUrl = '[email protected]:Mrxa69K/tp_deploy.git' //
Repository URL
def zipFile = 'project.zip'
def s3Bucket = 'jenkinsbucket69' // Your S3 bucket name
def awsRegion = 'us-east-1' // Replace with your AWS
region
pipeline {
agent { label 'master' }
stages {
stage('run_code') {
steps {
script {
// Configuration des variables
def zipFile = 'project.zip'
def s3Bucket = 'jenkinsbucket69' // Your S3 bucket name
def awsRegion = 'us-west-2' // Change this to your AWS region if needed
// Dézipper le fichier
sh "unzip ${zipFile}"
pipeline {
agent { label 'master' } // Use the same agent as Stage 2
stages {
stage('run_code') {
steps {
script {
// Configuration des variables
def zipFile = 'project.zip'
def s3Bucket = 'jenkinsbucket69' // Your S3 bucket name
stage('test_code') {
steps {
script {
// Exécuter les tests unitaires avec pytest
sh '''
source venv/bin/activate
pip install pytest
pytest tp_deploy/test_hello.py
'''
}
}
}
}
}
Stage 4
stages {
stage('Launch EC2 Instance') {
steps {
script {
// Configuration des variables
def instanceType = 't2.micro'
def amiId = 'ami-0ebfd941bbafe70c6' // AMI ID
def keyName = 'jenkin'
def securityGroupId = 'sg-05c656607d714e983' // Security group ID
def tagName = 'MyEC2Instance' // Tag name for the instance
pipeline {
agent { label 'agent1' }
stages {
stage('deploy_server') {
steps {
script {
// Configuration des variables
def ec2PublicIp = '54.235.239.14' // Replace with the EC2 instance
public IP
def keyPath = '/var/lib/jenkins/.ssh/id_rsa' // Path to your SSH key
def s3Bucket = 'jenkinsbucket69'
def s3ObjectKey = 'project.zip'
def ec2User = 'ec2-user' // or 'ubuntu', depending on the AMI used
// Utilisation des identifiants AWS pour accéder au bucket S3
withCredentials([
string(credentialsId: 'aws-access-key-id', variable:
'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret-access-key', variable:
'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: 'aws-session-token', variable:
'AWS_SESSION_TOKEN')
]) {
// SSH into the EC2 instance and deploy the server
sh """
ssh -o StrictHostKeyChecking=no -i ${keyPath}
${ec2User}@${ec2PublicIp} << EOF
EOF
"""
}
}
}
}
}
}