-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJenkinsfile
116 lines (94 loc) · 3.84 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pipeline {
agent any
environment {
AWS_CRED = "AWS_sortlog"
AWS_REGION = "ap-southeast-2"
SORTLOG_DEV_REPO = "sortlog-dev"
SORTLOG_PROD_REPO = "sortlog-prod"
IMAGE_DEV = "$SORTLOG_DEV_REPO"
IMAGE_PROD = "$SORTLOG_PROD_REPO"
IMAGE_TAG = "${env.BUILD_TAG}"
ECR_URL = "003374733998.dkr.ecr.ap-southeast-2.amazonaws.com"
}
//Install denpendencies
stages{
stage('Install dependency')
{
steps{
echo "Installing packages"
sh 'yarn install'
}
}
stage ('Test') {
steps {
echo "Testing...."
sh 'yarn pre-commit'
}
}
stage('Build Docker Image and Image Updating to ECR'){
when { anyOf { branch 'main'; branch 'dev' } }
stages {
stage ('Docker Build'){
agent {
docker {
image 'tremendousure/sortlog'
}
}
steps {
withAWS(credentials: AWS_CRED, region: AWS_REGION){
script {
if(currentBuild.result != null && currentBuild.result != 'SUCCESS'){
return false
}
if (env.BRANCH_NAME == 'dev' ){
echo "Building and Uploading Dev Docker Image to ECR"
sh '''
docker build -t $IMAGE_DEV:$IMAGE_TAG .
docker images --filter reference=$IMAGE_DEV
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_URL
docker tag $IMAGE_DEV:$IMAGE_TAG $ECR_URL/$IMAGE_DEV:$IMAGE_TAG
docker push $ECR_URL/$IMAGE_DEV:$IMAGE_TAG
'''
}
if (env.BRANCH_NAME == 'main'){
echo "Building and Uploading Prod Docker Image to ECR"
sh '''
docker build -t $IMAGE_PROD:$IMAGE_TAG .
docker images --filter reference=$IMAGE_PROD
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_URL
docker tag $IMAGE_PROD:$IMAGE_TAG $ECR_URL/$IMAGE_PROD:$IMAGE_TAG
docker push $ECR_URL/$IMAGE_PROD:$IMAGE_TAG
'''
}
}
}
}
}
}
}
}
post {
always {
script {
try{
// docker images -qa | xargs docker rmi -f
sh'''
docker rmi -f $(docker images -q)
docker system prune -f
cleanWs()
'''
} catch (Exception e) {
echo "docker clean failed"
}
}
}
failure {
// send message it was failsure
echo "uhm... 我觉得不太行!"
}
success {
// send message it was success
echo "老铁!恭喜你,成功了呀!"
}
}
}