<p>I'm trying to setup auto deploy with Kubernetes on GitLab. I've successfully enabled Kubernetes integration in my project settings. </p>

<p>Well, the integration icon is green and when I click "Test Settings" I see "We sent a request to the provided URL":</p>

<p><a href="https://i.stack.imgur.com/5jjm1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5jjm1.png" alt="Kubernetes Integration"></a></p>

<p>My deployment environment is the Google Container Engine.</p>

<p>Here's the auto deploy section in my <code>gitlab-ci.yml</code> config:</p>

<pre><code>deploy:
  image: registry.gitlab.com/gitlab-examples/kubernetes-deploy
  stage: deploy
  script:
    - export
    - echo CI_PROJECT_ID=$CI_PROJECT_ID
    - echo KUBE_URL=$KUBE_URL
    - echo KUBE_CA_PEM_FILE=$KUBE_CA_PEM_FILE
    - echo KUBE_TOKEN=$KUBE_TOKEN
    - echo KUBE_NAMESPACE=$KUBE_NAMESPACE

    - kubectl config set-cluster "$CI_PROJECT_ID" --server="$KUBE_URL" --certificate-authority="$KUBE_CA_PEM_FILE"
    - kubectl config set-credentials "$CI_PROJECT_ID" --token="$KUBE_TOKEN"
    - kubectl config set-context "$CI_PROJECT_ID" --cluster="$CI_PROJECT_ID" --user="$CI_PROJECT_ID" --namespace="$KUBE_NAMESPACE"
    - kubectl config use-context "$CI_PROJECT_ID"
</code></pre>

<p>When I look at the results, the deploy phase fails. This is because all the <code>KUBE</code> variables are empty. </p>

<p>I'm not having much luck with the Kubernetes services beyond this point. Am I missing something?</p>

<p>As it turns out, the Deployment Variables will not materialise unless you have configured and referenced an Environment.</p>

<p>Here's what the <code>.gitlab-ci.yaml</code> file looks like with the <code>environment</code> keyword:</p>

<pre><code>deploy:
  image: registry.gitlab.com/gitlab-examples/kubernetes-deploy
  stage: deploy
  environment: production
  script:
    - export
    - echo CI_PROJECT_ID=$CI_PROJECT_ID
    - echo KUBE_URL=$KUBE_URL
    - echo KUBE_CA_PEM_FILE=$KUBE_CA_PEM_FILE
    - echo KUBE_TOKEN=$KUBE_TOKEN
    - echo KUBE_NAMESPACE=$KUBE_NAMESPACE

    - kubectl config set-cluster "$CI_PROJECT_ID" --server="$KUBE_URL" --certificate-authority="$KUBE_CA_PEM_FILE"
    - kubectl config set-credentials "$CI_PROJECT_ID" --token="$KUBE_TOKEN"
    - kubectl config set-context "$CI_PROJECT_ID" --cluster="$CI_PROJECT_ID" --user="$CI_PROJECT_ID" --namespace="$KUBE_NAMESPACE"
    - kubectl config use-context "$CI_PROJECT_ID"
</code></pre>
