<p>I'm trying to set up a full CI/CD pipeline for a SpringBoot application, starting from a GitLab repository (see <a href="https://gitlab.com/pietrom/clock-api" rel="nofollow noreferrer">https://gitlab.com/pietrom/clock-api</a>) and automatically deploying to a Kubernetes Cluster backed by Google Cloud Platform.</p>

<p>My pipeline works quite fine (the app is built, it is packaged as a Docker image, the image is published on my project registry and containers are started for both <code>staging</code> and <code>production</code> environment), except for a detail: <code>Operation/Environments</code> page shows me both environments, with the following warning:</p>

<pre><code>Kubernetes deployment not found
To see deployment progress for your environments, make sure your deployments are in Kubernetes namespace &lt;projectname&gt;, and annotated with app.gitlab.com/app=$CI_PROJECT_PATH_SLUG and app.gitlab.com/env=$CI_ENVIRONMENT_SLUG.
</code></pre>

<p>I googled around a bit but I can't resolve this problem: my <code>deployment.yml</code> contains requested annotation, both for <em>deployment</em> and <em>pod</em>:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: clock-api-ENVIRONMENT
  annotations:
    app.gitlab.com/app: "PROJECT_PATH_SLUG"
    app.gitlab.com/env: "ENVIRONMENT"

spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: ENVIRONMENT
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8080"
        prometheus.io/path: "/actuator/prometheus"
        app.gitlab.com/app: "PROJECT_PATH_SLUG"
        app.gitlab.com/env: "ENVIRONMENT"
    spec:
      containers:
        - name: clock-api-ENVIRONMENT
          image: registry.gitlab.com/pietrom/clock-api
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
      imagePullSecrets:
        - name: registry.gitlab.com
</code></pre>

<p><code>PROJECT_PATH_SLUG</code> and <code>ENVIRONMENT</code> placeholder are substituded (using <code>sed</code>) during pipeline execution with values provided by GitLab infrastructure (<code>$CI_PROJECT_PATH_SLUG</code> and <code>$CI_ENVIRONMENT_SLUG</code>, respectively) and I can see the expected values in my GCP console, but GitLab integration does not seem to work.</p>

<p>I'm missing something but I can't figure what differences there are between my deployment setup and the official documentation available <a href="https://docs.gitlab.com/ee/user/project/deploy_boards.html" rel="nofollow noreferrer">here</a>.</p>

<p>Thanks in advance for your help!</p>

<p>This is also an important part:</p>

<blockquote>
  <p>make sure your deployments are in Kubernetes namespace</p>
</blockquote>

<p>GitLab tries to manage namespaces in the attached Kubernetes cluster - creates a new namespace for every new GitLab project. It generates namespace from project name and project id.</p>

<p>Sometimes GitLab fails to create namespace, for example when cluster is added <strong>after</strong> project is created. It's probably a bug, and here is how they overcome it <a href="https://gitlab.com/gitlab-org/gitlab-ce/blob/691d88b71d51786983b823207d876cee7c93f5d4/lib/gitlab/ci/templates/Jobs/Deploy.gitlab-ci.yml#L506" rel="nofollow noreferrer">in AutoDevOps</a>:</p>

<pre><code>  function ensure_namespace() {
    kubectl get namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
  }
</code></pre>

<p>This env var <code>$KUBE_NAMESPACE</code> - is defined by GitLab automatically, as well as many other Kubernetes-related variables:
<a href="https://docs.gitlab.com/ee/user/project/clusters/#deployment-variables" rel="nofollow noreferrer">https://docs.gitlab.com/ee/user/project/clusters/#deployment-variables</a></p>

<p>Then GitLab relies on this namespace internally and uses for various UI pages of the project, including Operations/Environment. So if you follow their convention, and apply your Kubernetes deployment to this namespace, GitLab will see your application.</p>

<p>In our work we go one step further and at the beginning of a job even <a href="https://gitlab.com/softmill/template-gitlab-ci/blob/2656a064c8e92be0f93e036e0f74f79b39d8d1fb/.gitlab-ci-kube.yml#L128" rel="nofollow noreferrer">set this namespace as the default</a>.  </p>

<pre><code>kubectl config set-context --current --namespace="$KUBE_NAMESPACE"
</code></pre>

<p>Then all further <code>kubectl</code> commands will by default use this namespace.</p>
