Running Jenkins tests in Docker containers build from dockerfile in codebase

后端 未结 1 930
遥遥无期
遥遥无期 2021-02-20 18:42

I want to deploy a continuous integration platform based on Jenkins. As I have various kinds of projects (PHP / Symfony, node, angular, …) and as I want these tests to run both

相关标签:
1条回答
  • 2021-02-20 18:51

    A setup I suggest is Docker in Docker.

    The base is a derived Docker image, which extends the jenkins:2.x image by adding a Docker commandline client. The Jenkins is started as a container with its home folder (a folder e.g. /var/jenkins_home mounted from the Docker host) and the Docker socket file to be able to start Docker containers from Jenkins build jobs.

    docker run -d --name jenkins -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock ... <yourDerivedJenkinsImage>
    

    To check, if this setup is working just execute following command after starting the Jenkins container:

    docker exec jenkins docker version
    

    If the "docker version" output does NOT show:

    Is the docker daemon running on this host?

    Everythin is fine.

    In your build jobs, you could configure the process you mentioned above. Let Jenkins simply check out the repository. The repository should contain your build and test scripts.

    Use a freestyle build job with a shell execution. A shell execution could look like this:

    docker run --rm --volumes-from jenkins <yourImageToBuildAndTestTheProject> bash $WORKSPACE/<pathToYourProjectWithinTheGitRepository>/build.sh 
    

    This command simply starts a new container (to build and/or test your project) with the volumes from jenkins. Which means that the cloned repository will be available under $WORKSPACE. So if you run "bash $WORKSPACE/<pathToYourProjectWithinTheGitRepository>/build.sh" your project will be built within a container of "yourImageToBuildAndTestTheProject". After running this, you could start other containers for integration tests or combine this with "docker-compose" by installing it on the derived Jenkins image.

    Advantages are the minimal configuration affort you have within Jenkins - only the SCM configuration for cloning the GIT repository is required. Since each Jenkins job uses the Docker client directly you could use for each project one or Docker image to build and/or test, WITHOUT further Jenkins configuration.

    If you need additional configuration e.g. SSH keys or Maven settings, just put them on the Docker host and start the Jenkins container with the additional volumes, which contain those configuration files.

    Using this Docker option within the shell execution of your build jobs:

    --volumes-from jenkins
    

    Automatically adds workspace and configuration files to each of your build jobs.

    0 讨论(0)
提交回复
热议问题