Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to set environment variable within shell resources #754

Merged
merged 23 commits into from
Jul 8, 2022

Conversation

olblak
Copy link
Member

@olblak olblak commented Jul 5, 2022

Signed-off-by: Olblak me@olblak.com

Allow to set environment variable within shell resources

Fix #749

Because target shell resources, set the environment variable DRY_RUN, all other environment variable are hidden with the shell script.

Note that by default all environment variables are available for source and condition shell resource.

This affects multiple project relying on Github Action that update the PATH env variable with cached tool

Test

To test this pull request, you can run the following commands:

cp <to_package_directory>
go test

Additional Information

Tradeoff

Potential improvement

Signed-off-by: Olblak <me@olblak.com>
@olblak olblak added the resource-shell Resource of kind Shell label Jul 5, 2022
@olblak olblak marked this pull request as draft July 5, 2022 09:47
@olblak olblak marked this pull request as draft July 5, 2022 09:47
@olblak
Copy link
Member Author

olblak commented Jul 5, 2022

Manifest example

name: Shell example using environment variable

sources:
  default:
    name: Test source shell
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

conditions:
  default:
    name: Test source shell
    disablesourceinput: true
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    disablesourceinput: true
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

targets:
  default:
    name: Test source shell
    disablesourceinput: true
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    disablesourceinput: true
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

@olblak
Copy link
Member Author

olblak commented Jul 5, 2022

And the script example

#!/bin/sh 

echo "PATH: $PATH"
echo "DRY_RUN: $DRY_RUN"

olblak and others added 2 commits July 5, 2022 11:49
Signed-off-by: Olblak <me@olblak.com>
@olblak olblak marked this pull request as ready for review July 5, 2022 09:59
Copy link
Contributor

@jlevesy jlevesy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Most of my comments are style nits so feel free to ignore them ^^.

Let me know this this helps or not!

pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
@olblak
Copy link
Member Author

olblak commented Jul 5, 2022

Most of my comments are style nits so feel free to ignore them ^^.

Thanks, I am always open for some learning :)

olblak and others added 5 commits July 5, 2022 14:27
Co-authored-by: Julien Levesy <jlevesy@gmail.com>
Co-authored-by: Julien Levesy <jlevesy@gmail.com>
Co-authored-by: Julien Levesy <jlevesy@gmail.com>
Co-authored-by: Julien Levesy <jlevesy@gmail.com>
Signed-off-by: Olblak <me@olblak.com>
@olblak
Copy link
Member Author

olblak commented Jul 5, 2022

I now realize that I didn't address a weird behavior:

  • By default source and condition inehrit from all environment variable from the updatecli process
  • By default target has almost no variable ["PWD","DRY_RUN","SHLVL"]

@dduportal Do you have any opinions on which behavior should be the default for all stages

@dduportal
Copy link
Contributor

I now realize that I didn't address a weird behavior:

* By default `source` and `condition` inehrit from all environment variable from the updatecli process

* By default `target` has almost no variable ["PWD","DRY_RUN","SHLVL"]

@dduportal Do you have any opinions on which behavior should be the default for all stages

I can't remember why I differentiate these behaviors. I fear that I might have been lazy for the target.

Is it ok to keep the behavior that you introduced in this PR also for target (which adding the DRY_RUN variable in the list of env to be passed, overriding the value with a warning if it already exists)

@dduportal
Copy link
Contributor

Manifest example

name: Shell example using environment variable

sources:
  default:
    name: Test source shell
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

conditions:
  default:
    name: Test source shell
    disablesourceinput: true
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    disablesourceinput: true
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

targets:
  default:
    name: Test source shell
    disablesourceinput: true
    kind: shell
    spec:
      command: ./examples/scripts.d/env.sh

  default2:
    name: Test source shell
    kind: shell
    disablesourceinput: true
    spec:
      command: ./examples/scripts.d/env.sh
      environments:
        - name: PATH
          inherit: true

Looks good!

WDYT to use the same configuration behavior as Docker?

