Compare commits

..

No commits in common. "main" and "teste" have entirely different histories.
main ... teste

96
Jenkinsfile vendored
View file

@ -15,12 +15,7 @@ pipeline {
timestamps() timestamps()
} }
parameters {
booleanParam(name: 'ROLLBACK', defaultValue: false, description: 'Executar rollback da produção?')
}
stages { stages {
stage('Commit') { stage('Commit') {
steps { steps {
echo 'Pipeline iniciado via push ou merge.' echo 'Pipeline iniciado via push ou merge.'
@ -29,7 +24,7 @@ pipeline {
stage('Checkout Código') { stage('Checkout Código') {
steps { steps {
git branch: 'main', url: 'https://forgeo-olymp.duckdns.org/rayankonecny/portal-app.git' git branch: 'main', url: 'https://forgeo-olymp.duckdns.org/rayankonecny/astro-app.git'
script { script {
env.GIT_COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() env.GIT_COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.BUILD_TAG = "${env.BUILD_NUMBER}-${env.GIT_COMMIT}" env.BUILD_TAG = "${env.BUILD_NUMBER}-${env.GIT_COMMIT}"
@ -41,7 +36,7 @@ pipeline {
stage('Build') { stage('Build') {
steps { steps {
sh 'npm ci' sh 'npm ci'
sh 'npm run build || echo "Build não crítico"' sh 'npm run build'
} }
} }
@ -51,53 +46,42 @@ pipeline {
} }
} }
stage('SonarQube Analysis') {
environment {
SONAR_SCANNER_HOME = tool 'SonarQube Scanner'
}
steps {
withSonarQubeEnv('SonarQube') {
sh """
${SONAR_SCANNER_HOME}/bin/sonar-scanner \
-Dsonar.projectKey=${SONAR_PROJECT_KEY} \
-Dsonar.sources=. \
-Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.login=$SONAR_AUTH_TOKEN
"""
}
}
}
stage('Deploy to Stage') { stage('Deploy to Stage') {
steps { steps {
sshagent(credentials: ['chave-ssh-id']) { sshagent(credentials: ['chave-ssh-id']) {
sh """ sh """
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} " ssh ${DEPLOY_USER}@${DEPLOY_SERVER} '
set -e set -e
rm -rf ${REMOTE_PATH} rm -rf ${REMOTE_PATH}
git clone https://forgeo-olymp.duckdns.org/rayankonecny/portal-app.git ${REMOTE_PATH} mkdir -p ${REMOTE_PATH}
cd ${REMOTE_PATH} cd ${REMOTE_PATH}
sed -i '/NODE_ENV/d' .env || true
cat <<EOF > Dockerfile cat <<EOF > Dockerfile
FROM node:18-alpine # STAGE 1: Build (não será usado aqui no servidor)
WORKDIR /app FROM node:20-alpine AS build
COPY package*.json ./
RUN npm install # STAGE 2: Produção estática
COPY . . FROM node:20-alpine
EXPOSE 5173 WORKDIR /var/app/usr/astro
CMD [\\"npm\\", \\"run\\", \\"dev\\"] RUN npm install -g serve
COPY dist ./dist
EXPOSE 3000
CMD ["serve", "dist", "-l", "3000"]
EOF EOF
podman build -t ${IMAGE_NAME}:${BUILD_TAG}-stage -t ${IMAGE_NAME}:stage . exit
'
# Copia arquivos necessários via SCP
scp -r ./dist ${DEPLOY_USER}@${DEPLOY_SERVER}:${REMOTE_PATH}/dist
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} '
cd ${REMOTE_PATH}
podman build -t ${IMAGE_NAME}:${BUILD_TAG}-stage -t ${IMAGE_NAME}:stage .
podman stop ${CONTAINER_NAME}-stage || true podman stop ${CONTAINER_NAME}-stage || true
podman rm ${CONTAINER_NAME}-stage || true podman rm ${CONTAINER_NAME}-stage || true
podman run -d --network host --name ${CONTAINER_NAME}-stage ${IMAGE_NAME}:${BUILD_TAG}-stage podman run -d --network host --name ${CONTAINER_NAME}-stage ${IMAGE_NAME}:${BUILD_TAG}-stage
" '
""" """
} }
} }
@ -113,17 +97,14 @@ EOF
steps { steps {
sshagent(credentials: ['chave-ssh-id']) { sshagent(credentials: ['chave-ssh-id']) {
sh """ sh """
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} " ssh ${DEPLOY_USER}@${DEPLOY_SERVER} '
echo '${BUILD_TAG}' > ${REMOTE_PATH}/last_successful_build.txt echo "${BUILD_TAG}" > ${REMOTE_PATH}/last_successful_build.txt
cd ${REMOTE_PATH} cd ${REMOTE_PATH}
podman build -t ${IMAGE_NAME}:${BUILD_TAG}-prod -t ${IMAGE_NAME}:prod . podman build -t ${IMAGE_NAME}:${BUILD_TAG}-prod -t ${IMAGE_NAME}:prod .
podman stop ${CONTAINER_NAME} || true podman stop ${CONTAINER_NAME} || true
podman rm ${CONTAINER_NAME} || true podman rm ${CONTAINER_NAME} || true
podman run -d --network host --name ${CONTAINER_NAME} ${IMAGE_NAME}:${BUILD_TAG}-prod podman run -d --network host --name ${CONTAINER_NAME} ${IMAGE_NAME}:${BUILD_TAG}-prod
" '
""" """
} }
} }
@ -133,9 +114,9 @@ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} "
steps { steps {
sshagent(credentials: ['chave-ssh-id']) { sshagent(credentials: ['chave-ssh-id']) {
sh """ sh """
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} " ssh ${DEPLOY_USER}@${DEPLOY_SERVER} '
podman ps | grep ${CONTAINER_NAME} || echo 'Container não encontrado' podman ps | grep ${CONTAINER_NAME} || echo "Container não encontrado"
" '
""" """
} }
} }
@ -148,26 +129,23 @@ ssh ${DEPLOY_USER}@${DEPLOY_SERVER} "
steps { steps {
sshagent(credentials: ['chave-ssh-id']) { sshagent(credentials: ['chave-ssh-id']) {
sh """ sh """
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} " ssh ${DEPLOY_USER}@${DEPLOY_SERVER} '
TAG=\$(cat ${REMOTE_PATH}/last_successful_build.txt || echo 'latest') TAG=$(cat ${REMOTE_PATH}/last_successful_build.txt || echo "latest")
echo "[INFO] Restaurando versão: \$TAG"
echo '[INFO] Restaurando versão: \$TAG'
podman stop ${CONTAINER_NAME} || true podman stop ${CONTAINER_NAME} || true
podman rm ${CONTAINER_NAME} || true podman rm ${CONTAINER_NAME} || true
podman run -d --network host --name ${CONTAINER_NAME} ${IMAGE_NAME}:\$TAG podman run -d --network host --name ${CONTAINER_NAME} ${IMAGE_NAME}:\$TAG
" '
""" """
} }
} }
} }
stage('Limpeza de Imagens Antigas') { stage('Limpeza') {
steps { steps {
sshagent(credentials: ['chave-ssh-id']) { sshagent(credentials: ['chave-ssh-id']) {
sh """ sh """
ssh ${DEPLOY_USER}@${DEPLOY_SERVER} "podman image prune -f" ssh ${DEPLOY_USER}@${DEPLOY_SERVER} 'podman image prune -f'
""" """
} }
} }
@ -194,4 +172,8 @@ curl -H "Content-Type: application/json" -X POST \
} }
} }
} }
parameters {
booleanParam(name: 'ROLLBACK', defaultValue: false, description: 'Executar rollback da produção?')
}
} }