|
| 1 | +pipeline { |
| 2 | + agent { |
| 3 | + docker { |
| 4 | + image "docker:24.0.6-git" // Set the Docker image for the Jenkins agent |
| 5 | + } |
| 6 | + } |
| 7 | + environment { |
| 8 | + CODECOV_TOKEN = credentials('CODECOV_TOKEN') // Use the Codecov token from Jenkins secret |
| 9 | + CONTAINER_SUFFIX = "${BUILD_NUMBER}" // Use the build number as a container suffix for uniqueness |
| 10 | + DOCKER_NETWORK = "variantvalidator_docker_network-$CONTAINER_SUFFIX" // Create a unique Docker network for this build |
| 11 | + DATA_VOLUME = "docker-shared-space" // Define a data volume for shared data |
| 12 | + } |
| 13 | + stages { |
| 14 | + stage("Clone Repository Remove dangling docker components and Create Docker Network") { |
| 15 | + steps { |
| 16 | + checkout scm // Checkout the source code from the configured source code management system |
| 17 | + sh 'docker system prune --all --volumes --force' // Remove unused Docker resources |
| 18 | + sh 'docker network create $DOCKER_NETWORK' // Create a Docker network for containers |
| 19 | + } |
| 20 | + } |
| 21 | + stage("Switch to Git Branch") { |
| 22 | + steps { |
| 23 | + sh "git checkout ${BRANCH_NAME}" |
| 24 | + sh "git pull" |
| 25 | + } |
| 26 | + } |
| 27 | + stage("Build and Run VVTA PostgreSQL") { |
| 28 | + steps { |
| 29 | + script { |
| 30 | + def dockerfile = './db_dockerfiles/vvta/Dockerfile' // Define the Dockerfile path |
| 31 | + def vvtaContainer = docker.build("postgres-vvta-${CONTAINER_SUFFIX}", "--no-cache -f ${dockerfile} ./db_dockerfiles/vvta") |
| 32 | + // Build and run a PostgreSQL container for VVTA |
| 33 | + vvtaContainer.run("-p 5432:5432 -d --name vv-vvta --network $DOCKER_NETWORK --shm-size=2g") |
| 34 | + sh 'echo Building and running VVTA PostgreSQL' // Display a message |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + stage("Build and Run Validator MySQL") { |
| 39 | + steps { |
| 40 | + script { |
| 41 | + def dockerfile = './db_dockerfiles/vdb/Dockerfile' // Define the Dockerfile path |
| 42 | + def validatorContainer = docker.build("mysql-validator-${CONTAINER_SUFFIX}", "--no-cache -f ${dockerfile} ./db_dockerfiles/vdb") |
| 43 | + // Build and run a MySQL container for the Validator |
| 44 | + validatorContainer.run("-p 3306:3306 -d --name vv-vdb --network $DOCKER_NETWORK") |
| 45 | + sh 'echo Building and running Validator MySQL' // Display a message |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + stage("Build and Run SeqRepo") { |
| 50 | + steps { |
| 51 | + script { |
| 52 | + def dockerfile = './db_dockerfiles/vvsr/Dockerfile' // Define the Dockerfile path |
| 53 | + def seqRepoContainer = docker.build("sqlite-seqrepo-${CONTAINER_SUFFIX}", "--no-cache -f ${dockerfile} ./db_dockerfiles/vvsr") |
| 54 | + // Build and run a SQLite SeqRepo container |
| 55 | + seqRepoContainer.run("--network $DOCKER_NETWORK --name vv-seqrepo -v $DATA_VOLUME:/usr/local/share:rw") |
| 56 | + sh 'echo Building and running SeqRepo' // Display a message |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + stage("Build and Run VariantValidator") { |
| 61 | + steps { |
| 62 | + script { |
| 63 | + def dockerfile = './Dockerfile' // Define the Dockerfile path |
| 64 | + def variantValidatorContainer = docker.build("variantvalidator-${CONTAINER_SUFFIX}", "--no-cache -f ${dockerfile} .") |
| 65 | + |
| 66 | + // Run variantValidatorContainer and Mount the DATA_VOLUME |
| 67 | + variantValidatorContainer.run("-v $DATA_VOLUME:/usr/local/share:rw -d --name variantvalidator --network $DOCKER_NETWORK") |
| 68 | + |
| 69 | + // Display a message indicating that VariantValidator is being built and run |
| 70 | + sh 'echo Building and running VariantValidator' |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + stage("Run Pytest and Codecov") { |
| 75 | + steps { |
| 76 | + script { |
| 77 | + sh 'docker ps' // List running Docker containers |
| 78 | + def connectionSuccessful = false |
| 79 | + |
| 80 | + for (int attempt = 1; attempt <= 5; attempt++) { |
| 81 | + echo "Attempt $attempt to connect to the database..." |
| 82 | + def exitCode = sh(script: ''' |
| 83 | + docker exec -e PGPASSWORD=uta_admin variantvalidator psql -U uta_admin -d vvta -h vv-vvta -p 5432 |
| 84 | + ''', returnStatus: true) |
| 85 | + |
| 86 | + if (exitCode == 0) { |
| 87 | + connectionSuccessful = true |
| 88 | + echo "Connected successfully! Running pytest..." |
| 89 | + |
| 90 | + // Run pytest && Run Codecov with the provided token and branch name |
| 91 | + sh 'docker exec variantvalidator pytest -n 3 --cov=VariantValidator --cov=VariantFormatter --cov-report=term tests/' |
| 92 | + |
| 93 | + // Send coverage report to Codecov |
| 94 | + sh 'docker exec variantvalidator codecov -t $CODECOV_TOKEN -b ${BRANCH_NAME}' |
| 95 | + |
| 96 | + // Check for test failures in the captured output |
| 97 | + if (currentBuild.rawBuild.getLog(2000).join('\n').contains("test summary info") && currentBuild.rawBuild.getLog(2000).join('\n').contains("FAILED")) { |
| 98 | + failure(message:"Pytest completed with test failures") |
| 99 | + } |
| 100 | + |
| 101 | + // Check the Jenkins console log for pytest exit code |
| 102 | + def pytestExitCode = currentBuild.rawBuild.getLog(2000).find { line -> line =~ /.*Pytest exit code: (\d+).*/ } |
| 103 | + if (pytestExitCode) { |
| 104 | + pytestExitCode = Integer.parseInt(pytestExitCode.replaceAll(/.*Pytest exit code: (\d+).*/, '$1')) |
| 105 | + if (pytestExitCode != 0) { |
| 106 | + failure(message:"Pytest failed with exit code $pytestExitCode") |
| 107 | + } |
| 108 | + } |
| 109 | + break |
| 110 | + } |
| 111 | + |
| 112 | + echo "Connection failed. Waiting for 60 seconds before the next attempt..." |
| 113 | + sleep 60 |
| 114 | + } |
| 115 | + |
| 116 | + if (!connectionSuccessful) { |
| 117 | + failure(message:"All connection attempts failed. Exiting...") |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + post { |
| 124 | + always { // This ensures cleanup is executed regardless of build outcome |
| 125 | + script { |
| 126 | + // Cleanup Docker |
| 127 | + sh 'docker stop vv-vvta' |
| 128 | + sh 'docker rm vv-vvta' |
| 129 | + sh 'docker stop vv-vdb' |
| 130 | + sh 'docker rm vv-vdb' |
| 131 | + sh 'docker stop vv-seqrepo' |
| 132 | + sh 'docker rm vv-seqrepo' |
| 133 | + sh 'docker stop variantvalidator' |
| 134 | + sh 'docker rm variantvalidator' |
| 135 | + sh 'docker network rm $DOCKER_NETWORK' |
| 136 | + sh 'docker system prune --all --volumes --force' |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments