ogcore/Jenkinsfile

87 lines
3.1 KiB
Groovy

pipeline {
agent {
label 'jenkins-slave'
}
environment {
DOCKER_REPO = "opengnsys"
DOCKER_CREDENTIALS = credentials('docker-hub-credentials')
DOCKER_TAG = "${env.BUILD_NUMBER}"
DOCKER_IMAGE_NAME = "ogcore"
BRANCH_NAME = "${GIT_BRANCH.split("/")[1]}"
DOCKER_IDENTITY = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}-php:${BRANCH_NAME}-${DOCKER_TAG}"
DOCKER_IDENTITY_NGINX = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}-nginx:${BRANCH_NAME}-${DOCKER_TAG}"
DOCKER_COMPOSE_TEMPLATE = 'docker-compose-ci-template.yaml'
DOCKER_COMPOSE_FILE = 'docker-compose-ci-tmp.yaml'
}
stages {
stage('Build Environment') {
steps {
script {
docker.build("${DOCKER_IDENTITY}", '-f docker/Dockerfile-php .')
docker.build("${DOCKER_IDENTITY_NGINX}", '-f docker/Dockerfile-nginx .')
}
}
}
stage(('Prepare Docker Composer')) {
steps {
sh """
sed 's|ogcore-php:static|${DOCKER_IDENTITY}|g; s|ogcore-nginx:static|${DOCKER_IDENTITY_NGINX}|g' ${DOCKER_COMPOSE_TEMPLATE} > ${DOCKER_COMPOSE_FILE}
"""
}
}
stage('Run containers') {
steps {
sh "docker compose -f ${DOCKER_COMPOSE_FILE} up -d"
}
}
stage('Install dependencies') {
steps {
// Install dependencies
sh """
docker compose exec php composer install
docker compose exec php php bin/console lexik:jwt:generate-keypair --overwrite
docker compose exec php php bin/console doctrine:migrations:migrate --no-interaction
docker compose exec php php bin/console doctrine:fixtures:load --no-interaction
// Create report directory
docker compose exec php mkdir -p /report
"""
}
}
stage('Tests') {
steps {
// Run tests
sh """
docker compose exec php bin/phpunit --log-junit /report/phpunit.xml
docker compose cp php:/report/phpunit.xml ./phpunit.xml
"""
}
post {
success {
script {
docker.withRegistry('https://index.docker.io/v1/', "${DOCKER_CREDENTIALS}") {
docker.image("${DOCKER_IDENTITY}").push()
docker.image("${DOCKER_IDENTITY_NGINX}").push()
}
}
}
}
}
}
post {
always {
// Publicar los resultados de las pruebas de PHPUnit
xunit (
thresholds: [ skipped(failureThreshold: '0'), failed(failureThreshold: '0') ],
tools: [ PHPUnit(pattern: 'phpunit.xml') ]
)
// Remove containers
sh 'docker compose -f ${DOCKER_COMPOSE_FILE} down'
sh 'docker compose -f ${DOCKER_COMPOSE_FILE} rm -f'
}
}
}