Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jenkins shared lib #48

Merged
merged 9 commits into from
Jun 10, 2024
Merged
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
51 changes: 51 additions & 0 deletions jenkins-shared-lib/vars/BuildDeployDocker.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
def call(Map config) {
pipeline {
agent any

environment {
DOCKER_CREDENTIALS = credentials("${config.dockerCredentialsId ?: 'dockerhub-halkeye'}")
DOCKER_REGISTRY = "${config.dockerRepoUrl ?: ''}"
IMAGE_NAME = "${config.imageName}"
IMAGE_TAG = "${config.imageTag ?: 'latest'}"
DOCKERFILE = "${config.dockerfile ?: 'Dockerfile'}"
}
stages {
stage('Checkout') {
steps {
script {
checkout scm: [$class: 'GitSCM', branches: [[name: "*/${config.branch ?: 'main'}"]], userRemoteConfigs: [[url: config.repoUrl]]]
}
}
}
stage('Build') {
steps {
script {
sh(config.buildCommand ?: 'mvn clean install')
}
}
}
stage('Build Docker Image') {
steps {
script {
sh """
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} -f ${DOCKERFILE} .
"""
}
}
}
stage('Push Docker Image') {
steps {
script {
withCredentials([usernamePassword(credentialsId: config.dockerCredentialsId, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh """
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
docker push ${DOCKER_REGISTRY}
"""
}
}
}
}
}
}
}
62 changes: 62 additions & 0 deletions jenkins-shared-lib/vars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Jenkins Shared Library

This directory contains reusable Groovy scripts for Jenkins pipelines. These scripts can be used to perform common tasks such as checking out Git repositories, building code, building Docker images and pushing Docker images.

## Script
`BuildDeployDocker.groovy`
This script performs the following tasks:

Checks out a Git repository.
Builds the code using a specified build command.
Builds a Docker image using a specified Dockerfile, image name, and image tag.
Pushes the Docker image to a specified Docker repositor

### Parameters
`repoUrl`: The URL of the Git repository.
`branch`: The branch to checkout (default is 'main').
`buildCommand`: The command to build the code (default is 'mvn clean install').
`dockerfile`: The path to the Dockerfile (default is 'Dockerfile').
`imageName`: The name of the Docker image.
`imageTag`: The tag for the Docker image (default is 'latest').
`dockerRepoUrl`: The URL of the Docker repository.
`dockerCredentialsId`: The ID of the Docker credentials in Jenkins.


##### Example Jenkins Pipeline
Here is an example of how to use these scripts in a Jenkins pipeline where I used a public repo which contains a Spring Boot based Java web application:

qburst-praven marked this conversation as resolved.
Show resolved Hide resolved
```groovy
@Library('my-shared-library') _

def config = [
repoUrl: 'https://github.com/Rithin-QB/SpringBoot-Web-App.git',
branch: 'main',
buildCommand: 'mvn clean install',
dockerfile: 'Dockerfile',
imageName: 'sample',
imageTag: 'latest',
dockerRepoUrl: 'rithin730/sample',
dockerCredentialsId: 'Dockerhub_cred'
]

BuildDeployDocker(config)
```


### Common Issues
#### Docker Permission Denied
If you encounter a permission denied error while trying to connect to the Docker daemon, ensure that the Jenkins user has the appropriate permissions. You may need to add the Jenkins user to the docker group:

```sh
sudo usermod -aG docker jenkins
```

#### Docker Hub Authentication
If you receive a denied: requested access to the resource is denied error when pushing a Docker image, ensure that your Jenkins instance is properly authenticated with Docker Hub. This can be done by configuring Docker credentials in Jenkins:

Go to Jenkins Dashboard.
Navigate to Credentials > System > Global credentials.
Add new credentials with your Docker Hub username and password.
Use these credentials in your pipeline to login to Docker Hub before pushing the image: