pipeline { agent any triggers { GenericTrigger( genericVariables: [ [key: 'action', value: '$.action'], [key: 'merged', value: '$.pull_request.merged'] ], causeString: 'Pipeline disparado por merge de PR no Forgejo', printContributedVariables: true, printPostContent: true, regexpFilterText: '$action $merged', regexpFilterExpression: 'closed true' ) } environment { DEPLOY_USER = "jenkins" DEPLOY_SERVER = "192.168.1.81" IMAGE_NAME = "portal-app" CONTAINER_NAME = "portal-app" REMOTE_PATH = "/home/jenkins/app" } stages { stage('Testar Conexão SSH') { steps { sshagent(credentials: ['chave-ssh-id']) { sh "ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} 'echo Conexão SSH bem-sucedida!'" } } } stage('Ver chave pública usada') { steps { sshagent(credentials: ['chave-ssh-id']) { sh 'ssh-add -L' } } } stage('Checkout Código (local, só para pegar commit hash)') { steps { git branch: 'main', url: 'https://forgeo-olymp.duckdns.org/rayankonecny/portal-app.git' // usando IP local por confiabilidade script { env.GIT_COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() env.BUILD_TAG = "${env.BUILD_NUMBER}-${env.GIT_COMMIT}" echo "BUILD_TAG definido como: ${env.BUILD_TAG}" } } } stage('Clonar Projeto e Criar Dockerfile no Servidor') { steps { sshagent(credentials: ['chave-ssh-id']) { script { sh """ ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} ' set -e mkdir -p ${REMOTE_PATH} git config --global --add safe.directory ${REMOTE_PATH} rm -rf ${REMOTE_PATH} git clone https://forgeo-olymp.duckdns.org/rayankonecny/portal-app.git ${REMOTE_PATH} if [ -f ${REMOTE_PATH}/.env ]; then echo "[INFO] Removendo NODE_ENV do .env para evitar erro com Vite" sed -i "/NODE_ENV/d" ${REMOTE_PATH}/.env fi cat < ${REMOTE_PATH}/Dockerfile FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 5173 CMD ["npm", "run", "dev"] EOF ' """ } } } } stage('Build da Imagem Podman') { steps { sshagent(credentials: ['chave-ssh-id']) { sh """ ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} ' cd ${REMOTE_PATH} && podman build -t ${IMAGE_NAME}:${BUILD_TAG} -t ${IMAGE_NAME}:latest . ' """ } } } stage('Parar e Remover Container Antigo') { steps { sshagent(credentials: ['chave-ssh-id']) { sh """ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} ' podman stop ${CONTAINER_NAME} || true && podman rm ${CONTAINER_NAME} || true ' """ } } } stage('Deploy no Servidor') { steps { sshagent(credentials: ['chave-ssh-id']) { sh """ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} ' podman run -d --network host --name ${CONTAINER_NAME} ${IMAGE_NAME}:${BUILD_TAG} ' """ } } } stage('Verificar Status do Container') { steps { sshagent(credentials: ['chave-ssh-id']) { sh """ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} ' podman ps | grep ${CONTAINER_NAME} || echo "Container não encontrado" ' """ } } } stage('Limpeza de Imagens Antigas') { steps { sshagent(credentials: ['chave-ssh-id']) { sh """ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} ' podman image prune -f ' """ } } } } }