https://github.com/codethebuild
Created by Niek Palm (2016)
Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries - anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.
Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.
slides: image: nginx environment: VIRTUAL_HOST: 'nextbuild.docker' VIRTUAL_PORT: 80 volumes: - .:/usr/share/nginx/html ports: - "8889:80"
docker-compose.yml
gitlab: image: 'gitlab/gitlab-ce:latest' hostname: 'gitlab.docker' ports: - '81:8080' volumes: - '/opt/gitlab/config:/etc/gitlab' - '/opt/gitlab/logs:/var/log/gitlab' - '/opt/gitlab/data:/var/opt/gitlab'
jenkins: image: 'jenkins:1.642.3' links: - gitlab:gitlab.docker ports: - '82:8080' volumes: - '/opt/jenkins/jenkins_home:/var/jenkins_home'
OKAY now we have GIT and Jenkins running. But we still need to install some magick: the jenkins plugins.
MAINTAINER Niek Palm dev.npalm@gmail.com FROM jenkins:1.642.3 COPY plugins.txt /usr/share/jenkins/plugins.txt RUN /usr/local/bin/plugins.sh /usr/share/jenkins/plugins.txt
workflow-support:1.15 workflow-multibranch:1.15 workflow-step-api:1.15 git:2.4.4 workflow-basic-steps:1.15 workflow-api:1.15 workflow-scm-step:1.15 pipeline-rest-api:1.0 pipeline-stage-view:1.0
Update docker-compose.yml
gitlab: image: 'gitlab/gitlab-ce:latest' ... jenkins: build: 'jenkins' links: - gitlab:gitlab.docker ports: - '82:8080' volumes: - '/opt/jenkins/jenkins_home:/var/jenkins_home'
The sample application is a Java based Spring Boot service witch uses a mongo db (data store).
The build is scripted in gradle. We can build by running:
gradlew <tasks>
Jenkinsfile
node { git url: 'http://gitlab.docker/nextbuild/service.git' stage 'Clean' sh './gradlew clean' stage 'Build' sh './gradlew assemble' stage 'Test' sh './gradlew check' }
artifactory: image: 'jfrog-docker-reg2.bintray.io/jfrog/artifactory-oss' ports: - '87:8081' volumes: - '/opt/artifactory/data:/var/opt/jfrog/artifactory/data' - '/opt/artifactory/logs:/var/opt/jfrog/artifactory/logs' - '/opt/artifactory/backup:/var/opt/jfrog/artifactory/backup'
sonardb: image: postgres:9 environment: POSTGRES_USER: 'sonar' POSTGRES_PASSWORD: 'secret' sonar: image: sonarqube:5.4 environment: SONARQUBE_JDBC_USERNAME: 'sonar' SONARQUBE_JDBC_PASSWORD: 'secret' SONARQUBE_JDBC_URL: 'jdbc:postgresql://sonardb.docker/sonar' links: - sonardb:sonardb.docker - gitlab:gitlab.docker ports: - "9000:9000" jenkins: ... links: ... - sonardb:sonardb.docker ...
node { git url: 'http://gitlab.docker/nextbuild/service.git' ... stage 'QA' sh './gradlew sonarRunner' }
COOL now we can code the build as part of our sources. But what about the infrastructure we are using for the build? Can we create a stable and reproducable infrastructure for the build as well?
.gitlab-ci.yml snippet
stages: - build assemble: stage: build script: - ./gradlew clean assemble
We have GitLab CI already running since we use GitLab as our GIT server. So, we only need to create a runner.
gitlabrunner: image: 'gitlab/gitlab-runner:latest' environment: REGISTRATION_TOKEN: 'JCqWxz4LcNmth4F4PdTo' CI_SERVER_URL: 'http://gitlab.docker/ci' ...
> docker exec -i -t gitlab-runner-dind1 gitlab-runner register -n \ --docker-links 'gitlab_gitlab_1:gitlab.docker'
.gitlab-ci.yml for our service
stages: - build - test - qa assemble: stage: build image: npalm/java:oracle-java8 script: - ./gradlew clean distTar test: stage: test image: npalm/java:oracle-java8 script: - ./gradlew check
.gitlab-ci.yml for our service
stages: - build - test - qa assemble: ... test: ... sonar: stage: qa image: npalm/java:oracle-java8 script: - ./gradlew sonarRunner
mongo: image: mongo:3.2 service: build: ./build/docker ports: - "8888:8080" links: - mongo:mongodb
.gitlab-ci.yml
stages: - build - test - qa ... performance_test: stage: test image: npalm/dind-java:latest script: - ./gradlew assemble copyDockerfiles - docker-compose build - docker-compose up -d - ./wait.sh service_service_1 service 8888 - ./gradlew jmeterRun ...
All sources are available on GitHub