environments:
  - name: PATH # Only name, eg. 'if value == ""' means "inherit"
  - name: GITHUB_TOKEN
     value: "SuperSecretTokenExposed" # Custom value that could be templatized from within updatecli

Also, I wonder if the attribute name: could not be key: or variable: ? (but nitpicking for this one)

?

@olblak
Copy link
Member Author

olblak commented Jul 6, 2022

I can't remember why I differentiate these behaviors. I fear that I might have been lazy for the target.
Is it ok to keep the behavior that you introduced in this PR also for target (which adding the DRY_RUN variable in the list of env to be passed, overriding the value with a warning if it already exists)

I guess you mean the source/condition :D I agree it doesn't make sense to us the DRY_RUN env variable for source and condition but if we don't do it then by default then the default behavior in the "os" golang package is to inherit all environment variable from the updatecli process. So I am gonna add "DRY_RUN" to source and condition as well

Also, I wonder if the attribute name: could not be key: or variable: ? (but nitpicking for this one)

TBH I hesitated implementing the inherit key, and I agree with you it doesn't really add value, so I am gonna remove it
Regarding variable key/variable instead of name/value, It's a personal taste but I prefer the name/value so unless addition input, I am gonna keep it :D

lemeurherve
lemeurherve previously approved these changes Jul 7, 2022
Copy link
Member

@lemeurherve lemeurherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit only :)

TBH I hesitated implementing the inherit key, and I agree with you it doesn't really add value, so I am gonna remove it

🎉

pkg/plugins/resources/shell/command.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environment.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environment.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/environments.go Outdated Show resolved Hide resolved
pkg/plugins/resources/shell/main.go Outdated Show resolved Hide resolved
olblak and others added 3 commits July 7, 2022 13:37
Co-authored-by: Damien Duportal <damien.duportal@gmail.com>
Co-authored-by: Hervé Le Meur <91831478+lemeurherve@users.noreply.github.com>
Co-authored-by: Hervé Le Meur <91831478+lemeurherve@users.noreply.github.com>
olblak and others added 8 commits July 7, 2022 13:37
Co-authored-by: Hervé Le Meur <91831478+lemeurherve@users.noreply.github.com>
Co-authored-by: Hervé Le Meur <91831478+lemeurherve@users.noreply.github.com>
Rename ToStringArray to ToStringSlice

Update validate function to update

Signed-off-by: Olblak <me@olblak.com>
Signed-off-by: Olblak <me@olblak.com>
Signed-off-by: Olblak <me@olblak.com>
Signed-off-by: Olblak <me@olblak.com>
Signed-off-by: Olblak <me@olblak.com>
Signed-off-by: Olblak <me@olblak.com>
@olblak
Copy link
Member Author

olblak commented Jul 7, 2022

After the final evaluation, I would like to trigger a new release, as this PR is blocking several of my pipelines

@olblak olblak enabled auto-merge (squash) July 8, 2022 08:28
@olblak olblak disabled auto-merge July 8, 2022 08:28
@olblak olblak added the enhancement New feature or request label Jul 8, 2022
@olblak
Copy link
Member Author

olblak commented Jul 8, 2022

Thanks everybody for the constructive reviews

@olblak olblak merged commit 9e8b7ee into updatecli:main Jul 8, 2022
@jetersen
Copy link
Member

jetersen commented Jul 20, 2022

oh damn, could really use a release of this 😅

Thought this was just supported.

This is massively messing with variables in pwsh cannot even access $ENV:TEMP cannot access $ENV:PATH either

@jetersen
Copy link
Member

jetersen commented Jul 20, 2022

Workaround :( jenkinsci/bom@b359dd5 (#1290)

@lemeurherve
Copy link
Member

@jetersen FTR, it's now available in the v0.28.0 release

@jetersen
Copy link
Member

Thanks for reminding me that I need to setup releases watch on this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resource-shell Resource of kind Shell
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Shell resource don't inherit environment variable
5 participants