Jenkins as CI-CD (Continuous integration and continuous deployment) tool, scripted a Declarative pipeline for various stages
Stage 1. Using Git to access GitHub to check out the code ,the connection between jenkins and GitHub is SSH, using Public
and Private key to access private repository,
Stage 2. Building a JAR file(Java ARchive) using Maven command # mvn clean, mvn install
Stage 3. so, in next stage Docker can utilize the JAR file to create docker image based on Docker file.
Stage 4. push the created Docker image into ECR(AWS-Elastic Container Registry) using "View Push Command" provided by AWS.
Stage 5. As I am using t2micro instance limited on resource, I am stopping and deleting the previous Build running container.
docker ps -aq | xargs --no-run-if-empty docker stop | xargs --no-run-if-empty docker rm
"docker ps -aq" will show all running container/ all container ID. "xargs --no-run-if-empty" to suppress if no ID found by docker ps
Stage6. Using the docker image which we pushed to ECR in previous stage and running it in the same EC2 instance
as Containerized application to Accomplish Software Development Lifecycle.
https://www.jenkins.io/doc/tutorials/tutorial-for-installing-jenkins-on-AWS
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html
sudo usermod -a -G docker jenkins
sudo service jenkins restart
sudo service docker restart
sudo systemctl daemon-reload
yum install git
Create an IAM role with "AmazonEC2ContainerRegistryFullAccess" policy, attach to Jenkins
agent any
tools{
maven 'maven'
}
stages{
stage('scm-git'){
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'provide the credential here', url: '[email protected]:shankar439/demoSpringApp.git']]])
}
}
stage('Build') {
steps {
sh 'mvn clean'
sh 'mvn install'
}
}
stage('buils image'){
steps{
script{
sh 'docker build -t hello-app .'
}
}
}
stage('push'){
steps{
script{
sh 'aws ecr get-login-password --regi on ap-south-1 | docker login --username AWS --password-stdin accountNO.dkr.ecr.ap-south-1.amazonaws.com'
sh 'docker tag hello-app:latest accountNO.dkr.ecr.ap-south-1.amazonaws.com/aws-ecr-repo-demo-job:latest'
sh 'docker push accountNO.dkr.ecr.ap-south-1.amazonaws.com/aws-ecr-repo-demo-job:latest'
}
}
}
stage('cleaning containers') {
steps {
script{
sh 'docker ps -aq | xargs --no-run-if-empty docker stop | xargs --no-run-if-empty docker rm'
}
}
}
stage('Docker Run') {
steps{
script {
sh 'docker run -d -p 9090:9090 accountNO.dkr.ecr.ap-south-1.amazonaws.com/aws-ecr-repo-demo-job:latest'
}
}
}
}
}