This document describes how to use Continuous Integration to release a project to a Docker server.
Before configuring the CODING Continuous Integration (CODING-CI) build environment, you must activate the CODING DevOps service for your Tencent Cloud account.
Log in to the server console and create an SSH key pair. After you obtain the private key pair, enter it in CODING's project token. Then, copy the public key content in id_rsa.pub to~/.ssh/authorized_keys
in the server.
Go to "Build Plan Settings" > "Process Configuration". Refer to the following configuration for filling.
pipeline {
agent any
stages {
stage('Check out') {
steps {
checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('Build') {
steps {
echo 'building...'
script {
// Modify dockerServer, dockerPath, and imageName
dockerServer = 'codes-farm-docker.pkg.coding.net'
dockerPath = '/laravel-demo/laravel-docker'
imageName = "${dockerServer}${dockerPath}/laravel-demo:1.0.0"
def customImage = docker.build(imageName)
// Push Docker image to repository
docker.withRegistry("https://${dockerServer}", CODING_ARTIFACTS_CREDENTIALS_ID) {
customImage.push()
}
}
}
}
stage('Deploy') {
steps {
echo 'deploying...'
script {
// Declare server information
def remote = [:]
remote.name = 'web-server'
remote.allowAnyHosts = true
remote.host = '106.54.86.239'
remote.port = 22
remote.user = 'ubuntu'
// In "CODING Credential Management" > "Credential ID", enter credentialsId. You do not need to modify id_rsa.
withCredentials([sshUserPrivateKey(credentialsId: "c4af855d-402a-4f38-9c83-f6226ae3441c", keyFileVariable: 'id_rsa')]) {
remote.identityFile = id_rsa
// Log in to the server via SSH and pull the Docker image
// Configure DOCKER_USER and DOCKER_PASSWORD in the environment variables of Continuous Integration
sshCommand remote: remote, sudo: true, command: "apt-get install -y gnupg2 pass"
sshCommand remote: remote, command: "docker login -u ${env.DOCKER_USER} -p ${env.DOCKER_PASSWORD} $DOCKER_SERVER"
sshCommand remote: remote, command: "docker pull ${imageName}"
sshCommand remote: remote, command: "docker stop web | true"
sshCommand remote: remote, command: "docker rm web | true"
sshCommand remote: remote, command: "docker run --name web -d ${imageName}"
}
}
}
}
}
}
The code for Docker Compose is slightly different:
sshCommand remote: remote, sudo: true, command: "apt-get install -y gnupg2 pass"
sshCommand remote: remote, command: "docker login -u ${env.DOCKER_USER} -p ${env.DOCKER_PASSWORD} $DOCKER_SERVER"
sshCommand remote: remote, sudo: true, command: "mkdir -p /var/www/site/"
sshCommand remote: remote, sudo: true, command: "chmod 777 /var/www/site/"
sshPut remote: remote, from: 'docker-compose.yml', into: '/var/www/site/'
sshCommand remote: remote, command: "cd /var/www/site/ && echo IMAGE=${imageName} > .env && echo APP_KEY=${env.APP_KEY} >> .env && echo DB_CONNECTION=sqlite >> .env"
sshCommand remote: remote, command: "cd /var/www/site/ && docker-compose down --remove-orphans"
sshCommand remote: remote, command: "cd /var/www/site/ && docker-compose up -d --no-build"
docker-compose.yml
code:
version: '2.1'
services:
web:
env_file: .env
build: .
image: ${IMAGE:-laravel-demo:dev}
ports:
- "80:80"
links:
- redis
redis:
image: "redis:5"
Was this page helpful?