deploy 3.99 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done

export DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

source "$DEPLOY_ROOT_DIR/src/common.bash"

14 15 16 17 18 19
if [ "${INGRESS_ENABLE_SSL:-true}" = "true" ]; then
  export INGRESS_SSL_ANNOTATION='kubernetes.io/tls-acme: "true"'
else
  export INGRESS_SSL_ANNOTATION=''
fi

20
if [ ! -z "${IMAGE_PULL_SECRET}" ]; then
21 22 23
  export  IMAGE_PULL_SECRET_SNIPPET="imagePullSecrets:
      - name: $IMAGE_PULL_SECRET
"
24 25 26 27
else
  export  IMAGE_PULL_SECRET_SNIPPET=''
fi

28 29
export CONTAINER_HTTP_PORT="${CONTAINER_HTTP_PORT:-$CONTAINER_HTTP_PORT}"

30 31 32 33 34 35 36
if [[ -z "$CI_ENVIRONMENT_URL" ]]; then
  echo "Reading CI_ENVIRONMENT_URL from .gitlab-ci.yml..."
  CI_ENVIRONMENT_URL="$(ruby -ryaml -e 'puts YAML.load_file(".gitlab-ci.yml")[ENV["CI_BUILD_NAME"]]["environment"]["url"]')"
  CI_ENVIRONMENT_URL="$(eval echo "$CI_ENVIRONMENT_URL")"
  echo "CI_ENVIRONMENT_URL: $CI_ENVIRONMENT_URL"
fi

Kamil Trzcinski's avatar
Kamil Trzcinski committed
37 38 39
ensure_deploy_variables
create_kubeconfig

40 41 42
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_URL}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/http:\/\//}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/https:\/\//}"
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43

44 45 46 47 48
cat <<EOF | kubectl apply -f -
kind: Namespace
apiVersion: v1
metadata:
  name: $KUBE_NAMESPACE
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
49 50
EOF

Gerry Gunzenhauser's avatar
Gerry Gunzenhauser committed
51 52 53
export DEPLOYMENT_YML_FILE="${DEPLOYMENT_YML_FILE:-deployment.yml}"
if [ -e "${DEPLOYMENT_YML_FILE}" ]; then
  source <(echo 'kubectl apply -n $KUBE_NAMESPACE -f - <<EOFOFDEPLOYMENTYML'; cat "${DEPLOYMENT_YML_FILE}"; echo EOFOFDEPLOYMENTYML)
54 55 56
else

  cat <<EOF | kubectl apply -n $KUBE_NAMESPACE -f -
57 58 59 60 61 62 63
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: $CI_ENVIRONMENT_SLUG
  namespace: $KUBE_NAMESPACE
  labels:
    app: $CI_ENVIRONMENT_SLUG
Kamil Trzcinski's avatar
Kamil Trzcinski committed
64 65
    pipeline_id: "$CI_PIPELINE_ID"
    build_id: "$CI_BUILD_ID"
66 67
spec:
  replicas: 1
68
  revisionHistoryLimit: ${DEPLOYMENT_HISTORY_LIMIT:-3}
69 70 71 72
  template:
    metadata:
      labels:
        name: $CI_ENVIRONMENT_SLUG
73
        app: $CI_ENVIRONMENT_SLUG
74
    spec:
75
      $IMAGE_PULL_SECRET_SNIPPET
76 77 78 79
      containers:
      - name: app
        image: $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG
        imagePullPolicy: Always
Kamil Trzcinski's avatar
Kamil Trzcinski committed
80 81 82 83 84
        env:
        - name: CI_PIPELINE_ID
          value: "$CI_PIPELINE_ID"
        - name: CI_BUILD_ID
          value: "$CI_BUILD_ID"
85 86
        ports:
        - name: web
87
          containerPort: $CONTAINER_HTTP_PORT
88 89 90
        livenessProbe:
          httpGet:
            path: /
91
            port: $CONTAINER_HTTP_PORT
92 93 94 95 96
          initialDelaySeconds: 15
          timeoutSeconds: 15
        readinessProbe:
          httpGet:
            path: /
97
            port: $CONTAINER_HTTP_PORT
98 99
          initialDelaySeconds: 5
          timeoutSeconds: 3
Kamil Trzcinski's avatar
Kamil Trzcinski committed
100
---
101 102 103 104 105 106 107
apiVersion: v1
kind: Service
metadata:
  name: $CI_ENVIRONMENT_SLUG
  namespace: $KUBE_NAMESPACE
  labels:
    app: $CI_ENVIRONMENT_SLUG
Kamil Trzcinski's avatar
Kamil Trzcinski committed
108 109
    pipeline_id: "$CI_PIPELINE_ID"
    build_id: "$CI_BUILD_ID"
110 111 112
spec:
  ports:
    - name: web
113
      port: $CONTAINER_HTTP_PORT
114 115 116 117 118 119 120 121 122 123 124
      targetPort: web
  selector:
    name: $CI_ENVIRONMENT_SLUG
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: $CI_ENVIRONMENT_SLUG
  namespace: $KUBE_NAMESPACE
  labels:
    app: $CI_ENVIRONMENT_SLUG
Kamil Trzcinski's avatar
Kamil Trzcinski committed
125 126
    pipeline_id: "$CI_PIPELINE_ID"
    build_id: "$CI_BUILD_ID"
127 128
  annotations:
    kubernetes.io/ingress.class: "nginx"
129
    $INGRESS_SSL_ANNOTATION
130 131 132 133 134 135 136 137 138 139 140 141
spec:
  tls:
  - hosts:
    - $CI_ENVIRONMENT_HOSTNAME
    secretName: ${CI_ENVIRONMENT_SLUG}-tls
  rules:
  - host: $CI_ENVIRONMENT_HOSTNAME
    http:
      paths:
      - path: /
        backend:
          serviceName: $CI_ENVIRONMENT_SLUG
142
          servicePort: $CONTAINER_HTTP_PORT
143
EOF
144
fi
Kamil Trzcinski's avatar
Kamil Trzcinski committed
145

Kamil Trzcinski's avatar
Kamil Trzcinski committed
146
echo "Waiting for deployment..."
Kamil Trzcinski's avatar
Kamil Trzcinski committed
147
kubectl rollout status -n "$KUBE_NAMESPACE" -w "deployment/$CI_ENVIRONMENT_SLUG"
Kamil Trzcinski's avatar
Kamil Trzcinski committed
148

Kamil Trzcinski's avatar
Kamil Trzcinski committed
149 150
echo "Application is accessible at: ${CI_ENVIRONMENT_URL}"
echo ""