我正在尝试使用jenkinsfile和docker-compose安装jenkins管道。 我的码头组成运行正常。但接下来的步骤(Jenkinsfile中的测试阶段)不运行。
如何告诉jenkins“确定docker容器没问题,你可以做下一件事”但是阻止docker容器停止(这就是为什么我把rails s放在最后命令“
这里是 docker-compose.yml :
version: '3'
services:
db-test:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=server_dev
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "${POSTGRES_PORT}:5432"
web-test:
image: starefossen/ruby-node
command: bash -c "cd /app && bundle install && rake db:migrate && rails s"
volumes:
- /home/xero/jenkins/jenkins_home/workspace/project-open-source:/app # Workspace
- /home/cache/bundle:/usr/local/bundle # Cache gemfiles
- /home/cache/node_modules:/app/node_modules # Cache yarn files
- /home/xero/.ssh:/root/.ssh # SSH keys (for git)
ports:
- "3000:3000"
depends_on:
- db-test
Jenkinsfile :
pipeline {
agent any
options {
timeout(time: 1, unit: 'DAYS')
disableConcurrentBuilds()
}
stages {
stage("Init") {
agent any
steps { initialize() }
}
stage("Test") {
agent any
steps { test() }
}
}
}
def initialize() {
sh 'docker-compose -f docker-compose-jenkins.yml up --build --abort-on-container-exit'
}
def test() {
sh 'docker exec -ti web-test sh -c "cd app/ && bundle exec rspec -f documentation"'
}
答案 0 :(得分:0)
这是我的解决方案。我使用重试和睡眠,等待码头工人容器启动。
#!groovy
def message = "";
def author = "";
def getLastCommitMessage = {
message = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
}
def getGitAuthor = {
def commit = sh(returnStdout: true, script: 'git rev-parse HEAD')
author = sh(returnStdout: true, script: "git --no-pager show -s --format='%an' ${commit}").trim()
}
pipeline {
agent any
options {
timeout(time: 1, unit: 'DAYS')
disableConcurrentBuilds()
}
stages {
stage("Init RoR and DB") {
agent any
steps { initialize() }
}
stage("Tests") {
agent any
steps { test() }
post {
success {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/var/jenkins_home/workspace/VPX-open-source/coverage/', reportFiles: 'index.html', reportName: 'RspecCoverage', reportTitles: ''])
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/var/jenkins_home/workspace/VPX-open-source/coverage/lcov-report', reportFiles: 'index.html', reportName: 'JestCoverage', reportTitles: ''])
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/var/jenkins_home/workspace/VPX-open-source/reports/', reportFiles: 'eslint.html', reportName: 'Eslint', reportTitles: ''])
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/var/jenkins_home/workspace/VPX-open-source/reports/', reportFiles: 'rubocop.html', reportName: 'Rubocop', reportTitles: ''])
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/var/jenkins_home/workspace/VPX-open-source/reports/rubycritic/', reportFiles: 'overview.html', reportName: 'Rubycritic', reportTitles: ''])
}
}
}
}
post {
failure {
script {
getLastCommitMessage()
getGitAuthor()
}
rocketSend channel: 'myproject-ci', emoji: ':x:', message: "Build failed - Commit : '${message}' by ${author}", rawMessage: true
}
}
}
def initialize() {
sh 'docker-compose -f docker-compose-jenkins.yml up --build --detach'
}
def test() {
try {
retry(3){
sleep 25
HEALTH = sh (
script: 'docker inspect -f \'{{json .State.Health.Status}}\' vpx-web-test',
returnStdout: true
).trim()
echo "${HEALTH}"
if(HEALTH == "starting"){
return true
}
}
sh 'docker exec vpx-web-test sh -c "cd app/ && RAILS_ENV=test bundle exec rspec -f documentation"'
sh 'docker exec vpx-web-test sh -c "cd app/ && yarn test"'
sh 'docker exec vpx-web-test sh -c "cd app/ && yarn test --coverage > reports/jest-coverage.html"'
sh 'docker exec vpx-web-test sh -c "cd app/ && yarn lint --f html reports/eslint.html ; exit 0"'
sh 'docker exec vpx-web-test sh -c "cd app/ && rubycritic app/ --no-browser -p reports/rubycritic"'
sh 'docker exec vpx-web-test sh -c "cd app/ && rubocop app/ --format html -o reports/rubocop.html --fail-level error"'
}
catch (exc) {
error("Build failed")
}
finally{
sh 'docker-compose -f docker-compose-jenkins.yml down'
}
}