3

I'm creating a template for all the deploy jobs, and I need to be able to use needs keyword with different values for each deploy job, but GitLab CI, as far as I know, does not support using variable in needs keyword. Is there any workaround?

This is what I need to do:

# Deploy template
.deploy:
  stage: deploy
  only:
    - develop
  tags:
    - deploy
  needsL ["build:$PROJECT_NAME"]


# Deploy jobs

deploy:package1:
  extends: .deploy
  variables:
    PROJECT_NAME: 'package1'
  #needs: ['build:package1']

deploy:package2:
  extends: .deploy
  variables:
    PROJECT_NAME: 'package2'
  #needs: ['build:package2']
Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
Akram Rabie
  • 473
  • 5
  • 11

1 Answers1

3

You can't do this. needs: will not support variables.

However, if the template you're making does not contain the job it depends on, the best approach is probably to not use needs: at all, otherwise you greatly increase the likelihood that including your template will cause an invalid yaml file.

So, your options would be either to (1) include the jobs you depend on in the same template, then designate needs: explicitly or (2) Rely on users to provide the needs: key in the deploy job if they want.

For example, a user can do this:

include:
  - "your template"

# job originates in the project configuration
my_project_jobs:
  script: "..."


your_deploy_template_job:
  needs: ["my_project_job"]  # add the key to the included template job

Or if you provide both jobs in your pipeline configuration, you can use some rules: to keep the jobs from running, and let users enable them and override their script configurations to implement builds.

# your template yaml
your_template_build_job:package1
  rules:
    - if: '$PACKAGE1_ENABLED'
      when: on_success
    - when: never


your_template_deploy_job:package1
  rules:
    - if: '$PACKAGE1_ENABLED'
  needs: [your_template_build_job:package1]
  # ...

Then a user might just do this:

# user project yaml
include:
  - "your template"

variables:
  PACKAGE1_ENABLED: true

your_template_build_job:package1
  script: "my project build script"

When the user doesn't explicitly enable a job, neither the build nor deploy job will be in the pipeline configuration. However, they only need to enable the build job (by variable) and the needs: configuration for the deploy job will already be in place.

Neither of these approaches are particularly perfect for very flexible use of templates, unfortunately. But there may be another option...

Workaround: Dynamic child pipelines

As a possible workaround, users could use dynamic child pipelines to generate an entire pipeline configuration with correct needs: based on a minimal configuration. Almost anything is possible with dynamic child pipelines because you can generate the YAML programmatically on-the-fly, though, it may be more trouble than it's worth.

sytech
  • 29,298
  • 3
  • 45
  • 86