How to pass Github secrets to newman run with collections and environments?

Good evening,

I have a postman environment variable as a json file pushed to github action. This environment variable contains an api key which is hardcoded. To resolve this I added the api key in github action secrets. In the YAML file I am running newman with collections and environments. I am struggling to understand how it can be passed to my collection so my apis can pass. Need advice from this community as I am new to postman and github.

Postman Environment variable : Test-env.postman_environment.json

{

            "key": "ApiKey",

            "value": "abcdefg",

            "type": "secret",

            "enabled": true

        },

In the header of my API, i am passing this APIkey

{
                                "key": "Api-Key",
                                "value": "{{ApiKey}}"
                            },

In order to avoid hardcoding of api key “abcdefg” in environment variable. I decided to add it in githubsecrets.

In githubaction, added the value “abcdefg” as “test” in secrets

My yaml file is like this

name: Test secrets

on:

  push:

  workflow_dispatch:

  



jobs:

  Test-api:

    runs-on: ubuntu-latest

    steps:

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

      - uses: actions/checkout@v2



      # INstall Node on the runner

      - name: Install Node

        uses: actions/setup-node@v3

        with:

          node-version: "20.x"



      # Install the newman command line utility and also install the html extra reporter

      - name: Install newman

        run: |

          npm install -g newman

          npm install -g newman-reporter-htmlextra



    

      # Run the POSTMAN collection

      - name: Run POSTMAN collection

        run: |

         run: |

                    newman run "Testapi.postman_collection.json" -e "Test-env.postman_environment.json" -r cli,htmlextra --reporter-htmlextra-showEnvironmentData --reporter-htmlextra-export Reports/reports.html

       



      # Upload the contents of Test Results directory to workspace

      - name: Output the run Details

        uses: actions/upload-artifact@v4

        if: success() || failure()

        with:

          name: Report - Test

          path: Reports/Terstreports.html

          retention-days: 20

Hey @satellite-physicist8 :waving_hand:

Welcome to the Postman Community! :postman:

In order to reference the secrets in Github (or any other external CI/CD tool) you would need to add this to your Newman command:

--env-var "Api-Key=${{ secrets.API_KEY }}"

The flag --env-var is used to pass something outside of the collection, into the collection and resolve the variable placeholder you have. In this example, it’s going to resolve {{Api-Key}} in your Collection to API_KEY that you have stored in GitHub.

Each service will have it’s own variable syntax so it’s worth checking their documentation to know for sure.

HI Danny, Thanks for your response. Still not able to resolve the secrets and end point sis failing.

Here is the header

{
                                "key": "Api-Key",
                                "value": "{{ApiKey}}"
                            },

run: |

newman run "Testapi.postman_collection.json" -e "Test-env.postman_environment.json" --env-var "Api-Key = ${{secrets.APIKEY}}" -r cli,htmlextra --reporter-htmlextra-showEnvironmentData --reporter-htmlextra-export Reports/reports.html

There no space between the = but I also don’t know know what you have called the key in GitHub or know what you have placed it.

If you hardcode a value "Api-Key=test" does that resolve it? Do you have anything else in your environment file?

This flag can also be used to see the environment data:

--reporter-htmlextra-showEnvironmentData

Hi Danny,

yes I have other key value pairs in environment.

hii, @satellite-physicist8 :waving_hand:

Your API request header is looking for {{ApiKey}} but you’re trying to inject Api-Key from GitHub secrets, which creates a mismatch.

this is how your code should be

- name: Run POSTMAN collection
        run: |
          newman run "Testapi.postman_collection.json" \
            -e "Test-env.postman_environment.json" \
            --env-var "ApiKey=${{ secrets.APIKEY }}" \
            -r cli,htmlextra \
            --reporter-htmlextra-showEnvironmentData \
            --reporter-htmlextra-export Reports/reports.html

and make sure that ApiKey is the name of the Postman environment variable that your API header {{ApiKey}} is looking for. which is right in your code .

also make sure that APIKEY is the exact name you’ve given in secret of your GitHub repository.