Skip to content

Instantly share code, notes, and snippets.

@zkkmin
Created March 28, 2019 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkkmin/7ae635ae1e7056c4906c685ebfe06de5 to your computer and use it in GitHub Desktop.
Save zkkmin/7ae635ae1e7056c4906c685ebfe06de5 to your computer and use it in GitHub Desktop.
pipeline {
agent any
environment {
NETWORK_NAME = "nextgen-backend-testnet-${BRANCH_NAME.toLowerCase()}-b${BUILD_NUMBER}"
BUILD_NAME = "nextgen-test/${BRANCH_NAME.toLowerCase()}:B${BUILD_NUMBER}"
}
stages {
stage('Build') {
steps {
echo "Docker network name: ${NETWORK_NAME}"
echo "Docker build name: ${BUILD_NAME}"
sh "docker network create ${NETWORK_NAME}"
sh "docker build -t '${BUILD_NAME}' -f Dockerfile.test ."
sh "docker-compose -f docker-compose-jenkins.yml up -d"
sh "docker-compose -f docker-compose-jenkins.yml logs -f -t flyway"
script {
FLYWAY_RESULT = sh (
script: 'docker-compose -f docker-compose-jenkins.yml logs -f --tail=0 flyway | grep "exited with code 0"',
returnStatus: true
)
if ("${FLYWAY_RESULT}" != "0") {
error "Flyway migrate failed"
}
}
}
}
stage('Test and Publish Test Reports') {
environment {
containerID = sh (
script: "docker run --network=${NETWORK_NAME} -t -d ${BUILD_NAME}",
returnStdout: true
).trim()
}
steps {
echo "Container ID is ==> ${containerID}"
catchError {
sh "docker exec ${containerID} python manage.py sync_roles --reset_user_permissions --settings config.settings.test"
sh "docker exec ${containerID} pytest --cov-config=.coveragerc --cov-report=xml:/app/report/cov.xml --cov --junitxml=/app/report/out.xml; exit 0"
sh "docker cp ${containerID}:/app/report/out.xml test_results.xml"
sh "docker cp ${containerID}:/app/report/cov.xml coverage.xml"
}
sh "docker stop ${containerID}"
sh "docker rm ${containerID}"
archiveArtifacts 'test_results.xml'
archiveArtifacts 'coverage.xml'
junit 'test_results.xml'
script {
if (currentBuild.result == 'FAILURE') {
error "Error running tests"
}
if (currentBuild.result == 'UNSTABLE') {
error "Error running tests"
}
}
}
}
stage('SonarQube') {
environment {
scannerHome = tool 'SonarQube'
}
steps {
withSonarQubeEnv('SonarQube') {
sh '${scannerHome}/bin/sonar-scanner ' +
'-Dsonar.projectKey=nextgen-backend-PR ' +
'-Dsonar.projectName=nextgen-backend-PR ' +
'-Dsonar.projectVersion=${GIT_BRANCH}:B${BUILD_NUMBER} ' +
'-Dsonar.python.coverage.reportPaths=coverage.xml ' +
'-Dsonar.python.xunit.reportPath=test_results.xml'
}
timeout(time: 1, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
}
post {
cleanup {
sh "docker-compose -f docker-compose-jenkins.yml down"
sh "docker network rm ${NETWORK_NAME}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment