ssh-action icon indicating copy to clipboard operation
ssh-action copied to clipboard

Feature request: Nicer way to pass all ENV variables to script

Open AnderUstarroz opened this issue 3 years ago • 11 comments

Having to define one by one all github secrets on env and then pass them one by one to envs is cumbersome. Ideally adding a flag for passing all github secrets down to the script.

AnderUstarroz avatar Jan 22 '22 16:01 AnderUstarroz

@AnderUstarroz OK. Got it.

appleboy avatar Jan 22 '22 23:01 appleboy

Thanks @appleboy other than this is the best SSH package for Github actions I have found. But with easier/cleaner ENV management would be perfect.

For instance I have a project using about 20 Env variables and Docker picks the right ones just by filtering the needed ones with the flag --env-list:

obs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script_stop: true
          export_all_envs: true    
          script: |
            docker run --env-list ./env.list MY_IMAGE:latest

Where export_all_envs or all_envs Exports al github envs and secrets down to the script so they can be used like regular environment variables:

echo $MY_ENV

Currently doing something like this implies a lot of manual work defining every single variable one by one.

AnderUstarroz avatar Jan 23 '22 06:01 AnderUstarroz

The quickest way to test if the feature works would be just by printing the env vars with printenv. It should display all github secrets: ${{ secrets.* }}

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script_stop: true
          all_envs: true    
          script: |
           printenv

all_env: Injects all Github secrets as environment variables within the script.

AnderUstarroz avatar Jan 24 '22 01:01 AnderUstarroz

I would say this is the major issue of the package at the moment, and the only reason we didn't use it in production (Too much manual work when having certain number of variables). But if this feature is applied would be awesome!

peterschwarzdev avatar Jan 30 '22 13:01 peterschwarzdev

Maybe I can export all ENVS with GITHUB_ and INPUT_ as prefixes as below:

Screen Shot 2022-02-06 at 3 24 02 PM

so developer can use all variables in the script.

appleboy avatar Feb 06 '22 07:02 appleboy

@AnderUstarroz I can't add repository secret as global env dynamically so the best way is to define secret as environment manually.

appleboy avatar Feb 06 '22 13:02 appleboy

@appleboy I see.. but still would be super useful if we can have the rest of the variables automatically exported. Regarding Github secrets we can define them manually just once within the global space of a Github action, that way they will be available for all jobs:

env:
  SOME_VARIABLE: ${{ secrets.my_var1 }}  
  ANOTHER_VARIABLE: ${{ secrets.my_var2 }}    <-- Here all secrets defined manually within the Task global space.

jobs:
  some_job:
    name: some_job
    runs-on: ubuntu-latest
    steps:
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script_stop: true
          all_envs: true    
          script: |
           printenv <----- Should display SOME_VARIABLE, ANOTHER_VARIABLE and ideally also GITHUB_* and INPUT_*

  another_job:
    name: another_job
    runs-on: ubuntu-latest
    steps:
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script_stop: true
          all_envs: true    
          script: |
           printenv <----- envs also available here

AnderUstarroz avatar Feb 15 '22 08:02 AnderUstarroz

@appleboy agree with @AnderUstarroz on this one, if we have to define the github secrets manually only once at the root of the file that won't really be a problem. But at the moment we have to define over and over which variables are passed down to the script on every job which is a lot of boilerplate and hard to maintain.

peterschwarzdev avatar Feb 27 '22 08:02 peterschwarzdev

@peterschwarzdev @AnderUstarroz OK. I understand your requirement. I will add a new parameter all_envs to pass all environments to the host.

appleboy avatar Feb 27 '22 23:02 appleboy

This would be great!

simonv3 avatar Sep 05 '22 21:09 simonv3

Looking forward to having this!

alpavlove avatar Sep 11 '22 04:09 alpavlove

Was this implemented?

nekiro avatar Jul 18 '23 16:07 nekiro

I will take it.

appleboy avatar Jul 19 '23 13:07 appleboy

@peterschwarzdev @nekiro

    - name: pass all ENV variables to the script
      uses: appleboy/[email protected]
      env:
        INPUT_FOO: "BAR"
        INPUT_AAA: "BBB"
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY }}
        port: ${{ secrets.PORT }}
        allenvs: true
        script: |
          echo "I am $INPUT_FOO, thanks"
          echo "I am $INPUT_AAA, thanks"
          echo "$GITHUB_BASE_REF"
          echo "$GITHUB_REF"

Add allenvs flag to pass all environments to the scripts, only support the prefix value as INPUT_ and GITHUB_

appleboy avatar Jul 23 '23 01:07 appleboy

Add allenvs flag to pass all environments to the scripts, only support the prefix value as INPUT_ and GITHUB_

@appleboy Is there a reason why you only allow to pass environments starting with INPUT_ and GITHUB_ ?

abhinayagarwal avatar Mar 20 '24 02:03 abhinayagarwal

@abhinayagarwal

Because the GitHub Action Container by default will pass in variables that start with INPUT_ and GITHUB_.

appleboy avatar Mar 20 '24 07:03 appleboy

Let's say I have the following step where I already have N number of env variables, none of which start with either INPUT_ or GITHUB_:

 - name: pass all ENV variables to the script
      uses: appleboy/[email protected]
      env:
        FOO_1: "BAR_1"
        FOO_2: "BAR_2"
        ...
        FOO_N: "BAR_N"
      with:
        envs: FOO_1, FOO_2, ..., FOO_N 
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY }}
        port: ${{ secrets.PORT }}

As you can see I have to pass all of these env manually using envs. Adding allenvs: true support for these environment variables and automatically supplying them to the script would be nice, without having to append them with INPUT_ or GITHUB_.

abhinayagarwal avatar Mar 20 '24 07:03 abhinayagarwal