-
Notifications
You must be signed in to change notification settings - Fork 7
/
.gitlab-ci.yml
127 lines (116 loc) · 3.66 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
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
117
118
119
120
121
122
123
124
125
126
127
stages:
- build
- package
- deploy
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
IMAGE_NAME: registry.gitlab.com/$CI_PROJECT_PATH
#####################
# Building
#####################
build-jar:
stage: build
image: openjdk:11-oracle
variables:
MONGO_INITDB_ROOT_USERNAME: ifunny
MONGO_INITDB_ROOT_PASSWORD: ifunny
MONGODB_HOST: mongo
REDIS_HOST: redis
REDIS_PORT: 6379
services:
- name: mongo
alias: $MONGODB_HOST
- name: redis
alias: $REDIS_HOST
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
script:
- ./gradlew build
after_script:
- cat build/reports/jacoco/test/html/index.html
coverage: '/Total.*?([0-9]{1,3})%/'
artifacts:
paths:
- build/reports/
- build/libs/*.jar
expire_in: 1 week
when: always
cache:
paths:
- .gradle/wrapper
- .gradle/caches
###########################
# Packaging
###########################
package-docker:
image: docker:stable
stage: package
services:
- docker:stable-dind
before_script:
- if [[ ${CI_COMMIT_REF_NAME} == master ]]; then export IMAGE_TAG=latest; else export IMAGE_TAG=${CI_COMMIT_REF_NAME}; fi;
script:
- docker build -t $IMAGE_NAME:$IMAGE_TAG .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
- docker push $IMAGE_NAME:$IMAGE_TAG
only:
- master
- tags
#####################
# Deployment
#####################
.prepare-key-script: &prepare_key
- apk add --no-cache openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
.deploy-script: &deploy
- if [[ ${CI_COMMIT_REF_NAME} == master ]]; then export IMAGE_TAG=latest; else export IMAGE_TAG=${CI_COMMIT_REF_NAME}; fi;
- ssh $SSH_USER@$SSH_HOST "docker stop $CONTAINER_NAME || true"
- ssh $SSH_USER@$SSH_HOST "docker rm $CONTAINER_NAME || true"
- ssh $SSH_USER@$SSH_HOST "docker rmi $IMAGE_NAME:$IMAGE_TAG || true"
- ssh $SSH_USER@$SSH_HOST "docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com"
- |
ssh $SSH_USER@$SSH_HOST "
docker run -d --name $CONTAINER_NAME --restart always \
-p $CONTAINER_PORT:8080 \
-e MONGODB_HOST=$MONGODB_HOST \
-e MONGODB_DATABASE=$MONGODB_DATABASE \
-e MONGODB_USERNAME=$MONGODB_USERNAME \
-e MONGODB_PASSWORD=$MONGODB_PASSWORD \
-e REDIS_HOST=$REDIS_HOST \
-e SPRING_REDIS_PASSWORD=$REDIS_PASSWORD \
-e AWS_S3_ACCESS_KEY=$AWS_S3_ACCESS_KEY \
-e AWS_S3_SECRET_KEY=$AWS_S3_SECRET_KEY \
-e AWS_S3_REGION=$AWS_S3_REGION \
-e AWS_S3_BUCKET=$AWS_S3_BUCKET \
$IMAGE_NAME:$IMAGE_TAG
"
deploy-production:
image: docker:latest
stage: deploy
dependencies: []
variables:
CONTAINER_NAME: ifunny
CONTAINER_PORT: 4386
MONGODB_HOST: $PRODUCTION_MONGODB_HOST
MONGODB_DATABASE: $PRODUCTION_MONGODB_DATABASE
MONGODB_USERNAME: $PRODUCTION_MONGODB_USERNAME
MONGODB_PASSWORD: $PRODUCTION_MONGODB_PASSWORD
REDIS_HOST: $PRODUCTION_REDIS_HOST
REDIS_PASSWORD: $PRODUCTION_REDIS_PASSWORD
AWS_S3_ACCESS_KEY: $PRODUCTION_AWS_S3_ACCESS_KEY
AWS_S3_SECRET_KEY: $PRODUCTION_AWS_S3_SECRET_KEY
AWS_S3_REGION: $PRODUCTION_AWS_S3_REGION
AWS_S3_BUCKET: $PRODUCTION_AWS_S3_BUCKET
before_script: *prepare_key
script: *deploy
environment:
name: production
url: https://ifunny.ruslanys.me
only:
- tags