/**
* Jenkinsfile to deploy multiple Docker containers based on docker-compose into a DEV server and run any test.
* This pipeline will run the Docker containers, execute the tests and then stop and remove the containers from the DEV
* server automatically.
**/
pipeline {
agent any
environment {
APP_URL = "https://kea.curex-project.eu"
KEYCLOAK_URL = "https://keycloak-curex.gnubila.fr/auth/realms/Integration/protocol/openid-connect/token"
USER = credentials('keycloak-test-user')
PASSWORD = credentials('keycloak-test-password')
}
stages {
stage('Run tests') {
steps {
script {
echo '*************'
echo '*** TESTS ***'
echo '*************'
/* Here do your tests */
try {
String testName = "KEA_keycloak_fail"
String url = "$APP_URL/api/v1/od/status"
String responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
if ( responseCode != '401' ) {
error("$testName: Returned status code = $responseCode when calling $url")
}
testName = "KEA_keycloak_success"
String token = sh(label: "KEA_keycloak_token", script: "curl -s -X POST $KEYCLOAK_URL -H 'Content-Type: application/x-www-form-urlencoded' -d 'username=$USER' -d 'password=$PASSWORD' -d 'grant_type=password' -d 'client_id=KEA' | sed 's/.*access_token\":\"//g' | sed 's/\".*//g'", returnStdout: true)
responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Authorization: Bearer $token' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
if ( responseCode != '401' ) {
error("$testName: Returned status code = $responseCode when calling $url")
}
} catch (ignored) {
currentBuild.result = 'FAILURE'
echo "KEA Keycloak Tests failed"
}
}
}
}
stage('Stop and remove the Docker containers in DEV server') {
steps {
script {
docker.withServer("$DEPLOYMENT_HOST", "$DEPLOYMENT_HOST_CREDENTIALS") {
docker.withRegistry("$PRIVATE_REGISTRY" , 'artifactory') {
//echo 'Stop and remove the specified Docker containers from the DEV server'
sh 'sh delete_hetzner.sh'
}
}
}
}
}
}
}