Skip to content

Instantly share code, notes, and snippets.

@lccalluchi
Created December 13, 2024 01:39
Show Gist options
  • Select an option

  • Save lccalluchi/6127ac8aee32b7c6013dd71efdc1947c to your computer and use it in GitHub Desktop.

Select an option

Save lccalluchi/6127ac8aee32b7c6013dd71efdc1947c to your computer and use it in GitHub Desktop.
pipeline {
agent any
environment {
REPO_DIR = '/home/projects/dock-fastmvp' // Ruta completa a tu proyecto
}
stages {
stage('Prepare Repository') {
steps {
script {
// Verificar si el directorio existe, y si no, crearlo
sh '''
if [ ! -d "${REPO_DIR}" ]; then
echo "Directorio no encontrado, creando ${REPO_DIR}..."
mkdir -p ${REPO_DIR}
fi
'''
// Si el repositorio ya existe, hacer un git pull, sino clonar el repositorio
if (fileExists("${REPO_DIR}/.git")) {
echo "Repositorio ya existe, haciendo git pull..."
dir(REPO_DIR) {
sh 'git pull origin main'
}
} else {
echo "Repositorio no encontrado, clonando..."
dir(REPO_DIR) {
git credentialsId: 'github_credentials', branch: 'main', url: 'https://github.com/andritowmega/fastmvp.git'
}
}
}
}
}
stage('Create configDb.json if not exists') {
steps {
script {
// Verificar si el archivo configDb.json no existe
sh '''
if [ ! -f "${REPO_DIR}/config/configDb.json" ]; then
echo "Archivo configDb.json no encontrado, creando..."
mkdir -p ${REPO_DIR}/config
echo '{"test": {
"type": "postgres",
"connection": {
"user": "test",
"host": "161.132.40.70",
"database": "test2",
"password": "123456",
"port": 5432
},
"token_secret": "TokenRonaldo2024%"
}}' > ${REPO_DIR}/config/configDb.json
else
echo "El archivo configDb.json ya existe, no es necesario crear."
fi
'''
}
}
}
stage('Build Docker Image') {
steps {
dir(REPO_DIR) {
script {
// Detener y eliminar el contenedor antes de reconstruir la imagen
sh 'docker stop fastmvp || true'
sh '''
if docker ps -a | grep -q fastmvp; then
docker rm fastmvp
fi
'''
// Reconstruir la imagen de Docker
sh 'docker build -t fastmvp .'
}
}
}
}
stage('Run Docker Container') {
steps {
script {
def status = sh(script: 'docker run -d --name fastmvp -p 3005:3000 -v /home/mp3/data:/app/data --restart on-failure:15 fastmvp', returnStatus: true)
if (status != 0) {
error "Docker container failed to start!"
}
}
}
}
stage('Clean Up') {
steps {
sh 'docker image prune -f'
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment