woodpecker-ci / woodpecker

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

`CI_STEP_*` environment variables do not return values #1760

Open rubenelshof opened 1 year ago

rubenelshof commented 1 year ago

Component

server

Describe the bug

While testing the ntfy plugin to send notifications after pipeline failure I wanted to use CI_STEP_NAME but it doesn't return a value. The other CISTEP* variables are not returning anything either.

Here a example pipeline. I have added CI_WORKFLOW_NAME which does work.

pipeline:
  deploy:
    image: alpine:latest
    commands:
      - exit 1

  notify_on_failure:
    image: codeberg.org/l-x/woodpecker-ntfy
    settings:
      url: https://ntfy.example.com/ci
      token:
        from_secret: ntfy_token
      title: Build failure
      priority: urgent
      icon: https://woodpecker-ci.org/img/logo.svg
      tags: x, robot
      message: >
            Workflow: ${CI_WORKFLOW_NAME}
            Step name: ${CI_STEP_NAME}
            Step status: ${CI_STEP_STATUS}
            Step started: ${CI_STEP_STARTED}
            Step finished:  ${CI_STEP_FINISHED}
    when:
      status: [ failure ]

System Info

Version: next-02913fa7

Additional context

No response

Validations

anbraten commented 1 year ago

I think the issue is that those value are set by the agent when the step is started, but the values you are using are evaluated when parsing the config.

rubenelshof commented 1 year ago

I dont think I quite get what you mean. Could you elaborate?

anbraten commented 1 year ago

There are different "stages" in which the variable placeholder you define are replaced. The one you are trying to use here is not set at that stage. Its set as env var inside your container, but not before. Maybe we can improve this somehow, but currently I have no concrete idea

rubenelshof commented 1 year ago

I see. Reason I thought I could use it this way was this issue #1681 & follow up PR. Additionally because the variables are on this list I thought they would be available.

I will just use the workflow name for now.

lonix1 commented 1 year ago

For me, CI_STEP_NAME shows something like wp_s6vj82zh9s2h8b33opzqk45sut7_0_step_3 instead of the name (e.g. build).

I am unsure whether that is the same problem or not? (Because for me it is not blank.) I am using that env var with a different plugin though.

s3lph commented 1 year ago

I'm experiencing the same issue as @lonix1: Some kind of autogenerated name is used, even if a name is set in the woodpecker.yml file. However, I'm observing this behavior with regular steps (i.e. not using a plugin). Definitely sounds like a bug to me.

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

The affected steps are being executed on a docker agent.

itsTurnip commented 2 months ago

Reproduced this behavior on v2.5.0. There's empty CI_STEP_NAME, CI_STEP_NUMBER always equals zero, no CI_STEP_URL following to the current step (the link is directing to the whole pipeline) and step with failure: ignore setting is reported as success.

Workflow:

when:
  - event: push
    branch: main

steps:
  failing-step:
    image: alpine
    commands:
      - printenv
      - exit 1
    failure: ignore
  after-fail:
    image: alpine
    commands:
      - printenv

Output for failing:

+ printenv
...
CI_STEP_FINISHED=1718965573
CI_STEP_NAME=
CI_STEP_NUMBER=0
CI_STEP_STARTED=1718965571
CI_STEP_STATUS=success
CI_STEP_URL=https://ci.example.com/repos/1/pipeline/25
...

Output for after-fail step:

+ printenv
...
CI_STEP_FINISHED=1718965578
CI_STEP_NAME=
CI_STEP_NUMBER=0
CI_STEP_STARTED=1718965571
CI_STEP_STATUS=success
CI_STEP_URL=https://ci.example.com/repos/1/pipeline/25
...

There's another kind of question: as I can understand some variables (CI_STEP_STARTED, CI_STEP_FINISHED) belong to the previous step, but in documentation it is described as variables for the current step.