Create a json file during Azure DevOps build pipeline

Viewed 2449

I have a Azure DevOps build pipeline that runs a Cypress test. In that Cypress test we have a test user login with a e-mail and password. On my local system I have the password in a cypress.env.json file.

On the Azure build pipeline I get the message that the password is undefined which makes sense since we put the cypress.env.json file in the .gitignore not to expose it to the repo.

I've created a Azure variable to represent the password: $(ACCOUNT_PASSWORD)

So I think I need to create the cypress.env.json file in the build pipeline and use Azure variables for it, but I can't figure out how to create a file during the build step.

I have this task:

- task: CmdLine@2
  displayName: 'run Cypress'
  inputs:
    script: |
      npm run ci

So I need to add a task before this that creates the cypress.env.json file with the variable that represents the password:

{
  "ACCOUNT_PASSWORD": $(ACCOUNT_PASSWORD)
}
1 Answers

You can add a simple PS script that creates the file:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $json = '{
       "ACCOUNT_PASSWORD": $(ACCOUNT_PASSWORD)
      }'
        
      $json | Out-File cypress.env.json
    workingDirectory: '$(Build.SourcesDirectory)'
    pwsh: true # For Linux

In the workingDirectory set the path to where you want the file to be created.

Related