pipeline { agent any environment { FRONTEND_GIT = 'http://35.222.33.50/root/newreact.git' FRONTEND_BRANCH = 'master' CREDENTIALS_ID = 'gitlab' FRONTEND_IMAGE = 'nhatcuongboy/myreact' FRONTEND_SERVER = '139.180.155.230' FRONTEND_SERVER_DIR = './app' } stages { stage('Build JS') { agent { docker { image 'node:latest' args '-v react_modules:$WORKSPACE/node_modules' } } steps { git( url: FRONTEND_GIT, credentialsId: CREDENTIALS_ID, branch: FRONTEND_BRANCH ) sh 'npm install' sh 'npm run build' stash(name: 'frontend', includes: 'build/*/**') } } stage('Build Image') { steps { unstash 'frontend' script { docker.withRegistry('', 'docker-hub') { def image = docker.build(FRONTEND_IMAGE) image.push(BUILD_ID) } } } } stage('Deploy') { steps { script { withCredentials([sshUserPrivateKey( credentialsId: 'ssh', keyFileVariable: 'identityFile', passphraseVariable: '', usernameVariable: 'user' )]) { def remote = [:] remote.name = 'server' remote.host = FRONTEND_SERVER remote.user = user remote.identityFile = identityFile remote.allowAnyHosts = true sshCommand remote: remote, command: "cd $FRONTEND_SERVER_DIR && export FRONTEND_IMAGE=$FRONTEND_IMAGE:$BUILD_ID && docker-compose up -d" } } } } } }