-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
63 lines (55 loc) · 1.88 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
59
60
61
62
63
pipeline {
agent any
environment {
REGISTRY_URL = 'ghcr.io'
IMAGE_NAME = sh(script: 'echo $GIT_URL | sed -E "s/.*[:\\/]([^\\/]+\\/[^\\/]+)\\.git$/\\1/"', returnStdout: true).trim()
IMAGE_TAG = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
IMAGE_URL = "${REGISTRY_URL}/${IMAGE_NAME}:${IMAGE_TAG}"
CONTAINER_NAME = sh(script: 'echo $GIT_URL | sed -E "s/.*[:\\/]([^\\/]+\\/[^\\/]+)\\.git$/\\1/" | tr "/" "-"', returnStdout: true).trim()
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build Image') {
steps {
script {
docker.build(env.IMAGE_URL)
}
}
}
stage('Push Image') {
steps {
script {
docker.withRegistry('https://ghcr.io', 'ghcr') {
docker.image(env.IMAGE_URL).push()
docker.image(env.IMAGE_URL).push("latest")
}
}
}
}
stage('Remove Container') {
steps {
script {
sh "docker rm -f ${env.CONTAINER_NAME} || true"
}
}
}
stage('Deploy Container') {
steps {
script {
sh "docker create \
--name ${env.CONTAINER_NAME} \
--restart always \
--network proxy \
--volume /mnt/data/services/portfolio/back/.env:/app/.env \
${env.IMAGE_URL}"
sh "docker network connect portfolio ${env.CONTAINER_NAME}"
sh "docker start ${env.CONTAINER_NAME}"
}
}
}
}
}