Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 142 additions & 48 deletions MF05-PRA05.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,95 @@ This practical exercise will guide you through setting up a Jenkins CI/CD pipeli
Pull the official Jenkins Docker image from DockerHub:

```bash
docker pull jenkins/jenkins:lts
sudo docker pull jenkins/jenkins:lts
```
![](PRA05_ANSWER/screenshots/1-pull-jenkins-docker-image.png)

#### 2. Build and Run Jenkins Container

Create and run a Jenkins container:
Create and run a Jenkins container **with privileges**:

```bash
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts
sudo docker run --privileged -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts
```

![](PRA05_ANSWER/screenshots/2-0-build-and-run-jenkins-container.png)

To pause the container:

```bash
docker pause jenkins
```
![](PRA05_ANSWER/screenshots/2-1-pause-the-container.png)

To resume:

```bash
docker unpause jenkins
```
![](PRA05_ANSWER/screenshots/2-2-to-resume.png)

#### 3. Configure Jenkins Account

Access Jenkins at `http://localhost:8080` and follow the setup wizard. Retrieve the initial admin password from the Docker logs:
Access Jenkins at `http://localhost:8081` and follow the setup wizard. Retrieve the initial admin password from the Docker logs:

```bash
docker logs jenkins
```
![](PRA05_ANSWER/screenshots/3-0-retrieve-initial-admin-pwd.png)

Create an admin account during the setup process.

![](PRA05_ANSWER/screenshots/3-1-creation-admin-account-during-setup-process.png)


#### 3.1 Install Docker inside Jenkins: bash from host

Here's a summary of the steps to install Docker in a Jenkins container:

1. On the Linux host:

```bash
sudo docker exec -it --user root jenkins bash
```
This command accesses the Jenkins container's bash console.

2. Inside the Jenkins container:

```bash
curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall
```
This downloads and runs the Docker installation script.
![](PRA05_ANSWER/screenshots/3-2-installation-docker-in-jenkins.png)

3. Still inside the Jenkins container:

```bash
usermod -aG docker jenkins
```

This adds the Jenkins user to the Docker group.
![](PRA05_ANSWER/screenshots/3-3-jenkins-docker-group.png)

4. If permission issues persist, inside the Jenkins container:

```bash
chmod 666 /var/run/docker.sock
```

This changes the permissions of the Docker socket.
![](PRA05_ANSWER/screenshots/3-4-permissions-docker-socket.png)

5. Back on the Linux host:

```bash
docker restart jenkins2
```

This restarts the Jenkins container to apply the changes.

Note that steps 2-4 are executed within the Jenkins container's bash, while steps 1 and 5 are performed on the Linux host machine.

#### 4. Install Basic Plugins

In Jenkins, go to "Manage Jenkins" > "Manage Plugins" and install these plugins:
Expand All @@ -54,23 +110,59 @@ In Jenkins, go to "Manage Jenkins" > "Manage Plugins" and install these plugins:
- SSH
- SonarQube Scanner

**Plugins** that are **already installed**: Git Client, Pipeline and Pipeline: Graph View.

**Available** plugins to **install**:

- Maven Integration
- Docker
- Docker Pipeline
- SSH
- SonarQube Scanner
- JDK Tool
- AWS Java SDK Secrets Manager
- AWS Java SDK ECS
- Amazon ECR

![](PRA05_ANSWER/screenshots/4-0-basic-public-installation.png)

#### 4.1 Configure tools
JDK

![](PRA05_ANSWER/screenshots/4-3-jdk-tool.png)

Git

![](PRA05_ANSWER/screenshots/4-4-git-tool.png)

Maven

![](PRA05_ANSWER/screenshots/4-5-maven-tool.png)

#### 5. Create a Pipeline for Spring Boot Project

Create a new pipeline job in Jenkins and use this script:
Create a new pipeline job in Jenkins:

