Skip Job creation when project feature is disabled (eg: package registry)

Problem to solve

Skip Job creation when some project feature is enabled/disabled

Configuration

A simple gitlab-ci.yml with an example job that only gets created when the package registry is enabled for the project.

check_registry:
  script:
    - echo "Package Registry is enabled"
  rules:
    - if: '$CI_PACKAGE_REGISTRY_ENABLED == "true"'
      when: always
    - when: never

Versions

  • Self-managed
  • GitLab.com SaaS
  • Self-hosted Runners

Thanks in advance!
Best Regards.

Hi @Urien ,

Thanks for posting this, I like your idea and I’m sure there are others who could benefit from something like this.

Right now there is no predefined CI variable that could be used to determine whether or not a package registry (or other project feature) is available, so for the CI job rules: - if clause to work as desired/expected you have two options:

Immediate solution: Write a script or tool that goes through your GitLab projects, checks whether packages_enabled is true or false, and create a project level CI_PACKAGE_REGISTRY_ENABLED variable for each project set to true (when packages_enabled == true) and false (when packages_enabled == false.

You should be able to use the GitLab REST API in combination with the scripting or programming language of choice to accomplish this:

  1. Get all projects at the group, user, or instance (using admin account) level.
  2. Use Projects API to check if packages_enabled is true or false for each project
  3. Create CI_PACKAGE_REGISTRY_ENABLED CI variable for each project, set to true/false based on whether packages_enabled returned true or false

I suggest running this script on a schedule to update the variable if the package registry toggle is changed and to set the variable for newly created projects.

Long-term solution: Feature request to add $CI_PACKAGE_REGISTRY enabled boolean as a predefined CI job variable which could then be used for CI job rules. Having other project-level settings exposed as CI job variables (e.g. container registry enabled/disabled) could also be handy for different use cases. To make a feature proposal, use this issue template: https://gitlab.com/gitlab-org/gitlab/-/issues/new?issuable_template=Feature%20Proposal%20-%20lean

@gitlab-greg, thanks for the suggested workarounds… I am currently running a check on my jobs, but this should be more efficient. I am worried because it would mean adding a bunch of variables to the projects, though. Maybe just add when the feature is enabled would generate less cruft.

I will post the feature request.

Best regards.

Done!

Regards

1 Like