簡體   English   中英

帶有RoR的Docker-compose和jenkinsfile

[英]Docker-compose and jenkinsfile with RoR

我正在嘗試使用jenkinsfile和docker-compose來安裝jenkins管道。 我的docker-compose運行良好。 但是接下來的步驟 (Jenkinsfile中的測試階段) 沒有運行

如何告訴詹金斯“ 好吧, Docker 容器很好,您可以做下一件事 ”,但要防止Docker容器停止運行(這就是為什么我將rails放在命令末尾的原因)

這是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"'
}

這是我的解決方案。 我使用重試和睡眠來等待dockers容器啟動。

#!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'
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM