azure-pipelines-tasks: Caching react scripts test fails after using caching in pipeline - on Ubuntu

Required Information

Entering this information will route you directly to the right team and expedite traction.

Question, Bug, or Feature?
Type: Bug

Enter Task Name: CacheBeta

Environment

  • Server - Azure Pipelines or TFS on-premises?

    • If using TFS on-premises, provide the version:

    • If using Azure Pipelines, provide the account name, team project name, build definition name/build number: 2toLead/VOR/ECSPilotFrontEnd/6858

  • Agent - Hosted or Private:

    • If using Hosted agent, provide agent queue name: Hosted Ubuntu 1604

    • If using private agent, provide the OS of the machine running the agent and the agent version:

Issue Description

# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript
name: $(TeamProject)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

trigger:
  branches:
    include:
      - master
      - develop
      - support/production

stages:
- stage: build
  displayName: build
  variables:
    CI: true
  jobs:
    - job: mainbuild
      pool:
        vmImage: 'ubuntu-16.04'
      steps:
      - task: NodeTool@0
        inputs:
          versionSpec: '10.x'
        displayName: 'Install Node.js'
      - task: CacheBeta@0
        inputs:
          key: |
            $(Agent.OS)
            $(Build.SourcesDirectory)/package-lock.json
          path: $(Build.SourcesDirectory)/node_modules
          cacheHitVar: CACHE_RESTORED
        displayName: 'Cache Npm'
      #install nodejs modules with npm
      - task: Npm@1
        displayName: 'npm install'
        inputs:
          workingDir: '$(Build.SourcesDirectory)'
          verbose: false
        condition: ne(variables.CACHE_RESTORED, 'true')
      #workaround for office ui fabric and unit tests
      - task: PowerShell@2
        inputs:
          filePath: './scripts/pretest.ps1'
          workingDirectory: '$(Build.SourcesDirectory)'
      #start unit tests
      - task: Npm@1
        displayName: 'npm test'
        inputs:
          command: custom
          customCommand: 'test'
          workingDir: '$(Build.SourcesDirectory)'
          verbose: false
      #workaround for office ui fabric and unit tests
      - task: PowerShell@2
        inputs:
          filePath: './scripts/posttest.ps1'
          workingDirectory: '$(Build.SourcesDirectory)'
      #build the solution
      - task: Npm@1
        displayName: 'npm run build'
        inputs:
          command: custom
          customCommand: 'run build'
          workingDir: '$(Build.SourcesDirectory)'
          verbose: false
      - task: PublishBuildArtifacts@1
        inputs:
          pathtoPublish: '$(Build.SourcesDirectory)/build'
          ArtifactName: drop
      - task: PublishBuildArtifacts@1
        inputs:
          pathtoPublish: '$(Build.SourcesDirectory)/scripts'
          ArtifactName: scripts

This is my pipeline definition (truncated the deploy stage for brevity), as I was testing the new CacheBeta task, I added this task as well as the condition on the npm install task. On cache miss, build succeeds. On cache hit, build fails on the npm test task with the log below. Project was created using the create react app a few months ago and is up to date in terms of dependencies. I suspect the test script wants to log somewhere in the node_modules folder, which is set to read-only by the cache restore. It’d be nice if we could still write to the folders restored by the cache, but get a warning in the post op (or even better, an option to get a warning or an error)

Troubleshooting

Checkout how to troubleshoot failures and collect debug logs: https://docs.microsoft.com/en-us/vsts/build-release/actions/troubleshooting

Error logs


Starting: npm test |  
-- | --
  | ============================================================================== |  
  | Task         : npm |  
  | Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts. |  
  | Version      : 1.155.2 |  
  | Author       : Microsoft Corporation |  
  | Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm |  
  | ============================================================================== |  
  | SYSTEMVSSCONNECTION exists true |  
  | SYSTEMVSSCONNECTION exists true |  
  | pt/hostedtoolcache/node/10.16.0/x64/bin/npm --version |  
  | 6.9.0 |  
  | pt/hostedtoolcache/node/10.16.0/x64/bin/npm config list |  
  | ; cli configs |  
  | metrics-registry = "https://registry.npmjs.org/" |  
  | scope = "" |  
  | user-agent = "npm/6.9.0 node/v10.16.0 linux x64" |  
  |   |  
  | ; environment configs |  
  | userconfig = "/home/vsts/work/1/npm/6858.npmrc" |  
  |   |  
  | ; node bin location = /opt/hostedtoolcache/node/10.16.0/x64/bin/node |  
  | ; cwd = /home/vsts/work/1/s |  
  | ; HOME = /home/vsts |  
  | ; "npm config ls -l" to show all defaults. |  
  |   |  
  | pt/hostedtoolcache/node/10.16.0/x64/bin/npm test |  
  |   |  
  | sh: 1: react-scripts: Permission denied |  
  | npm ERR! Test failed.  See above for more details. |  
  | > ecs@0.1.0 test /home/vsts/work/1/s |  
  | > react-scripts test |  
  |   |  
  | ##[warning]Couldn't find a debug log in the cache or working directory |  
  | ##[error]Error: Npm failed with return code: 1 |  
  | Finishing: npm test

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 21 (20 by maintainers)

Most upvoted comments

Follow up: reverting the scripts in pacakage.json to the original setup of create react app and using the following pipeline works much better (and faster). Note: upgraded cache beta task to version 1, used npm ci instead of npm install,

name: $(TeamProject)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

trigger:
  branches:
    include:
      - master
      - develop
      - support/production

stages:
- stage: build
  displayName: build
  variables:
    CI: true
    npm_config_cache: $(Pipeline.Workspace)/.npm
  jobs:
    - job: mainbuild
      pool:
        vmImage: 'ubuntu-latest'
      steps:
      - task: NodeTool@0
        inputs:
          versionSpec: '10.x'
        displayName: 'Install Node.js'
      - task: CacheBeta@1
        inputs:
          key: npm | $(Agent.OS) | package-lock.json
          path: $(npm_config_cache)
        displayName: 'Cache Npm'
      #install nodejs modules with npm
      - script: 'npm ci'
        workingDirectory: '$(Build.SourcesDirectory)'
        displayName: 'npm ci'
      #workaround for office ui fabric and unit tests
      - task: PowerShell@2
        inputs:
          filePath: './scripts/pretest.ps1'
          workingDirectory: '$(Build.SourcesDirectory)'
      #start unit tests
      - script: 'node node_modules/react-scripts/scripts/test.js'
        displayName: 'npm test'
        workingDirectory: '$(Build.SourcesDirectory)'
      #workaround for office ui fabric and unit tests
      - task: PowerShell@2
        inputs:
          filePath: './scripts/posttest.ps1'
          workingDirectory: '$(Build.SourcesDirectory)'
      #build the solution
      - script: 'node node_modules/react-scripts/scripts/build.js'
        displayName: 'npm run build'
        workingDirectory: '$(Build.SourcesDirectory)'
      - task: PublishBuildArtifacts@1
        inputs:
          pathtoPublish: '$(Build.SourcesDirectory)/build'
          ArtifactName: drop
      - task: PublishBuildArtifacts@1
        inputs:
          pathtoPublish: '$(Build.SourcesDirectory)/scripts'
          ArtifactName: scripts

Thanks @baywet . The chmod workaround works ?