I want to run all tests written in JUnit and Integration tests through Jenkins as CI/CD process in my Spring boot.
I created grrovy file, dockerfile and docker compose yml for Jenkins.
Here is the groovy file show below
import hudson.plugins.git.UserRemoteConfig
import hudson.plugins.git.BranchSpec
import hudson.plugins.git.GitSCM
import jenkins.model.*
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition
def instance = Jenkins.getInstance()
def jobName = "flightsearchapi"
def job = instance.getItem(jobName)
if (job != null) {
job.delete()
}
def pipelineJob = instance.createProject(org.jenkinsci.plugins.workflow.job.WorkflowJob, jobName)
def definition = new CpsScmFlowDefinition(
new GitSCM(
[
new UserRemoteConfig("https://github.com/Rapter1990/flightsearchapi.git", null, null, null)
],
[new BranchSpec("*/development/issue-2/implement-jenkins-for-ci-cd")],
false, Collections.emptyList(),
null, null, Collections.emptyList()
),
"Jenkinsfile"
)
definition.setLightweight(true)
pipelineJob.setDefinition(definition)
pipelineJob.save()
println("Pipeline job '${jobName}' has been successfully created!")
Here is the dockerfile shown below
FROM jenkins/jenkins:lts
# Plugin list
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt
# For Groovy scripts, init.d directory
COPY init.groovy.d/ /var/jenkins_home/init.groovy.d/
# Install Docker CLI
USER root
RUN apt-get update && apt-get install -y docker.io
Here is the docker-compose yml file
version: '3.9'
services:
jenkins:
build:
context: .
dockerfile: Dockerfile
container_name: jenkins-server
ports:
- "8080:8080" # Expose Jenkins UI on port 8080
- "50000:50000" # Expose port for Jenkins agents
volumes:
- jenkins_home:/var/jenkins_home # Persistent Jenkins data
- /var/run/docker.sock:/var/run/docker.sock # Mount Docker socket for Docker builds
- ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional)
- ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional)
environment:
JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional)
user: root # Run as root to allow installing dependencies
volumes:
jenkins_home:
When I run docker-compose through docker-compose up -d
and run pipeline , I got the error shown below
12:47:17.631 [main] INFO org.testcontainers.DockerClientFactory -- Docker host IP address is 172.17.0.1
12:47:17.679 [main] INFO org.testcontainers.DockerClientFactory -- Connected to docker:
Server Version: 20.10.21
API Version: 1.41
Operating System: Docker Desktop
Total Memory: 12678 MB
12:47:17.790 [main] INFO tc.testcontainers/ryuk:0.11.0 -- Creating container for image: testcontainers/ryuk:0.11.0
12:47:17.814 [main] INFO org.testcontainers.utility.RegistryAuthLocator -- Failure when attempting to lookup auth config. Please ignore if you don't have images in an authenticated registry. Details: (dockerImageName: testcontainers/ryuk:0.11.0, configFile: /root/.docker/config.json, configEnv: DOCKER_AUTH_CONFIG). Falling back to docker-java default behaviour. Exception message: Status 404: No config supplied. Checked in order: /root/.docker/config.json (file not found), DOCKER_AUTH_CONFIG (not set)
12:47:18.097 [main] INFO tc.testcontainers/ryuk:0.11.0 -- Container testcontainers/ryuk:0.11.0 is starting: a821037942a489255c5ee014be369aecbeb7793ff5b60a9b7189497e103051cd
12:47:18.906 [main] INFO tc.testcontainers/ryuk:0.11.0 -- Container testcontainers/ryuk:0.11.0 started in PT1.1156687S
12:47:18.917 [testcontainers-ryuk] WARN org.testcontainers.utility.RyukResourceReaper -- Can not connect to Ryuk at 172.17.0.1:30511
java.net.ConnectException: Connection refused
It works with Linux but it couldn't work on Windows.
How can I fix the issue?
Here is the solution
The issue disappeared after changing /var/run/docker.sock:/var/run/docker.sock
to /var/run/docker.sock.raw:/var/run/docker.sock
Here is the code block shown below
volumes:
- jenkins_home:/var/jenkins_home # Persistent Jenkins data
- /var/run/docker.sock.raw:/var/run/docker.sock # Mount Docker socket for Docker builds