Skip to content

Check if Kubernetes integration is active in 'only' and 'except' conditions

Description

With Auto DevOps (#35712 (closed)) we want to introduce a single .gitlab-ci.yml that works for all projects. This includes deploy to a Kubernetes cluster, but there are many cases where this is not configured, and so the deploy would fail.

To prevent this, a change in the .gitlab-ci.yml syntax is needed. It adds capabilities to select if a job should be executed or not if specific environment variables are set, extending only and except. This is good since it allows to avoid creating the job if conditions are not met, saving a lot of runner resources.

Old description
For Auto DevOps, we want to make Kubernetes deploy actions optional, so that we can have a single `.gitlab-ci.yml` that replaces Auto Deploy, and works for all projects, even if they don't have a k8s cluster. One way is to add a capability to filter jobs using `only` and `except` based on existence (or value) of environment variables such as `KUBECONFIG`, which is only present when the kubernetes service is enabled (and thus we know we have a deployable platform available). Since we already expect refs (branch names or tags) or special keywords on the `only` and `except`, we could possibly detect a special symbol like `$` to then say we're checking for a variable to exist. Thus, we could do something like:
production:
  stage: production
  script:
    - some deploy script
  environment:
    name: production
    url: http://$CI_PROJECT_PATH_SLUG.$KUBE_DOMAIN
  when: manual
  only:
    - master
    - $KUBECONFIG

But it might be better to add a new keyword like condition, and allow more complicated logic like condition: $DEPLOY_STRATEGY==prod. Since this is still fundamentally affecting which jobs get instantiated, this condition should be a sub-keyword of only. e.g.:

production:
  stage: production
  script:
    - some deploy script
  environment:
    name: production
    url: http://$CI_PROJECT_PATH_SLUG.$KUBE_DOMAIN
  when: manual
  only:
    - master
    - condition: $DEPLOY_STRATEGY==prod

I don't really like that it's in the array though, especially because the array otherwise is a logical OR of all it's array entries, but we want this to be a logical AND.

Proposal

Proposal is to implement a new keyword under only and except, that specifically checks if Kubernetes is available.

job:
  only:
    refs:
      - master
      - tags
    kubernetes: active

It checks if a Kubernetes cluster has been configured in as an integration service for the specific project, and if the integration is active.

A more generic solution has been moved to #37397 (closed).

Links / references

Feature checklist

Make sure these are completed before closing the issue, with a link to the relevant commit.

Edited by Fabio Busatto