Skip to content

Add NGINX - pipeline.groovy #109

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

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2982ad3
Add NGINX - pipeline.groovy
OnurOzcelikSE Sep 4, 2023
f1941c9
Update Docker Hub username
OnurOzcelikSE Sep 4, 2023
91b792a
Update NGINX - pipeline.groovy
OnurOzcelikSE Sep 7, 2023
61c0c78
Update NGINX - pipeline.groovy
OnurOzcelikSE Sep 7, 2023
6551f4f
Update NGINX - pipeline.groovy
OnurOzcelikSE Sep 7, 2023
3ac35d5
Update NGINX - pipeline.groovy
OnurOzcelikSE Sep 7, 2023
832bf4c
Update nginx_pipeline.groovy
OnurOzcelikSE Sep 7, 2023
a041c71
Update NGINX - pipeline.groovy
OnurOzcelikSE Sep 7, 2023
d1bc62c
Update nginx_pipeline.groovy
OnurOzcelikSE Sep 7, 2023
e246cae
Update repository name
OnurOzcelikSE Sep 7, 2023
2f1f2dc
Update repository name
OnurOzcelikSE Sep 7, 2023
ed14dc2
Update repository name
OnurOzcelikSE Sep 7, 2023
b45dea1
Add Create Pipeline
OnurOzcelikSE Sep 7, 2023
5611631
Update nginx_pipeline.groovy
OnurOzcelikSE Sep 7, 2023
a8d3d1b
Update Trigger Stage
OnurOzcelikSE Sep 7, 2023
aee983c
Update Trigger Stage
OnurOzcelikSE Sep 7, 2023
01977b6
Update Trigger Stage
OnurOzcelikSE Sep 7, 2023
fdbd1a4
Delete second pipeline
OnurOzcelikSE Sep 7, 2023
b2bc3fd
Add Docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
d6b4213
Add Docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
2b9a211
Update nginx_pipeline
OnurOzcelikSE Sep 7, 2023
0bdfe40
Add properties
OnurOzcelikSE Sep 7, 2023
a9539e4
add options
OnurOzcelikSE Sep 7, 2023
74f498f
delete options
OnurOzcelikSE Sep 7, 2023
8b8d51e
Update docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
8a1f331
Update docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
003fa1c
Update docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
f603712
add def
OnurOzcelikSE Sep 7, 2023
8b2e5c5
change line
OnurOzcelikSE Sep 7, 2023
44614b0
Update docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
3378331
add env
OnurOzcelikSE Sep 7, 2023
0f78bef
add quotes
OnurOzcelikSE Sep 7, 2023
a073b67
add \"
OnurOzcelikSE Sep 7, 2023
7987297
Update docker-dive-pipeline.groovy
OnurOzcelikSE Sep 7, 2023
4ce0b05
Update parameters to fail
OnurOzcelikSE Sep 8, 2023
73183bf
Update parameter values back to default and print the values
OnurOzcelikSE Sep 8, 2023
cd1f2b4
Update Stage Order
OnurOzcelikSE Sep 8, 2023
5f61ac7
Update syntax
OnurOzcelikSE Sep 8, 2023
3f98d96
Update variables
OnurOzcelikSE Sep 8, 2023
7154e96
Update merged stages
OnurOzcelikSE Sep 8, 2023
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
67 changes: 67 additions & 0 deletions docker-dive-pipeline.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
pipeline {
agent any

environment {
DOCKER_REPOSITORY = 'onurozcelikse/nginx-demos'
LOWEST_EFFICIENCY = '0.95'
HIGHEST_USER_WASTED_PERCENT = '0.20'
HIGHEST_WASTED_BYTES = '20MB'
}

parameters {
string(name: 'BUILD_NUMBER', description: 'Build number from the previous pipeline')
}


stages {
stage('Pull Docker Image') {
steps {
script {
// Get the BUILD_NUMBER parameter
def buildNumber = params.BUILD_NUMBER

// Define the tagged image
def dockerTaggedImage = "${DOCKER_REPOSITORY}:${buildNumber}"

// Pull the Docker image
sh "docker pull ${dockerTaggedImage}"
}
}
}

stage('Analyze Tagged Docker Image with Dive') {
steps {
script {
// Get the BUILD_NUMBER parameter
def buildNumber = params.BUILD_NUMBER

// Define the tagged image
def dockerTaggedImage = "${DOCKER_REPOSITORY}:${buildNumber}"
// Define the Dive command with parameters
def diveCommand = "dive --ci --lowestEfficiency \"${env.LOWEST_EFFICIENCY}\" --highestUserWastedPercent \"${env.HIGHEST_USER_WASTED_PERCENT}\" --highestWastedBytes \"${env.HIGHEST_WASTED_BYTES}\" ${dockerTaggedImage}"

// Execute Dive and capture the status
def status = sh(script: diveCommand, returnStatus: true)

if (status == 0) {
echo "Dive analysis completed successfully for ${dockerTaggedImage}."
} else {
error("Dive found inefficiencies in the ${dockerTaggedImage} Docker image.")
}

// Execute Dive and capture the output
def diveOutput = sh(script: diveCommand, returnStdout: true).trim()

// Parse the Dive output to extract and print the desired values
def efficiency = (diveOutput =~ /efficiency:\s+([\d.]+ %)/)[0][1]
def userWastedPercent = (diveOutput =~ /userWastedPercent:\s+([\d.]+ %)/)[0][1]
def wastedBytes = (diveOutput =~ /wastedBytes:\s+([\d,]+ bytes)/)[0][1]

echo "Lowest Efficiency: ${efficiency}"
echo "Highest User Wasted Percent: ${userWastedPercent}"
echo "Highest Wasted Bytes: ${wastedBytes}"
}
}
}
}
}
72 changes: 72 additions & 0 deletions nginx_pipeline.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
pipeline {
agent any

environment {
DOCKER_REPOSITORY = 'onurozcelikse/nginx-demos'
}

stages {
stage('Clone Repository') {
steps {
// Checkout the Git repository
checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[url: 'https://github.com/OnurOzcelikSE/NGINX-Demos.git']]])
echo 'Repository cloned'
}
}
stage('Build Docker Image') {
steps {
script {
// Define the Docker image tag
def dockerImageTag = "${DOCKER_REPOSITORY}:${env.BUILD_NUMBER}"

// Use withCredentials to securely pass Docker Hub credentials
withCredentials([usernamePassword(credentialsId: 'DOCKER_HUB_CREDENTIALS', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
echo 'Logged in to DockerHub securely'

// Use --password-stdin to securely pass the password to docker login
sh """
echo \${DOCKER_PASSWORD} | docker login --username \${DOCKER_USERNAME} --password-stdin docker.io
docker build -t ${dockerImageTag} ./nginx-hello
"""
echo 'Docker image built'
}
}
}
}

stage('Push Docker Image') {
steps {
script {
// Define the Docker image tag again
def dockerImageTag = "${DOCKER_REPOSITORY}:${env.BUILD_NUMBER}"

// Push the Docker image
sh "docker push ${dockerImageTag}"
echo 'Docker image pushed'
}
}
}

stage('Clean Up') {
steps {
script {
// Define the image name for cleanup
def imageName = "${DOCKER_REPOSITORY}:${env.BUILD_NUMBER}"

// Remove the Docker image
sh "docker rmi ${imageName}"
echo 'Cleanup completed'
}
}
}

stage('Trigger Next Pipeline') {
steps {
script {
def buildNumberString = env.BUILD_NUMBER.toString()
build job: 'docker-dive-pipeline', parameters: [string(name: 'BUILD_NUMBER', value: buildNumberString)]
}
}
}
}
}