woodpecker-ci / woodpecker

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

Custom environment variables don't work with string substitution #3983

Open june128 opened 3 months ago

june128 commented 3 months ago

Component

server, agent

Describe the bug

Setting custom environment variables using the environment key can't be used via string substitution.

Steps to reproduce

  1. Define a workflow containing the following step:
    - name: test
     image: debian
     environment:
       MY_COOL_TEST_VAR: test
     commands:
       - echo "$CI $MY_COOL_TEST_VAR ${CI} ${MY_COOL_TEST_VAR}"
  2. Run the workflow
  3. Observe the output in the Web interface

Expected behavior

The output being:

+ echo "$CI $MY_COOL_TEST_VAR test woodpecker "
woodpecker test test woodpecker 

What's actually being output:

+ echo "$CI $MY_COOL_TEST_VAR woodpecker "
woodpecker test woodpecker 

System Info

{"source":"https://github.com/woodpecker-ci/woodpecker","version":"2.7.0"}

Additional context

Woodpecker running on NixOS. Config available here.

Validations

qwerty287 commented 3 months ago

This is the expected behavior and not a bug.

Our current workflow of substitution does not allow to do something like this, it can only work with vars that are equal for the whole workflow.

zc-devs commented 3 months ago

Escape it:

skip_clone: true

steps:
  echo-envs:
    image: alpine
    environment:
      MY_COOL_TEST_VAR: test
    commands:
      - echo "Workspace is $$CI_WORKSPACE"
      - echo "My cool variable is $$MY_COOL_TEST_VAR"
+ echo "Workspace is $CI_WORKSPACE"
Workspace is /woodpecker/src/gitea.example.com/wp/test
+ echo "My cool variable is $MY_COOL_TEST_VAR"
My cool variable is test

or use scripts.

june128 commented 3 months ago

@qwerty287 Ahh fair enough, that's unfortunate. I guess having this clarified in the docs would be a good idea and this issue can be considered a feature request then.

june128 commented 3 months ago

@zc-devs But I want to have the value of MY_COOL_TEST_VAR, not the variable name. How does escaping it help me then?

zc-devs commented 3 months ago

values

What is this then?

june128 commented 3 months ago

@zc-devs Yeah, it works when using regular environment variables, but it doesn't work with string substitution (${MY_COOL_TEST_VAR}). For commands I can of course just use regular environment variables, however I only used the commands key as a simple example. A use case I have in particular is using the value of a custom environment variable in entrypoint, where only string substitution is available. There one then just can't use custom environment variables.

june128 commented 3 months ago

A friend pointed out a workaround for this problem. One can abuse the matrix key for setting values, which then can be accessed using string substitution. Not perfect, but it works.

It might be nice to have a lightweight matrix key named environment to facilitate global environment variables. However this is then another feature request.

anbraten commented 3 months ago

It should work when you try to escape that one as well: $${MY_COOL_TEST_VAR}

june128 commented 3 months ago

@anbraten That seems to work only in shells, but not if you want to use the variable in an entrypoint without a shell directly.

qwerty287 commented 2 months ago

Could be solved much more easily by doing something like https://github.com/woodpecker-ci/woodpecker/discussions/2473#discussioncomment-10129727 I think