```groovy
pipeline {
agent any

environment {
DOCKERHUB_CREDENTIALS = credentials('dockerhub_id')
IMAGE_NAME = 'marcius02/bookspageable'
IMAGE_TAG = "${BUILD_NUMBER}"
}


tools {
maven 'Maven'
jdk 'JDK'
maven 'M3'
jdk 'JDK21'
}

stages {
stage('Checkout') {
steps {
git 'https://github.com/yourusername/PageableBooks.git'
git 'https://github.com/AlbertProfe/BooksPageable.git'
}
}

Expand All @@ -80,64 +172,66 @@ pipeline {
}
}

stage('Docker Build') {
stage('Archive') {
steps {
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
}
}

stage('Build Docker Image') {
steps {
script {
docker.build("pageablebooks:${env.BUILD_ID}")
// Build the Docker image and tag it with both BUILD_NUMBER and latest
sh "docker build -t ${IMAGE_NAME}:${IMAGE_TAG} -t ${IMAGE_NAME}:latest ."
}
}
}

stage('Docker Run') {
stage('Push to DockerHub') {
steps {
script {
docker.image("pageablebooks:${env.BUILD_ID}").run('-p 8080:8080')
// Login to DockerHub
sh "echo ${DOCKERHUB_CREDENTIALS_PSW} | docker login -u ${DOCKERHUB_CREDENTIALS_USR} --password-stdin"

// Push both the specific tag and the latest tag
sh "docker push ${IMAGE_NAME}:${IMAGE_TAG}"
sh "docker push ${IMAGE_NAME}:latest"
}
}
}
}
}

post {
always {
// Logout from DockerHub and remove images
sh "docker logout"
sh "docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || true"
sh "docker rmi ${IMAGE_NAME}:latest || true"
}
}
}
```

Ensure your Spring Boot project (PageableBooks) has a Dockerfile in its root directory:

```dockerfile
FROM openjdk:11-jre-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
```

This pipeline will checkout your code, build the Spring Boot application, create a Docker image, and run it[1][2][3][4][5].
This Jenkins pipeline automates the process of building a Docker image for a Java project, pushing the image to DockerHub, and cleaning up afterward.

### Additional Notes
Docker Hub
![](PRA05_ANSWER/screenshots/5-0-dockerhub.png)

- Ensure Docker is installed on the Jenkins server.
- Configure Docker permissions for the Jenkins user.
- Adjust the pipeline script according to your project structure and requirements.
Status
![](PRA05_ANSWER/screenshots/5-1-status.png)

### Submission Guidelines
Timings
![](PRA05_ANSWER/screenshots/5-2-timings.png)

- Fork the repository or create a new one on GitHub.
- Create a branch named `MF05-PRA05-YourNameAndSurname`.
- Implement the required changes and add the Jenkins pipeline script.
- Commit with clear messages.
- Push your branch and create a pull request titled `MF05-PRA05-YourNameAndSurname-JenkinsPipeline`.
- Create a `PRA05_ANSWER` folder to save the answer, docs, and images.
Build Data
![](PRA05_ANSWER/screenshots/5-3-build-data.png)

### Evaluation Criteria
Pipe Overview
![](PRA05_ANSWER/screenshots/5-4-pipe-overview.png)

- Successful setup of Jenkins in Docker.
- Correct installation and configuration of required plugins.
- Proper implementation of the Jenkins pipeline for the Spring Boot project.
- Successful Docker integration within the pipeline.
- Clear documentation and commit messages.
Pipe console
![](PRA05_ANSWER/screenshots/5-5-pipe-console.png)

Citations:
[1] https://dev.to/hackmamba/jenkins-in-docker-running-docker-in-a-jenkins-container-1je?comments_sort=top
[2] https://www.red-gate.com/simple-talk/devops/containers-and-virtualization/deploying-a-dockerized-application-to-the-kubernetes-cluster-using-jenkins/
[3] https://devopscube.com/docker-containers-as-build-slaves-jenkins/
[4] https://www.youtube.com/watch?v=RKaxATb2kz8
[5] https://www.jenkins.io/doc/book/installing/docker/
[6] https://github.com/jenkinsci/docker
[7] https://phoenixnap.com/kb/how-to-configure-docker-in-jenkins
Workspace
![](PRA05_ANSWER/screenshots/5-6-workspace.png)
Loading