This document describes how to use Docker in Continuous Integration.
Prerequisites
Before configuring the CODING Continuous Integration (CODING-CI) build environment, you must activate the CODING DevOps service for your Tencent Cloud account.
Open Project
1. Log in to the CODING Console and click the team domain name to go to CODING.
2. Click in the upper-right corner to open the project list page and click a project icon to open the project. 3. Select Continuous Integration from the menu on the left.
Overview
Besides using Docker as a build environment for Continuous Integration, you may need to run additional services in Docker as test dependencies, or build a Docker image in a CI process and push it to the relevant repository.
Run Specific Docker Image and Execute Commands
In a build process, you may need to use a public Docker image repository. Refer to the following Jenkinsfile for the command to pull a specific Docker image.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
docker.image("ubuntu").inside('-e MY_ENV=123') {
sh 'echo ${MY_ENV}'
}
}
}
}
}
}
Run Docker Image of Specific Registry
In a build process, you may need to use a private Docker image repository. For example, you might need to use a Docker image repository that has been uploaded to the CODING Artifact Repository (CODING-AR). Refer to the following Jenkinsfile.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
docker.withRegistry('https://registry.example.com') {
docker.image('my-custom-image').inside {
sh 'make test'
}
}
}
}
}
}
}
Refer to the following Jenkinsfile in the case that a configured registry requires authentication to pull the image and needs a valid credential ID.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
docker.withRegistry('https://registry.example.com', 'my-credentials-id') {
}
}
}
}
}
}
Build Docker Image in CI Process
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('Build') {
steps {
script {
docker.build('my-docker-image:1.0.0')
}
}
}
}
}
If you need to specify additional parameters for a build, such as using a Dockerfile in a specific directory, refer to the following Jenkinsfile.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('Build') {
steps {
script {
docker.build('my-docker-image:1.0.0', '-f Dockerfile.build ./dockerfiles')
}
}
}
}
}
Push docker image to Specific Registry
Refer to the following Jenkinsfile.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]
])
}
}
stage('Build') {
steps {
script {
docker.build('my-docker-image:1.0.0')
docker.withRegistry('https://registry.example.com', 'my-credentials-id') {
docker.image('my-docker-image:1.0.0').push()
}
}
}
}
}
}
Use Docker to Run Additional Services as Test Dependencies
In the test process, you can use Docker to run MySQL and other services that can be used as test dependencies. Two containers are used in the following example: one as a MySQL service and the other as an execution environment. (Use a Docker link to link the two containers.)
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
docker.image('mysql:5').inside("--link ${c.id}:db") {
sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
}
}
}
}
}
}
}
Run Multiple Containers at the Same Time as Test Dependencies
If you need more than one additional service as test dependencies, you can run multiple services in a nested way.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c1 ->
docker.image('redis').withRun('') { c2 ->
sh 'docker ps'
}
}
}
}
}
}
}
References
For more information about Docker-based configuration in Jenkins, see the official Jenkins documentation:
Was this page helpful?