<p>I am trying to deploy to kubernetes using Gitlab CICD. No matter what I do, <code>kubectl apply -f helloworld-deployment.yml --record</code> in my <code>.gitlab-ci.yml</code> always returns that the deployment is unchanged:</p>

<pre><code>$ kubectl apply -f helloworld-deployment.yml --record
 deployment.apps/helloworld-deployment unchanged
</code></pre>

<p>Even if I change the tag on the image, or if the deployment doesn't exist at all. However, if I run <code>kubectl apply -f helloworld-deployment.yml --record</code> from my own computer, it works fine and updates when a tag changes and creates the deployment when no deployment exist. Below is my <code>.gitlab-ci.yml</code> that I'm testing with:</p>

<pre><code>image: docker:dind
services:
    - docker:dind

stages:
    - deploy

deploy-prod:
    stage: deploy
    image: google/cloud-sdk
    environment: production
    script:
        - kubectl apply -f helloworld-deployment.yml --record
</code></pre>

<p>Below is <code>helloworld-deployment.yml</code>:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
    name: helloworld-deployment
spec:
    replicas: 2
    selector:
        matchLabels:
            app: helloworld
    template:
        metadata:
            labels:
                app: helloworld
        spec:
            containers:
                - name: helloworld
                  image: registry.gitlab.com/repo/helloworld:test
                  imagePullPolicy: Always
                  ports:
                  - containerPort: 3000
            imagePullSecrets:
                - name: regcred
</code></pre>

<p><b>Update:</b></p>

<p>This is what I see if I run <code>kubectl rollout history deployments/helloworld-deployment</code> and there is no existing deployment:</p>

<pre><code>Error from server (NotFound): deployments.apps "helloworld-deployment" not found
</code></pre>

<p>If the deployment already exists, I see this:</p>

<pre><code>REVISION  CHANGE-CAUSE
1         kubectl apply --filename=helloworld-deployment.yml --record=true
</code></pre>

<p>With only one revision.</p>

<p>I did notice this time that when I changed the tag, the output from my Gitlab Runner was:</p>

<pre><code>deployment.apps/helloworld-deployment configured
</code></pre>

<p>However, there were no new pods. When I ran it from my PC, then I did see new pods created.</p>

<p><b>Update:</b></p>

<p>Running <code>kubectl get pods</code> shows two different pods in Gitlab runner than I see on my PC.</p>

<p>I definitely only have one kubernetes cluster, but <code>kubectl config view</code> shows <i>some</i> differences (the server url is the same). The output for <code>contexts</code> shows different namespaces. Does this mean I need to set a namespace either in my <code>yml</code> file or pass it in the command? Here is the output from the Gitlab runner:</p>

<pre><code> apiVersion: v1
 clusters:
 - cluster:
     certificate-authority-data: DATA+OMITTED
     server: URL
   name: gitlab-deploy
 contexts:
 - context:
     cluster: gitlab-deploy
     namespace: helloworld-16393682-production
     user: gitlab-deploy
   name: gitlab-deploy
 current-context: gitlab-deploy
 kind: Config
 preferences: {}
 users:
 - name: gitlab-deploy
   user:
     token: [MASKED]
</code></pre>

<p>And here is the output from my PC:</p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: URL
contexts:
- context:
    cluster: do-nyc3-helloworld
    user: do-nyc3-helloworld-admin
  name: do-nyc3-helloworld
current-context: do-nyc3-helloworld
kind: Config
preferences: {}
users:
- name: do-nyc3-helloworld-admin
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - kubernetes
      - cluster
      - kubeconfig
      - exec-credential
      - --version=v1beta1
      - --context=default
      - VALUE
      command: doctl
      env: null
</code></pre>

<p>It looks like Gitlab adds their own <a href="https://docs.gitlab.com/ee/user/project/clusters/#deployment-variables" rel="nofollow noreferrer">default for namespace</a>:</p>

<pre><code>&lt;project_name&gt;-&lt;project_id&gt;-&lt;environment&gt;
</code></pre>

<p>Because of this, I put this in the metadata section of helloworld-deployment.yml:</p>

<pre><code>namespace: helloworld-16393682-production
</code></pre>

<p>And then it worked as expected. It was deploying before, but <code>kubectl get pods</code> didn't show it since that command was using the <code>default</code> namespace.</p>

<p>Since Gitlab use a custom namespace you need to add a namespace flag to you command to display your pods:</p>

<p><code>kubectl get pods -n helloworld-16393682-production</code></p>

<p>You can set the default namespace for kubectl commands. See <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#setting-the-namespace-preference" rel="nofollow noreferrer">here</a>.</p>

<blockquote>
  <p>You can permanently save the namespace for all subsequent kubectl commands in that contex</p>
</blockquote>

<p>In your case it could be:</p>

<pre><code>kubectl config set-context --current --namespace=helloworld-16393682-production
</code></pre>

<p>Or if you are using <a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" rel="nofollow noreferrer">multiples cluster</a>, you can switch between namespaces using:</p>

<pre><code>kubectl config use-context helloworld-16393682-production
</code></pre>

<p>In this <a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-context-and-configuration" rel="nofollow noreferrer">link</a> you can see a lot of useful commands and configurations.</p>

<p>I hope it helps! =)</p>
