-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
70 lines (61 loc) · 2.77 KB
/
Jenkinsfile
File metadata and controls
70 lines (61 loc) · 2.77 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Deploy') {
steps {
script {
def deployHost = (params.DEPLOY_HOST ?: env.DEPLOY_HOST ?: '').trim()
def deployDir = (params.DEPLOY_DIR ?: env.DEPLOY_DIR ?: '/opt/textdiff').trim()
def deployCredentialId = (params.DEPLOY_SSH_CREDENTIAL_ID ?: env.DEPLOY_SSH_CREDENTIAL_ID ?: '').trim()
if (!deployHost) {
error("Missing DEPLOY_HOST. Configure it as a Jenkins job parameter or environment variable.")
}
if (!deployCredentialId) {
error("Missing DEPLOY_SSH_CREDENTIAL_ID. Configure it as a Jenkins job parameter or environment variable.")
}
sh "tar czf /tmp/textdiff.tar.gz --exclude='.git' --exclude='node_modules' --exclude='dist' --exclude='deploy/.env' ."
def remote = [:]
remote.name = 'deployment-server'
remote.host = deployHost
remote.user = 'root'
remote.port = 22
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: deployCredentialId, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName ?: 'root'
remote.identityFile = identity
sshCommand remote: remote, command: "mkdir -p ${deployDir}"
sshPut remote: remote, from: '/tmp/textdiff.tar.gz', into: '/tmp/'
sshCommand remote: remote, command: """
cd ${deployDir}
tar xzf /tmp/textdiff.tar.gz
rm -f /tmp/textdiff.tar.gz
cd deploy
docker compose build --build-arg NODE_IMAGE=docker.xuanyuan.run/node:22-alpine
docker create --name textdiff-tmp textdiff:latest
rm -rf ${deployDir}/dist
docker cp textdiff-tmp:/build/dist ${deployDir}/dist
docker rm textdiff-tmp
nginx -s reload 2>/dev/null || true
"""
}
}
}
}
}
post {
success {
echo 'TextDiff deployed successfully.'
}
failure {
echo 'Deploy failed. Check console output above for details.'
}
always {
sh 'rm -f /tmp/textdiff.tar.gz'
}
}
}