-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
77 lines (66 loc) · 2.22 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
stages:
- publish-image
- deploy-production
variables:
IMAGE_TAG: $CI_PIPELINE_ID-$CI_COMMIT_SHORT_SHA
IMAGE_URL: $SSC_REGISTRY_URL/$CI_PROJECT_PATH:$CI_PIPELINE_ID-$CI_COMMIT_SHORT_SHA
workflow:
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: always
- if: '$CI_COMMIT_BRANCH == "staging"'
when: always
- when: never
var-check:
stage: publish-image
image:
name: reg.aichallenge.ir/alpine:latest
entrypoint: [""]
before_script:
- echo "service == $SERVICE_NAME"
- echo "reg ====== $SSC_REGISTRY_URL"
- echo "image tag == $IMAGE_TAG"
- echo "image url == $IMAGE_URL"
- echo "ip ======== $SWARM_MANAGER_SERVER"
script:
- sed -i "2 i\ENV TOF_MODE=$CI_COMMIT_BRANCH" Dockerfile
- cat Dockerfile
build-push-image:
stage: publish-image
image:
name: $SSC_REGISTRY_URL/kaniko:debug
entrypoint: [""]
# rules:
# - if: '$CI_COMMIT_BRANCH == "main"'
# when: on_success
# - if: '$CI_COMMIT_BRANCH == "staging"'
# when: on_success
# - when: never
before_script:
- mkdir -p /kaniko/.docker/
- echo "{\"auths\":{\"$SSC_REGISTRY_URL\":{\"username\":\"$SSC_REGISTRY_USERNAME\",\"password\":\"$SSC_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- echo pushing image $IMAGE_URL to registry
- sed -i "2 i\ENV TOF_MODE=$CI_COMMIT_BRANCH" Dockerfile
- cat Dockerfile
script:
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $IMAGE_URL
deploy-production:
stage: deploy-production
image: kroniak/ssh-client:latest
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: on_success
- if: '$CI_COMMIT_BRANCH == "staging"'
when: on_success
- when: never
before_script:
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 700 ~/.ssh/id_rsa
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa
- declare -A NAME_PREFIX
- NAME_PREFIX=([main]=prd [staging]=stg)
- FINAL_SERVICE_NAME=${NAME_PREFIX[$CI_COMMIT_BRANCH]}_$SERVICE_NAME
script:
- ssh -o "StrictHostKeyChecking=no" root@$SWARM_MANAGER_SERVER -p$SWARM_MANAGER_SERVER_SSH_PORT -t "docker service update --image $IMAGE_URL $FINAL_SERVICE_NAME"