diff --git a/Jenkinsfile b/Jenkinsfile index 122f265..d3327a8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,171 +1,100 @@ - 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_REGISTRY = 'git.homeware.pt' 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' + + 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 { - stage('Validate Docker Connection') { + + stage('Preparar Ambiente') { steps { script { - echo "Validando conexão com Docker remoto: ${DOCKER_HOST}" - // Testa a conexão com o Docker remoto - sh 'docker --version' + echo "Verificar Docker local" + sh 'docker version' } } } - stage('Check for Changes') { + stage('Checkout Repositório') { 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 + "
Autor: ${entry.author.fullName} ( ${entry.author} )
Commit: ${entry.msg}" - def files = new ArrayList(entry.affectedFiles) - message = message + '
Ficheiros:
' - } - } - } - } + git branch: 'main', url: 'https://git.homeware.pt/Marco/Geradores.git' } } - stage('Checkout Code') { + stage('Build .NET Services') { steps { - git branch: 'master', url: 'https://git.homeware.pt/Marco/Geradores.git' - } - } - /******************************************************* - Stage BUILD - *******************************************************/ - stage('Build DLL') { - steps { - dir("${env.SERVICE_DIR}") { + dir("${SERVICE_DIR}") { + sh 'dotnet build -c Release' + } + dir("${API_DIR}") { sh 'dotnet build -c Release' } } } - stage('Build WS') { + stage('Build React App') { 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}") { + dir("${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') { + stage('Docker Build Imagens') { 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 { - 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 { success { - echo 'Services deployed successfully!' + echo 'Deploy feito com sucesso!' sh 'docker system prune -f' } failure { - echo 'Failed to deploy services. Check logs.' + echo 'Erro no pipeline. Verifica os logs!' } } }