0
votes

I've been trying to setup an Azure Devops pipeline for testing purposes and i'm struggling to understand why one of my tasks runs the script line despite being skipped.

Here's the pipeline yaml code:

## 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')}}

A example, for instance when a push is made into the main branch it runs Publish and auto accept followed by the Publish, when it technically should only run the first one. One other thing that i saw was that when a pull request is incoming to one other branch rather than main it shouldn't trigger the script associated to Publish and auto accept but instead jump over that and run only the script in Publish, but instead it runs the scripts in both.

If anyone could provide some help with this i would appreciate it.

Thanks in advance

1

1 Answers

0
votes

I think the problem is that you run 4 tasks instead of two

Take a look at NPM task syntax, it has no 'script' parameter
https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/npm?view=azure-devops

'script' task that you are using is indeed shortcut of another task 'CmdLine@2' https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=azure-devops&tabs=yaml

Firstly you run NPM task with specified condition, but it does nothing

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

Then you run script task without condition(so it will run always), this task does npm stuff

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

Then you run again npm task without desired parameters but with conditions

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

And finally you run fourth task doing stuff, without conditions so it runs always.

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

So as to fix this problem, you need to use Npm@1 task with parameters specified in provided documentation. Or just add conditions to your script tasks(CmdLine@2).

I think that below snippet should work

- 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}'