简体   繁体   中英

How to pass Github secrets as value in json file?

I'm using Cypress.io for my automated tests & triggering it in CI/D with Github Actions . The config cypress.json file has nested env values like so:

{
  "baseUrl": "<url-to-login>",
  "env": {
    "roles": {
      "admin": {
        "PASSWORD": "<password>",
        "USERNAME": "<username>"
      },
      "employee": {
        "PASSWORD": "<password>",
        "USERNAME": "<username>"
      },
      "client": {
        "PASSWORD": "<password>",
        "USERNAME": "<username>"
      }
    }
  }
}

Unfortunately, Cypress can't access deeply env variables so I'm creating the config cypress.json like so:

name: Cypress Tests

on: [push]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        # creates cypress.json file to run Cypress
      - name: Create Cypress config files
        run: |
          echo '{ "baseUrl": "${{ secrets.BASE_URL }}", "env": { "roles": { "admin": { "PASSWORD": "${{ secrets.PASSWORD }}", "USERNAME": "${{ secrets.USERNAME }}" } } } }' > cypress.json
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          build: yarn run
          start: yarn cypress:run
          wait-on-timeout: 120
          browser: chrome

It doesn't work, but I hardcoded the values it did work like so:

run: |
          echo '{ "baseUrl": "<hardcoded-redacted-value>", "env": { "roles": { "admin": { "PASSWORD": "<hardcoded-redacted-value>", "USERNAME": "<hardcoded-redacted-value>" } } } }' > cypress.json

So my question is, how to pass the secret in the json file?

I think you'll find it was fixed check for undefined values on setPluginResolvedOn function #7960

const roles = Cypress.env('roles') 
expect(roles.client.PASSWORD).to.eq('<password>')   // ✅ passes

I solved this issue by storing the entire cypress.json config file's content as GitHub's repository encrypted secret . Then, I used the create-json GitHub Action to generate the cypress.json needed to run Cypress on CI/CD. This is the final .github/workflows/main.yml file:

name: Cypress Tests

on: [push]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: create-json
        id: create-json
        uses: jsdaniell/create-json@1.1.2
        with:
          name: "cypress.json"
          json: ${{ secrets.CYPRESS_CONFIG_JSON }}
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          build: yarn run
          start: yarn cypress:run
          wait-on-timeout: 120
          browser: chrome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM