forked from sontung0/tutorial-jenkins-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
58 lines (57 loc) · 1.56 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
pipeline {
agent any
environment {
FRONTEND_GIT = 'https://github.com/sontung0/tutorial-jenkins-frontend.git'
FRONTEND_BRANCH = 'master'
FRONTEND_IMAGE = 'sontung0/tutorial-jenkins-frontend'
FRONTEND_SERVER = '1.2.3.4'
FRONTEND_SERVER_DIR = './app'
}
stages {
stage('Build JS') {
agent {
docker {
image 'node:latest'
args '-v tutorial_jenkins_frontend_modules:$WORKSPACE/node_modules'
}
}
steps {
git(url: FRONTEND_GIT, branch: FRONTEND_BRANCH)
sh 'npm i'
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"
}
}
}
}
}
}