gitlab-ci.yml 2.54 KB
Newer Older
torgiren's avatar
torgiren committed
1
2
3
4
5
6
7
---
stages:
  - upload

upload:
  stage: upload
  image:
8
    name: alpine/helm
torgiren's avatar
torgiren committed
9
10
11
  before_script:
    - apk add --no-cache ca-certificates git curl yq
    - helm plugin install https://github.com/chartmuseum/helm-push.git
torgiren's avatar
torgiren committed
12
    - helm repo add --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD} my-repo "${CI_API_V4_URL}/projects/6/packages/helm/stable"
torgiren's avatar
torgiren committed
13
14
15
16
17
    - helm repo update
  script:
    - export VERSION=$(git describe --tags)
    - sed -i "s/__VERSION__/$VERSION/" chart/Chart.yaml
    - helm lint chart
torgiren's avatar
torgiren committed
18
    - helm cm-push chart my-repo
torgiren's avatar
torgiren committed
19
20

.deploy-to-git:
torgiren's avatar
torgiren committed
21
  image: alpine
torgiren's avatar
torgiren committed
22
23
24
25
26
27
28
29
  before_script:
    - apk add --no-cache ca-certificates git curl yq openssh-client-default
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$GITLAB_SSH_KEY")
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - mkdir -p ~/.ssh
    - ssh-keyscan -t rsa gitlab.exphost.pl >> ~/.ssh/known_hosts
torgiren's avatar
torgiren committed
30
31
    - curl -L "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o /usr/local/bin/kubectl
    - chmod +x /usr/local/bin/kubectl
torgiren's avatar
torgiren committed
32
33
  script:
    - export VERSION=$(git describe --tags)
torgiren's avatar
torgiren committed
34
    - echo "VERSION $VERSION"
torgiren's avatar
torgiren committed
35
36
37
38
    - git clone $repo repo
    - cd repo
    - echo ${patch_cmd}
    - eval ${patch_cmd}
39
    - |
torgiren's avatar
torgiren committed
40
      if [[ -n "$(git status --porcelain)" ]]; then
41
42
        git add . &&
        git commit -m "update by gitlab-ci" &&
torgiren's avatar
torgiren committed
43
        git show &&
44
45
        git push
      fi
torgiren's avatar
torgiren committed
46
    - export pod=$(kubectl -n argocd get pods -l app.kubernetes.io/name=argocd-server -o name|head -n1)
torgiren's avatar
torgiren committed
47
48
    - test -n "$refreshapp" && kubectl -n argocd exec $pod -- sh -c "argocd login cd.argoproj.io --core; argocd app get $refreshapp --refresh"
    - stable=0; test -n "$waitapp" && for try in $(seq -w 1 120); do
torgiren's avatar
typo    
torgiren committed
49
        state=$(kubectl -n argocd get applications "$waitapp" -o template='{{.status.health.status}},{{.status.sync.status}},{{.spec.source.targetRevision}}');
torgiren's avatar
torgiren committed
50
        [ "$state" == "Healthy,Synced,$VERSION" ] && stable=$((stable+1)) || stable=0;
51
        echo "try $try/120. stable $stable/3. $state, desired $VERSION";
torgiren's avatar
torgiren committed
52
        [ $try -ge 120 ] && exit 1;
53
        [ $stable -ge 3 ] && break;
torgiren's avatar
typo    
torgiren committed
54
        sleep 5;
torgiren's avatar
torgiren committed
55
      done
torgiren's avatar
torgiren committed
56
57
58
59
60
61
62

.deploy-to-git-subapp:
  extends: .deploy-to-git
  variables:
    patch_cmd: |
      values=$(yq e '.spec.source.helm.values' $yamlfile| yq e '.'"$app"'.version="'$$VERSION'"' -)
      yq e -i '.spec.source.helm.values=strenv(values)' $yamlfile
torgiren's avatar
torgiren committed
63
64
65
66
67

.deploy-to-git-mainapp:
  extends: .deploy-to-git
  variables:
    patch_cmd: 'yq e -i ".spec.source.targetRevision=\"$$VERSION\"" $yamlfile'