jenkinsfile

This commit is contained in:
Marco Santos
2025-07-24 02:12:56 +01:00
parent e06815053a
commit ed729b9c8e

171
Jenkinsfile vendored
View File

@@ -1,171 +1,100 @@
pipeline { pipeline {
agent any agent any
parameters {
booleanParam(name: 'DeployAll', defaultValue: false, description: 'Deploy Site/WS ?')
booleanParam(name: 'deploySite', defaultValue: false, description: 'Deploy Site ?')
}
triggers {
pollSCM('* * * * *')
}
/*
environment { environment {
now = new Date().format('yyyyMMdd-HHmm', TimeZone.getTimeZone('UTC')) DOCKER_REGISTRY = 'git.homeware.pt'
DOCKER_HOST = 'tcp://192.168.2.20:2375'
DOCKER_TAG = "${env.BUILD_ID}" 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' REACT_DIR = 'geradoresfe'
SERVICE_DIR = 'GeradoresService' SERVICE_DIR = 'GeradoresService'
API_DIR = 'GeradoresWS' API_DIR = 'GeradoresWS'
REACT_IMAGE = "${DOCKER_REGISTRY}/geradores/fe:${DOCKER_TAG}"
WS_IMAGE = "${DOCKER_REGISTRY}/geradores/ws:${DOCKER_TAG}"
DOCKER_CREDENTIALS_ID = 'docker-registry-creds'
KUBECONFIG_CREDENTIALS_ID = 'kubeconfig-jenkins'
} }
stages { stages {
stage('Validate Docker Connection') {
stage('Preparar Ambiente') {
steps { steps {
script { script {
echo "Validando conexão com Docker remoto: ${DOCKER_HOST}" echo "Verificar Docker local"
// Testa a conexão com o Docker remoto sh 'docker version'
sh 'docker --version'
} }
} }
} }
stage('Check for Changes') { stage('Checkout Repositório') {
steps { steps {
script { git branch: 'main', url: 'https://git.homeware.pt/Marco/Geradores.git'
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') { stage('Build .NET Services') {
steps { steps {
git branch: 'master', url: 'https://git.homeware.pt/Marco/Geradores.git' dir("${SERVICE_DIR}") {
} sh 'dotnet build -c Release'
} }
/******************************************************* dir("${API_DIR}") {
Stage BUILD
*******************************************************/
stage('Build DLL') {
steps {
dir("${env.SERVICE_DIR}") {
sh 'dotnet build -c Release' sh 'dotnet build -c Release'
} }
} }
} }
stage('Build WS') { stage('Build React App') {
steps { steps {
dir("${env.API_DIR}") { dir("${REACT_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 install'
sh 'npm run build' 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') { stage('Docker Build Imagens') {
steps { steps {
dir("${env.REACT_DIR}") { script {
docker.build("${WS_IMAGE}", "${API_DIR}")
docker.build("${REACT_IMAGE}", "${REACT_DIR}")
}
}
}
stage('Push para Docker Registry') {
steps {
script {
docker.withRegistry("https://${DOCKER_REGISTRY}", DOCKER_CREDENTIALS_ID) {
docker.image("${WS_IMAGE}").push()
docker.image("${REACT_IMAGE}").push()
}
}
}
}
stage('Deploy no Kubernetes') {
steps {
withKubeConfig(credentialsId: KUBECONFIG_CREDENTIALS_ID) {
script { script {
docker.build("${DOCKER_REGISTRY}/geradoresfe:${DOCKER_TAG}", ".") // Substitui as imagens no manifest e aplica
sh """
sed 's|{{WS_IMAGE}}|${WS_IMAGE}|g; s|{{FE_IMAGE}}|${REACT_IMAGE}|g' k8s/deployment.template.yaml > k8s/deployment.yaml
kubectl apply -f k8s/deployment.yaml
"""
} }
} }
} }
} }
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 32772:8080 ${DOCKER_REGISTRY}/geradoresws:${DOCKER_TAG}
docker run -d --name geradoresfe-container -p 3000:3000 ${DOCKER_REGISTRY}/geradoresfe:${DOCKER_TAG}
"""
}
}
}
} }
post { post {
success { success {
echo 'Services deployed successfully!' echo 'Deploy feito com sucesso!'
sh 'docker system prune -f' sh 'docker system prune -f'
} }
failure { failure {
echo 'Failed to deploy services. Check logs.' echo 'Erro no pipeline. Verifica os logs!'
} }
} }
} }