pipeline {
agent any
environment {
GITLAB_REGISTRY_URL = 'registry.gitlab.com'
GITLAB_REPOSITORY_NAME = 'admin/node-helloworld-api'
DOCKER_COMPOSE_ORG_FILE = './node-helloworld-api/docker-compose.yml'
DOCKER_COMPOSE_PROD_FILE = './node-helloworld-api/docker-compose.prod.yml'
SSH_HOST = '192.168.1.1'
DIRECTORY = './node-helloworld-api'
}
options {
timestamps()
quietPeriod(5)
timeout(time: 30, unit: 'MINUTES')
durabilityHint('PERFORMANCE_OPTIMIZED')
}
stages {
stage('Clone Repository') {
steps {
git(branch: 'main', url: "https://gitlab.com/${GITLAB_REPOSITORY_NAME}", credentialsId: 'gitlab_credentials_id')
}
}
stage('Build image') {
steps {
withCredentials([usernamePassword(usernameVariable: 'GITLAB_USERNAME', passwordVariable: 'GITLAB_PASSWORD', credentialsId: 'gitlab_credentials_id')]) {
sh '''
docker login --username ${GITLAB_USERNAME} --password ${GITLAB_PASSWORD} ${GITLAB_REGISTRY_URL}
docker build -t ${GITLAB_REGISTRY_URL}/${GITLAB_REPOSITORY_NAME}:v${BUILD_NUMBER} --compress .
docker push ${GITLAB_REGISTRY_URL}/${GITLAB_REPOSITORY_NAME}:v${BUILD_NUMBER}
docker logout
'''
}
}
}
stage('Deploy App') {
steps {
withCredentials([
usernamePassword(usernameVariable: 'SSH_USERNAME', passwordVariable: 'SSH_PASSWORD', credentialsId: 'ssh_credentials_id'),
usernamePassword(usernameVariable: 'GITLAB_USERNAME', passwordVariable: 'GITLAB_PASSWORD', credentialsId: 'gitlab_credentials_token_id')
]) {
script {
def remoteServer = [:]
remoteServer.name = env.SSH_USERNAME
remoteServer.host = env.SSH_HOST
remoteServer.user = env.SSH_USERNAME
remoteServer.password = env.SSH_PASSWORD
remoteServer.allowAnyHosts = true
def commandServer = """
docker login --username ${GITLAB_USERNAME} --password ${GITLAB_PASSWORD} ${GITLAB_REGISTRY_URL}
if [ -f "${DOCKER_COMPOSE_PROD_FILE}" ]
then
cat ${DOCKER_COMPOSE_PROD_FILE}
else
git clone https:
touch ${DOCKER_COMPOSE_PROD_FILE}
fi
awk '{gsub(/:v1/, ":v${BUILD_NUMBER}"); print}' ${DOCKER_COMPOSE_ORG_FILE} > ${DOCKER_COMPOSE_PROD_FILE}
docker-compose -f ${DOCKER_COMPOSE_PROD_FILE} up --pull missing --remove-orphans --force-recreate --wait -d
docker logout
rm -rf ${DIRECTORY}
"""
sshCommand(remote: remoteServer, command: commandServer)
}
}
}
}
}
}