在 Azure DevOps 构建管道期间创建 json 文件

Pet*_*sma 10 azure-devops azure-pipelines

我有一个运行 Cypress 测试的 Azure DevOps 构建管道。在 Cypress 测试中,我们有一个使用电子邮件和密码登录的测试用户。在我的本地系统上,我的密码保存在一个cypress.env.json文件中。

在 Azure 构建管道上,我收到一条消息,表明密码是undefined有意义的,因为我们将cypress.env.json文件放入 .gitignore 中,以免将其暴露给存储库。

我创建了一个 Azure 变量来表示密码:$(ACCOUNT_PASSWORD)

因此,我认为我需要cypress.env.json在构建管道中创建文件并为其使用 Azure 变量,但我不知道如何在构建步骤中创建文件。

我有这个任务:

- task: CmdLine@2
  displayName: 'run Cypress'
  inputs:
    script: |
      npm run ci
Run Code Online (Sandbox Code Playgroud)

因此,我需要在此之前添加一个任务,cypress.env.json使用代表密码的变量创建文件:

{
  "ACCOUNT_PASSWORD": $(ACCOUNT_PASSWORD)
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*zyk 10

您可以添加一个简单的 PS 脚本来创建该文件:

- 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
Run Code Online (Sandbox Code Playgroud)

workingDirectory设置要创建文件的位置的路径。

  • 任务“PowerShell@2”也适用于 Linux,只需添加“pwsh: true” (4认同)