Skip to content

Commit b571478

Browse files
committed
Task 17 : Implement Jenkins Process
1 parent 091e258 commit b571478

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

Jenkinsfile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
pipeline {
2+
agent any
3+
4+
environment {
5+
GIT_REPO_URL = 'https://github.com/Rapter1990/cryptoexchangeapi.git'
6+
BRANCH_NAME = 'main'
7+
DOCKERHUB_USERNAME = 'noyandocker'
8+
DOCKER_IMAGE_NAME = 'cryptoexchangeapi-jenkins'
9+
}
10+
11+
stages {
12+
stage('Checkout') {
13+
steps {
14+
script {
15+
checkout([
16+
$class: 'GitSCM',
17+
branches: [[name: "*/${env.BRANCH_NAME}"]],
18+
userRemoteConfigs: [[url: "${env.GIT_REPO_URL}"]]
19+
])
20+
}
21+
}
22+
}
23+
24+
stage('Build') {
25+
agent {
26+
docker {
27+
image 'maven:3.9.9-amazoncorretto-21-alpine'
28+
}
29+
}
30+
steps {
31+
sh 'mvn clean install'
32+
}
33+
}
34+
35+
stage('Build Docker Image') {
36+
agent {
37+
docker {
38+
image 'docker:27.5.1'
39+
}
40+
}
41+
steps {
42+
sh "docker build -t ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest ."
43+
}
44+
}
45+
46+
stage('Push Docker Image') {
47+
agent {
48+
docker {
49+
image 'docker:27.5.1'
50+
}
51+
}
52+
steps {
53+
withDockerRegistry([credentialsId: 'docker-hub-credentials', url: '']) {
54+
sh "docker push ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest"
55+
}
56+
}
57+
}
58+
59+
}
60+
61+
post {
62+
always {
63+
cleanWs(cleanWhenNotBuilt: false,
64+
deleteDirs: true,
65+
disableDeferredWipeout: true,
66+
notFailBuild: true,
67+
patterns: [[pattern: '.gitignore', type: 'INCLUDE'],
68+
[pattern: '.propsfile', type: 'EXCLUDE']])
69+
}
70+
}
71+
}

jenkins/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM jenkins/jenkins:lts
2+
3+
# Plugin list
4+
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
5+
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt
6+
7+
# For Groovy scripts, init.d directory
8+
COPY init.groovy.d/ /var/jenkins_home/init.groovy.d/
9+
10+
# Install Docker CLI
11+
USER root
12+
RUN apt-get update && apt-get install -y docker.io

jenkins/docker-compose.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
services:
2+
jenkins:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: jenkins-server
7+
ports:
8+
- "8080:8080" # Expose Jenkins UI on port 8080
9+
- "50000:50000" # Expose port for Jenkins agents
10+
volumes:
11+
- jenkins_home:/var/jenkins_home # Persistent Jenkins data
12+
- /var/run/docker.sock.raw:/var/run/docker.sock # Mount Docker socket for Docker builds
13+
- ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional)
14+
- ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional)
15+
environment:
16+
JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional)
17+
user: root # Run as root to allow installing dependencies
18+
networks:
19+
- common-network
20+
21+
volumes:
22+
jenkins_home:
23+
24+
networks:
25+
common-network:
26+
driver: bridge
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import hudson.plugins.git.BranchSpec
2+
import hudson.plugins.git.GitSCM
3+
import hudson.plugins.git.UserRemoteConfig
4+
import jenkins.model.*
5+
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition
6+
7+
def instance = Jenkins.getInstance()
8+
9+
def jobName = "cryptoexchangeapi"
10+
def job = instance.getItem(jobName)
11+
12+
if (job != null) {
13+
job.delete()
14+
}
15+
16+
def pipelineJob = instance.createProject(org.jenkinsci.plugins.workflow.job.WorkflowJob, jobName)
17+
def definition = new CpsScmFlowDefinition(
18+
new GitSCM(
19+
[
20+
new UserRemoteConfig("https://github.com/Rapter1990/cryptoexchangeapi.git", null, null, null)
21+
],
22+
[new BranchSpec("*/main")],
23+
false, Collections.emptyList(),
24+
null, null, Collections.emptyList()
25+
),
26+
"Jenkinsfile"
27+
)
28+
definition.setLightweight(true)
29+
pipelineJob.setDefinition(definition)
30+
pipelineJob.save()
31+
32+
println("Pipeline job '${jobName}' has been successfully created!")

jenkins/plugins.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
workflow-aggregator
2+
git
3+
job-dsl
4+
ws-cleanup
5+
docker-plugin
6+
docker-workflow
7+
docker-commons

0 commit comments

Comments
 (0)