-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
57 lines (52 loc) · 1.72 KB
/
.gitlab-ci.yml
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
image: openjdk:10.0.1-slim
stages:
- build
- deploy_test
variables:
SERVICE_NAME: 'spring-auth'
DEPLOY_PATH: '/home/projects/$SERVICE_NAME'
SERVICE_DIST_FILE_PATH: 'build/libs/*.jar'
APP_PORT: 8090
before_script:
- echo "Before script..."
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- apt-get -y install zip unzip
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
build:
stage: build
script:
- echo "Initiliaze build task..."
- ./gradlew clean build
- echo "Finalized build task..."
artifacts:
untracked: true
tags:
- build
deploy_test:
variables:
DEPLOY_HOST: 'root@159.203.67.88'
DEPLOY_PATH: '/home/projects/$SERVICE_NAME'
DIST_FILE: '$SERVICE_NAME-distribution.jar'
DOCKER_IMAGE_NAME: '$SERVICE_NAME/java'
stage: deploy_test
tags:
- deploy_test
dependencies:
- build
script:
- echo "Deploy prod starting..."
- ssh -o StrictHostKeyChecking=no $DEPLOY_HOST 'exit'
- ssh $DEPLOY_HOST "rm -rf $DEPLOY_PATH/*"
- ssh $DEPLOY_HOST "mkdir -p $DEPLOY_PATH/"
- zip -r $DIST_FILE Dockerfile $SERVICE_DIST_FILE_PATH
- scp $DIST_FILE $DEPLOY_HOST:$DEPLOY_PATH/
- ssh $DEPLOY_HOST "unzip $DEPLOY_PATH/$DIST_FILE -d $DEPLOY_PATH/"
- ssh $DEPLOY_HOST "rm $DEPLOY_PATH/$DIST_FILE"
- ssh $DEPLOY_HOST "docker system prune -f"
- ssh $DEPLOY_HOST "cd $DEPLOY_PATH/ && docker build -f Dockerfile -t $DOCKER_IMAGE_NAME ."
- ssh $DEPLOY_HOST "docker stop $SERVICE_NAME || true && docker rm $SERVICE_NAME || true"
- ssh $DEPLOY_HOST "docker run -e APP_PORT=$APP_PORT -p $APP_PORT:$APP_PORT --name $SERVICE_NAME -d $DOCKER_IMAGE_NAME"
- echo "Deploy prod finished"