Files
Geradores/Jenkinsfile
2025-01-06 14:36:07 +00:00

172 lines
5.6 KiB
Groovy

pipeline {
agent any
parameters {
booleanParam(name: 'DeployAll', defaultValue: false, description: 'Deploy Site/WS ?')
booleanParam(name: 'deploySite', defaultValue: false, description: 'Deploy Site ?')
}
triggers {
pollSCM('* * * * *')
}
/*
environment {
now = new Date().format('yyyyMMdd-HHmm', TimeZone.getTimeZone('UTC'))
DOCKER_HOST = 'tcp://192.168.2.20:2375'
DOCKER_TAG = "${env.BUILD_ID}"
DOCKER_REGISTRY = 'Shini89' //'your-docker-registry.com' // Registro Docker
REACT_DIR = 'geradoresfe'
REACT_DOCKER_IMAGE = 'shini89/geradoresfe:lastest'
SERVICE_DIR = 'GeradoresService'
API_DIR = 'GeradoresWS'
API_DOCKER_IMAGE = 'shini89/geradoresws:lastest'
}
*/
environment {
DOCKER_HOST = 'tcp://192.168.2.20:2375'
DOCKER_REGISTRY = 'docker.io/shini89'
DOCKER_TAG = "${env.BUILD_ID}" // Tag única para builds
REACT_DIR = 'geradoresfe'
SERVICE_DIR = 'GeradoresService'
API_DIR = 'GeradoresWS'
}
stages {
stage('Validate Docker Connection') {
steps {
script {
echo "Validando conexão com Docker remoto: ${DOCKER_HOST}"
// Testa a conexão com o Docker remoto
sh 'docker --version'
}
}
}
stage('Check for Changes') {
steps {
script {
def message = ""
CodeChanges = currentBuild.changeSets != []
if (CodeChanges) {
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
message = message + "<br>Autor: ${entry.author.fullName} ( ${entry.author} ) <br> Commit: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
message = message + '<br>Ficheiros:<br><ul>'
for (int k = 0; k < files.size(); k++) {
def file = files[k]
message = message + "<li> ${file.path} ( ${file.editType.name} ) </li>"
}
message = message + '</ul>'
}
}
}
}
}
}
stage('Checkout Code') {
steps {
git branch: 'master', url: 'https://git.homeware.pt/Marco/Geradores.git'
}
}
/*******************************************************
Stage BUILD
*******************************************************/
stage('Build DLL') {
steps {
dir("${env.SERVICE_DIR}") {
sh 'dotnet build -c Release'
}
}
}
stage('Build WS') {
steps {
dir("${env.API_DIR}") {
sh 'dotnet build -c Release'
}
}
}
stage('Build WS Docker Image') {
steps {
script {
docker.build("${DOCKER_REGISTRY}/geradoresws:${DOCKER_TAG}", "-f ${API_DIR}/Dockerfile .")
}
}
}
stage('Build React') {
steps {
dir("${env.REACT_DIR}") {
sh 'npm install'
sh 'npm run build'
/*
sh """
docker build -t ${env.DOCKER_REGISTRY}/react-frontend:latest .
docker push ${env.DOCKER_REGISTRY}/react-frontend:latest
"""*/
}
}
}
stage('Build React Docker Image') {
steps {
dir("${env.REACT_DIR}") {
script {
docker.build("${DOCKER_REGISTRY}/geradoresfe:${DOCKER_TAG}", ".")
}
}
}
}
stage('Push Docker Images') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', 'docker-credentials') {
docker.image("${DOCKER_REGISTRY}/geradoresws:${DOCKER_TAG}").push()
docker.image("${DOCKER_REGISTRY}/geradoresfe:${DOCKER_TAG}").push()
}
}
}
}
stage('Deploy Containers Locally') {
steps {
script {
// Remove contêiner antigo (se existir)
sh """
docker rm -f geradoresws-container || true
docker rm -f geradoresfe-container || true
"""
// Executa contêineres com as novas imagens
sh """
docker run -d --name geradoresws-container -p 8080:8080 ${DOCKER_REGISTRY}/geradoresws:${DOCKER_TAG}
docker run -d --name geradoresfe-container -p 3000:3000 ${DOCKER_REGISTRY}/geradoresfe:${DOCKER_TAG}
"""
}
}
}
}
post {
success {
echo 'Services deployed successfully!'
sh 'docker system prune -f'
}
failure {
echo 'Failed to deploy services. Check logs.'
}
}
}