-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
87 lines (82 loc) · 3.25 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
stages:
- deploy
- manual-deploy
variables:
ARTIFACT_COMPRESSION_LEVEL: "fast"
CACHE_COMPRESSION_LEVEL: "fast"
workflow:
rules:
- if: $CI_COMMIT_REF_NAME == "main"
when: always
- if: $CI_COMMIT_REF_NAME == "dev"
when: always
- if: '$CI_PIPELINE_SOURCE == "web"'
when: always
remote-docker:
stage: deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
only:
- branches
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- echo "$RUNTIME_ENV" > runtime.env
- echo "Adding SSH Key"
- mkdir -p ~/.ssh
- echo "$DEPLOY_SSH_KEY" > ~/.ssh/gitlab
- chmod 400 ~/.ssh/gitlab
- echo 'IdentityFile ~/.ssh/gitlab' > ~/.ssh/config
- ssh-keyscan -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts
script:
- |
# Create docker network (envidat)
ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "docker network create envidat || true"
# Copy templates dir
USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "mkdir -p $USER_DIR/config/email-relay/$CI_COMMIT_REF_NAME"
scp -r templates "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME:$USER_DIR/config/email-relay/$CI_COMMIT_REF_NAME/"
HOME=$USER_DIR docker compose \
--project-name="email-relay-${CI_COMMIT_REF_NAME}" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
up --detach --force-recreate
# Job for manual deploying of a docker-compose file, to use it's
# docker images, make sure before you run it that the images referenced
# in the docker-compose.$CI_COMMIT_REF_NAME.yml exist!
manual-remote-docker:
stage: manual-deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
when: manual
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- echo "$RUNTIME_ENV" > runtime.env
- echo "Adding SSH Key"
- mkdir -p ~/.ssh
- echo "$DEPLOY_SSH_KEY" > ~/.ssh/gitlab
- chmod 400 ~/.ssh/gitlab
- echo 'IdentityFile ~/.ssh/gitlab' > ~/.ssh/config
- ssh-keyscan -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts -v
script:
- APP_NAME="email-relay-${CI_COMMIT_REF_NAME}"
- USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
- echo "Defined USER_DIR ${USER_DIR}"
# Create docker network (envidat)
# - ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "docker network create envidat || true"
- echo "Copy templates to ${DEPLOY_SSH_USER}@${DEPLOY_HOSTNAME}:${USER_DIR}/config/${APP_NAME}/"
- ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "mkdir -p $USER_DIR/config/$APP_NAME"
- scp -r templates "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME:$USER_DIR/config/$APP_NAME/"
- HOME=$USER_DIR
- echo "Going to remotely start the docker-compose.${CI_COMMIT_REF_NAME}.yml on ${DEPLOY_SSH_USER}@${DEPLOY_HOSTNAME}"
# use docker compose create to recreate the container and force pulling the latest image
# to avoid using the local cache
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
create --force-recreate --pull="always"
# use docker compose up to start the service on the remote machine
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
up --detach