-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-ci.yml
48 lines (42 loc) · 2.1 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
#image: docker:20.10.6
#services:
# - docker:20.10.6-dind
stages:
- build
- deploy
variables:
IMAGE_PREFIX: $DOCKER_HUB_USERNAME/$CI_PROJECT_NAME
IMAGE_TAG: $DOCKER_HUB_USERNAME/$CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA
PORT: 8082
DOCKER_NETWORK: my-network
before_script:
- docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_ACCESS_TOKEN
build:
stage: build
only:
- master
tags:
- develop
script:
- echo $( mvn -version)
- mvn clean package
# && docker push $IMAGE_TAG // slow to push
- if [ "$(docker images -a | grep $IMAGE_PREFIX | grep $CI_COMMIT_SHORT_SHA)" ]; then echo No image build or push; else docker build --pull -t $IMAGE_TAG . ; fi
- echo $(ls)
- echo $(pwd)
deploy:
stage: deploy
only:
- master
tags:
- develop
script:
- echo $IMAGE_TAG
# current version container already fail, then remove
- if [ "$(docker ps -a | grep $IMAGE_PREFIX | grep $CI_COMMIT_SHORT_SHA | grep Exited)" ]; then docker network disconnect $DOCKER_NETWORK $CI_PROJECT_NAME && docker rm $(docker ps -a | grep $IMAGE_PREFIX | grep $CI_COMMIT_SHORT_SHA | grep Exited | awk '{print $1}') -f; fi
# other version container exist, then stop and remove
- if [ "$(docker ps -a | grep $IMAGE_PREFIX | grep -v $CI_COMMIT_SHORT_SHA)" ]; then docker network disconnect $DOCKER_NETWORK $CI_PROJECT_NAME && docker stop $(docker ps -a | grep $IMAGE_PREFIX | grep -v $CI_COMMIT_SHORT_SHA | awk '{print $1}') && docker rm $(docker ps -a | grep $IMAGE_PREFIX | grep -v $CI_COMMIT_SHORT_SHA | awk '{print $1}') -f; fi
# other version images exist, then remove
- if [ "$(docker images -a | grep $IMAGE_PREFIX | grep -v $CI_COMMIT_SHORT_SHA)" ]; then docker rmi $(docker images -a | grep $IMAGE_PREFIX | grep -v $CI_COMMIT_SHORT_SHA | awk '{print $3}') -f; fi
# no current version container exist, then run a new container
- if [ "$(docker ps -a | grep $IMAGE_PREFIX | grep $CI_COMMIT_SHORT_SHA)" ]; then echo No need to run new container; else docker run -d --name $CI_PROJECT_NAME -p $PORT:$PORT $IMAGE_TAG && docker network connect $DOCKER_NETWORK $CI_PROJECT_NAME; fi