Jenkinsfile 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!groovy
node (label: "master"){
    def ENV_PATH = "$HOME/venv/faraday"
    echo "${ENV_PATH}"

    stage("Clean virtualenv") {
        sh "rm -rf ${ENV_PATH}"
    }

    stage("Install Python Virtual Enviroment") {
        sh "/usr/local/bin/virtualenv --no-site-packages ${ENV_PATH} --python=/usr/local/bin/python"
    }

    // Get the latest version of our application code.
    stage ("Pull Code from SCM") {
        checkout scm
    }

    stage ("Install Application Dependencies") {
        sh """
            source ${ENV_PATH}/bin/activate
22
            pip install virtualenv responses pytest-xdist
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            pip install -U -r $WORKSPACE/requirements.txt
            pip install -U -r $WORKSPACE/requirements_server.txt
            pip install -U -r $WORKSPACE/requirements_extras.txt
            pip install -U -r $WORKSPACE/requirements_dev.txt
            deactivate
           """
    }

    stage ("Check code style") {
        sh """
            sloccount --duplicates --wide --details $WORKSPACE   | fgrep -v .git > $WORKSPACE/sloccount.sc || :
            find $WORKSPACE -name \\"*.py\\" | egrep -v '^./tests/'  | xargs pyflakes  > $WORKSPACE/pyflakes.log || :
            find $WORKSPACE -name \\"*.py\\" | egrep -v '^./tests/'  | xargs pylint --output-format=parseable --reports=y > $WORKSPACE/pylint.log || :
            eslint -c /home/faraday/.eslintrc.js -f checkstyle $WORKSPACE/server/www/scripts/**/* > eslint.xml || true

           """
           warnings canComputeNew: false, canResolveRelativePaths: false, consoleParsers: [[parserName: 'PyFlakes']], defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', messagesPattern: '', parserConfigurations: [[parserName: 'AcuCobol Compiler', pattern: 'pyflakes.log']], unHealthy: ''

    }

    stage ("Run Unit/Integration Tests") {
        def testsError = null
        try {
            sh """
                source ${ENV_PATH}/bin/activate
                cd $WORKSPACE && pytest -v --junitxml=$WORKSPACE/xunit.xml || :
                deactivate
               """
               step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/coverage.xml', failNoReports: false, failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])
        }
        catch(err) {
            testsError = err
            currentBuild.result = 'FAILURE'
        }
        finally {
            junit "**/xunit.xml"
            notifyBuild(currentBuild.result, "SQLite Build")
            if (testsError) {
                throw testsError
            }

        }
    }

    stage ("Run Unit/Integration Tests (with PostgreSQL)") {
        def testsError = null
        try {
            withCredentials([string(credentialsId: 'postgresql_connection_string', variable: 'CONN_STRING')]) {
                sh """
                    source ${ENV_PATH}/bin/activate
73
                    cd $WORKSPACE && pytest -v --junitxml=$WORKSPACE/xunit-postgres.xml --connection-string "$CONN_STRING" -n 15 || :
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                    deactivate
                """
                step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/coverage.xml', failNoReports: false, failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])
            }
        }
        catch(err) {
            testsError = err
            currentBuild.result = 'FAILURE'
        }
        finally {
            junit "**/xunit-postgres.xml"
            notifyBuild(currentBuild.result, "PostgreSQL Build")
            if (testsError) {
                throw testsError
            }

        }
    }

    stage ("Build docs") {
        sh """
            source ${ENV_PATH}/bin/activate
            pip install sphinx
            mkdir -p ~/docs
            rm -rf ~/docs/jenkins_build
            cd $WORKSPACE/doc && make html && cp -r _build/html ~/docs/jenkins_build
        """
    }

    stage ("Run Closure Compiler") {
        try {
            sh """
                java -jar /home/faraday/closure-compiler-v20180610.jar $WORKSPACE/server/www/scripts
            """
        }
        catch (err) {
            currentBuild.result = 'FAILURE'
        }
        finally {
            notifyBuild(currentBuild.result, "Closure compiler")
        }
    }
}

def notifyBuild(String buildStatus = 'STARTED', String extraMessage = '') {
  // build status of null means successful
  buildStatus =  buildStatus ?: 'SUCCESSFUL'

  // Default values
  def colorName = 'RED'
  def colorCode = '#FF0000'
  def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
  def summary = "${subject} (${env.BUILD_URL}) " + extraMessage

  // Override default values based on build status
  if (buildStatus == 'STARTED') {
    color = 'YELLOW'
    colorCode = '#FFFF00'
  } else if (buildStatus == 'SUCCESSFUL') {
    color = 'GREEN'
    colorCode = '#00FF00'
  } else {
    color = 'RED'
    colorCode = '#FF0000'
    summary = summary + ' @channel'
  }

  // Send notifications
  slackSend (color: colorCode, message: summary)
}