woodpecker-ci / woodpecker

Woodpecker is a simple, yet powerful CI/CD engine with great extensibility.
https://woodpecker-ci.org
Apache License 2.0
4.19k stars 363 forks source link

Global `environment` option to set env variables across all steps #3661

Open Andre601 opened 5 months ago

Andre601 commented 5 months ago

Clear and concise description of the problem

Right now, there only seems to be a way to define global env variables for the entire CI, rather than a single pipeline itself. In addition is this from what I understand only something an admin can do, meaning users on a shared instance may not have a way to easily define an environment that is accessible across all steps (See "Alternatives" for the ones I know of and their (mostly) cons).

Suggested solution

A global environment option should be implemented, which allows you to define the environment variables you want to use across all steps.

I'm unsure if this global one should be overridable by a step-level environment setting with the same name.

Example:

when:
  - event: push

environment:
  FOO: BAR # Set a global env here.

steps:
  - name: 'echo'
    image: bash
    commands:
      - echo ${FOO}
  - name: 'echo again'
    image: bash
    environment:
      FOO: NOT_BAR # Should this be possible?
    commands:
      - echo ${FOO}

Alternative

From what I understand and gathered are there these possible alternatives, which all have some downsides to them:

Additional context

No response

Validations

stevenroose commented 4 weeks ago

I would like an option like this. I am using a custom docker image with a hash tag and I repeat it many times. I'd like to have a way to have the image description just once and then use image: $TEST_IMAGE in my steps.

qwerty287 commented 4 weeks ago

@stevenroose therefore you should use anchors: https://woodpecker-ci.org/docs/usage/advanced-usage#anchors--aliases

6543 commented 4 weeks ago

I would not add this as this will make all plugins to steps if set ... and using ancors is just fine.

you could efen define a step template as ancor and overwrite things that differ ...

6543 commented 4 weeks ago

If other maintainers are ok with it I'll going to close this as no fix.

As for @Andre601 all building blocks to achive your desired behaviour are already here, if it lacks of documentation, please point me to where we could improve that :)

Andre601 commented 4 weeks ago

I certainly wouldn't even know where in the docs I could look this stuff up to begin with, so that's the first problem there is for this.

Maybe it would be good to work on a cookbook page holding such examples people would use. If that is a thing already should it be presented more prominently.

I still believe global env vars to set is a feature many would benefit from.

lo48576 commented 3 weeks ago

The reason I +1'ed this issue is, environment variable would be easier to concatenate with other strings. Not necessary to be an envvar, but it looks harder (or maybe impossible?) to do it on plain YAML and anchors.

# without envvar
variables:
  rust_image_base: &rust_image_baes: 'rust'
  rust_msrv: '1.81.0'
  rust_msrv_image: &rust_msrv_image 'rust:1.81.0'
  rust_msrv_image_slim: &rust_msrv_image 'rust:1.81.0-slim'

# with envvar:
environment:
  RUST_IMAGE_BASE: rust
  RUST_MSRV: 1.81.0
variables:
  rust_msrv_image: ${RUST_IMAGE_BASE}:${RUST_MSRV}
  rust_msrv_image_slim: ${RUST_IMAGE_BASE}:${RUST_MSRV}-slim

The inability to make the version a variable (RUST_MSRV in the example) is not critical in my case but is frustrating and error prone.

6543 commented 3 weeks ago

ah you want to have them added to the metadata before parsing the whole config ... like done with the CI_* variables ... ... still that should move towards #2254 then ...