首页 > 解决方案 > 有关任务条件的 Azure Devops 管道问题

问题描述

我一直在尝试设置 Azure Devops 管道以用于测试目的,但我很难理解为什么我的一项任务尽管被跳过却运行脚本行。

这是管道 yaml 代码:

## Example azure-pipelines.yml

## Event (branch to trigger the pipeline execution)
trigger:
  branches:
    include:
    - main
    exclude:
    - My-branch # Will not run

# Configures pipeline execution on pull requests
pr:
  branches:
    include:
    - main
    exclude:
    - My-branch # Will not run
    
# Environment variables created
variables:
- group: my-keys

## OS where the pipeline will run
pool:
  vmImage: 'ubuntu-latest'

# List of stages for your application
stages:
- stage: Test
  displayName: Application Testing
  # List of jobs the pipeline stage will run
  jobs:
  - job: MyJob
    displayName: Install packages and and publishes
    variables:
      # Sets the environment variable to cache the application packages
      npm_config_cache: $(Pipeline.Workspace)/.npm 
    # List of steps for the job
    steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '12.x'
      displayName: 'Install Node.js'  
    - task: Cache@2
      displayName: Install and cache packages
      inputs:
        key: 'npm | "$(Agent.OS)" | package-lock.json'
        restoreKeys: |
          npm | "$(Agent.OS)"
        path: $(npm_config_cache)
    - script: npm ci
      condition: ne(variables.CACHE_RESTORED, 'true')
    - task: Npm@1
      displayName: Publish and auto accept
      condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/main'))
    - script: npx my-package  --with-token=${my-keys} --auto-publish-changes
    - task: Npm@1
      displayName: Publish
      condition: eq(variables['Build.Reason'], 'PullRequest')
    - script: npx my-package --with-token=${my-keys}
    - script: echo ${{variables['Build.Reason']}} ${{eq(variables['Build.Reason'], 'PullRequest')}}

举个例子,例如,当推送到主分支时,它会在Publish and auto accept后面运行Publish,而从技术上讲,它应该只运行第一个。我看到的另一件事是,当拉取请求传入另一个分支时,main它不应该触发关联的脚本,Publish and auto accept而是跳过该脚本并仅在Publish其中运行脚本,而是在两个分支中运行脚本。

如果有人可以为此提供一些帮助,我将不胜感激。

提前致谢

标签: azure-devopsazure-pipelines

解决方案


我认为问题在于您运行 4 个任务而不是两个

看看 NPM 任务语法,它没有“脚本”参数
https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/npm?view=azure-devops

您正在使用的“脚本”任务确实是另一个任务“CmdLine@2”的快捷方式 https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=azure -devops&tabs=yaml

首先,您在指定条件下运行 NPM 任务,但它什么也不做

task: Npm@1
  displayName: Publish and auto accept
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/main'))

然后你无条件运行脚本任务(所以它会一直运行),这个任务做 npm 的东西

script: npx my-package  --with-token=${my-keys} --auto-publish-changes

然后你再次运行 npm task 没有所需的参数但有条件

task: Npm@1
  displayName: Publish
  condition: eq(variables['Build.Reason'], 'PullRequest')

最后你运行第四个任务,没有条件,所以它总是运行。

script: npx my-package --with-token=${my-keys}

为了解决这个问题,您需要使用 Npm@1 任务,并在提供的文档中指定参数。或者只是为您的脚本任务添加条件(CmdLine@2)。

我认为下面的片段应该可以工作

- task: CmdLine@2
  displayName: 'Publish and auto accept'
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/main'))
  inputs:
    script: 'npx my-package --with-token=${my-keys} --auto-publish-changes' 

- task: CmdLine@2
  displayName: 'Publish'
  condition: eq(variables['Build.Reason'], 'PullRequest')
  inputs:
    script: 'npx my-package --with-token=${my-keys}' 

推荐阅读