can't find package.json when running cypress test in azure pipeline yaml - azure-devops

I'm trying to run a cypress test in an Azure Pipeline job using YAML. When the pipeline runs, it gets to the line where the cypress test runs and throws this error:
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\myagent\_work\5\s\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\myagent\_work\5\s\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
Here is my YAML file:
trigger:
- master
pool:
name: Default
variables:
buildConfiguration: 'Release'
steps:
- task: NodeTool#0
displayName: 'Use Node 12.x'
inputs:
versionSpec: 12.18.3
checkLatest: false
- task: Npm#1
displayName: 'npm install'
inputs:
workingDir: WebApp
verbose: false
- task: Npm#1
displayName: 'Build Angular'
inputs:
command: custom
customCommand: run build -- --prod
workingDir: WebApp
- task: UseDotNet#2
displayName: 'Use .NET Core SDK 3.1.300'
inputs:
packageType: sdk
version: 3.1.300
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: AppApi/AppApi.csproj
vstsFeed: '*************'
- task: DotNetCoreCLI#2
displayName: 'dotnet publish'
inputs:
command: publish
arguments: '--no-restore --configuration $(buildConfiguration) --verbosity quiet --output $(build.artifactstagingdirectory)'
publishWebProjects: true
zipAfterPublish: true
- script: 'npx cypress verify'
displayName: 'cypress verify'
failOnStderr: true
- script: 'npm run cy:run --headless --spec cypress/integration/TestDemoExpanded.spec.ts --project ./WebApp'
displayName: 'run cypress tests'
workingDirectory: $(Build.SourcesDirectory)/WebApp
My package.json is checked into my Git repository and is located in my WebApp subfolder. How do I solve this error?
UPDATED:
I added the workingDirectory parameter as suggested but now I have this new error: npm ERR! missing script: cypress
I solved the 'missing script' error. The cypress run command I had in my YAML file didn't match the cypress run command I had in my package.json. I updated the cypress run step and now the tests run in the pipeline.

The task searches the package.json in the root folder and not in WebApp.
Just add:
workingDirectory: $(Build.SourcesDirectory)/WebApp
To the cypress step:
- script: 'npm run cypress run --headless --spec cypress/integration/TestDemoExpanded.spec.ts --project ./WebApp'
displayName: 'run cypress tests'
workingDirectory: $(Build.SourcesDirectory)/WebApp

Related

XCode build with Azure Pipelines for Ionic Capacitor App (no such module 'Capacitor')

I have a question about running a xcode build with azure pipelines on an ionic capacitor app.
The ionic and capacitor stuff is working fine. But with the Xcode build part I have a problem. The modules are not found.
At the bottom you can find my pipe. When I run this I got following error message:
/Users/runner/work/1/s/ProjectName/MobileApp/ProjectName/ios/App/App/AppDelegate.swift:2:8: no such module 'Capacitor'
Anyone that has an idea what could be the issue?
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: macos-latest
steps:
- checkout: self
- task: NodeTool#0
displayName: Use Node 16.x
inputs:
versionSpec: 16.x
checkLatest: true
- task: CmdLine#2
displayName: Install global ionic and npx
inputs:
script: npm install -g #ionic/cli
- task: CmdLine#2
displayName: Npm install & ionic build in App Folder
inputs:
script: "npm install \nionic build"
workingDirectory: ProjectName/MobileApp/ProjectName
- task: CmdLine#2
displayName: Create iOS App
inputs:
script: "npx cap add ios \nnpx cap sync ios"
workingDirectory: ProjectName/MobileApp/ProjectName
- task: CocoaPods#0
displayName: pod install
inputs:
cwd: ProjectName/MobileApp/ProjectName/ios/App
- task: Xcode#5
displayName: Xcode build
inputs:
signingOption: auto
teamId: XXXXXXXX
cwd: ProjectName/MobileApp/ProjectName/ios/App/App
I ran into this same error and had to specify the Xcode#5 task input
xcWorkspacePath: '$(Build.SourcesDirectory)/ios/App/App.xcworkspace'
scheme: 'App'

Implement/integrate ESLint in SonarQube?

I would like to ask how can I implement the ESLint on our Azure Pipeline? I do not have enough knowledge on Azure Pipelines and SonarQube, and I also am not so sure about ESLint. So far, here's the script I have. Although this is pretty much from the available tasks. I'd like to implement the ESLint on SonarQube like on this link: https://docs.sonarqube.org/pages/viewpage.action?pageId=11639183
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
checkLatest: true
displayName: 'Install Node.js'
- task: Npm#1
inputs:
command: 'install'
displayName: 'NPM Install'
- script: |
npm bin -g
displayName: 'Check path'
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'test' #from the package.json
This one here is from the npm bin -g:
##[debug] /home/vsts/work/_temp/5b2e34ae-e8f8-4e24-a539-9d41f7435789.sh
/bin/bash --noprofile --norc /home/vsts/work/_temp/5b2e34ae-e8f8-4e24-a539-9d41f7435789.sh
/opt/hostedtoolcache/node/10.23.1/x64/bin
##[debug]Exit code 0 received from tool '/bin/bash'
##[debug]STDIO streams have closed for tool '/bin/bash'
##[debug]task result: Succeeded
##[debug]Processed: ##vso[task.complete result=Succeeded;done=true;]
Here's the error from the last task.
##[debug]Agent.BuildDirectory=/home/vsts/work/1
##[debug]rm -rf /home/vsts/work/1/npm
##[debug]removing directory
##[debug]task result: Failed
##[error]Error: Npm failed with return code: 1
##[debug]Processed: ##vso[task.issue type=error;]Error: Npm failed with return code: 1
##[debug]Processed: ##vso[task.complete result=Failed;]Error: Npm failed with return code: 1
Here's a bit of copy from my package.json:
"scripts": {
"lint": "npm run lint:lwc && npm run lint:aura",
"linter": "./node_modules/.bin/eslint ./",
"lint:aura": "eslint **/aura/**",
"lint:lwc": "eslint **/lwc/**",
"test": "npm run test:unit",
There is not tasks or commands in your yaml pipeline to run the linter. You can add a script task or npm task to run the linter. See below:
Add a npm task to run linter.
- task: Npm#1
inputs:
command: 'install'
displayName: 'NPM Install'
- task: Npm#1
displayName: 'Linter'
inputs:
command: 'custom'
customCommand: 'run linter'
Or add a script task to run linter:
- task: Npm#1
inputs:
command: 'install'
displayName: 'NPM Install'
- script: |
npm run linter
npm run lint #run npm run lint:lwc && npm run lint:aura in your package.json
Before you can run above linter commands in azure pipeline. You need to add eslint dependency to your project and generate your ESLint config file on your local machine.
Run below commands on your local repo and then push to your source server.
Add eslint dependency:
npm install eslint --save-dev
Generate ESLint config file
./node_modules/.bin/eslint --init
You can check out this blog.

why is azure build pipeline not generating a dist folder for an angular build

It seem like the folder that are getting generated during the build are getting deleted before I could ran any other task dist folder.
trigger:
- master
pool: default
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
ng build --prod
displayName: 'npm install and build'
#i added this task and it seems like the node_modules and the dist
#folder that was generated during the build are not getting copied to the (a)directory
#only the source code get copied. it seems like the folders are getting
#deleted even before the #PublishPipelineArtifact#1 task ran
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Pipeline.Workspace)/s'
publishLocation: 'pipeline'
I also trying outputting the build in the
Firstly separate npm install script with ng build script. Secondly, I was running npm build instead of ng build. It took me a few hours to notice. This resolved my dist folder not being created issue.
- script: |
npm install
displayName: 'npm install'
- script: |
ng build --prod
displayName: 'ng build'
I had the Same issue where i failed to locate the dist folder, but it seemed i was only missing ./ in front of dist on CopyFiles#2 task.
Below is a working build pipeline for a dev/main branch of a angular application.
trigger:
- main
- dev
pool:
vmImage: ubuntu-latest
jobs:
- job: 'Dev_build'
condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/dev'))
steps:
- task: NodeTool#0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
ng build --configuration test
displayName: 'npm install and build'
# Used to diagnostic the file structure
# - task: Bash#3
# inputs:
# targetType: 'inline'
# script: 'find -name node_modules -prune -o -print'
- task: CopyFiles#2
inputs:
SourceFolder: './dist'
Contents: '**'
TargetFolder: '$(build.artifactstagingdirectory)'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: 'www'
- job: 'Prod_build'
condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/main'))
steps:
- task: NodeTool#0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
ng build --configuration --production
displayName: 'npm install and build'
- task: CopyFiles#2
inputs:
SourceFolder: './dist'
Contents: '**'
TargetFolder: '$(build.artifactstagingdirectory)'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: 'www'

lerna on AzureDevops

I am trying to make an azure Pipeline CI CD .
I want have some dependencies in my project like Lerna .
so i tried to install it but i am getting an error that the command is not found when i try to build it
Thats my azure-pipelines.yml
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- release
pool:
vmImage: "ubuntu-latest"
steps:
- task: NodeTool#0
inputs:
versionSpec: "10.0.0"
displayName: "Install Node.js"
- task: HelmInstaller#1
inputs:
helmVersionToInstall: 'latest'
- script: |
yarn global add lerna
yarn global add #gov.au/pancake
yarn install
displayName: "install lerna & pancake packages"
- script: |
yarn run build
displayName: "NPM build"
- script: |
export NODE_OPTIONS=--max-old-space-size=8192
displayName: "set Env Variable "
- script: |
lerna run build --stream --concurrency=1 --include-dependencies
lerna run docker-build-local --stream --concurrency=4 --include-filtered-dependencies
displayName: "Build lerna "
and i am getting an error in the last script although lerna is installed
/bin/bash --noprofile --norc /home/vsts/work/_temp/2d7931d0-403d-45a0-b7c9-9dfd7b5325f4.sh
/home/vsts/work/_temp/2d7931d0-403d-45a0-b7c9-9dfd7b5325f4.sh: line 1: lerna: command not found
/home/vsts/work/_temp/2d7931d0-403d-45a0-b7c9-9dfd7b5325f4.sh: line 2: lerna: command not found
##[error]Bash exited with code '127'.

Visual Studio Team Services - How to Setup a NativeScript Build?

I'm just starting a new project using NativeScript + Angular and have hosted the code in Visual Studio Team Services. I'm brand new to NativeScript however I want to have a Build Definition setup to build the iOS and Android artifacts for Continuous Integration.
When I build locally, I do the following command: tns build however I'm unclear how I would be able to use this in a Team Services Build Definition. Right now my build definition only consists of
Get Sources
npm install
??? (where I'd like to do tns build here)
It currently fails because it doesn't know what the tns command is and as far as I'm aware, I cannot install these tools on an agent since its a hosted instance.
Appreciate any thoughts or alternative ideas!
Here's the answer, you need to set your global directory and add it to the path.
Feel free to remove the debugging comments.
echo 'This is the current path.'
echo $PWD
echo 'This is the global path.'
npm prefix -g
echo 'Setting the npm prefix path'
npm config set prefix "$PWD/NPM"
echo 'Setting path'
PATH=$PATH:"$PWD/NPM/bin"
npm install nativescript -g
echo "PATH: $PATH"
tns doctor
I was able to get this working. I did this yaml pipeline in Azure Devops.
For ios
- task: UseNode#1
displayName: "Use Node ${{ parameters.nodeVersion }}"
inputs:
version: XX.XX
- task: Npm#1
displayName: 'npm clean'
inputs:
command: custom
verbose: false
customCommand: 'run clean'
- task: Npm#1
displayName: 'npm install nativescript'
inputs:
command: custom
verbose: false
customCommand: 'install nativescript#6 -g'
- task: PythonScript#0
displayName: "Install python package six"
inputs:
scriptSource: 'inline'
script: |
import os
os.system("pip install six")
- task: InstallAppleProvisioningProfile#1
displayName: "Install an Apple provisioning profile"
inputs:
provisioningProfileLocation: 'secureFiles'
provProfileSecureFile: ${{ parameters.iosProvisionProfile }}
- task: InstallAppleCertificate#2
displayName: "Install an Apple certificate"
inputs:
certSecureFile: 'devops-cn.p12'
certPwd: ${{ parameters.certificatePassword }}
keychain: 'temp'
signingIdentity: 'iPhone Distribution: Whatever Company'
- task: PythonScript#0
displayName: "Build"
env:
IOS_PROVISION_PROFILE_NAME: ${{ parameters.iosProvisionProfileName }}
IOS_IPA_FILE_NAME: ${{ parameters.iosIpaFileName }}
inputs:
scriptSource: 'inline'
script: |
import os, shutil
os.system("tns build ios --bundle --provision \"{}\" --for-device --release".format(os.environ.get('IOS_PROVISION_PROFILE_NAME')))
For Android
- task: UseNode#1
displayName: "Use Node XX.XX"
inputs:
version: XX.XX
- task: Npm#1
displayName: "npm install"
inputs:
command: 'custom'
customCommand: 'run clean'
- task: Npm#1
displayName: "npm install nativescript"
inputs:
command: 'custom'
customCommand: 'install nativescript#6 -g'
- task: PythonScript#0
displayName: "Build"
env:
AND_KEY_STORE_PASSWORD: ${{ parameters.androidKeyStorePassword }}
AND_KEY_STORE_ALIAS: ${{ parameters.androidKeyStoreAlias }}
AND_KEY_STORE_ALIAS_PASSWORD: ${{ parameters.androidKeyStoreAliasPassword }}
inputs:
scriptSource: 'inline'
script: |
import os
os.system("tns build android --bundle --release --key-store-path \"$(secureFile.secureFilePath)\" --key-store-password \"{}\" --key-store-alias \"{}\" --key-store-alias-password \"{}\"".format(os.environ.get('AND_KEY_STORE_PASSWORD'), os.environ.get('AND_KEY_STORE_ALIAS'), os.environ.get('AND_KEY_STORE_ALIAS_PASSWORD')))
You need to add install NativeScript Command-Line interface by using NPM task, for example:
NPM task 1.* (Command: custom; Command and arguments: install nativescript –g)
Command Line (call tns command) (Tool: tns; Arguments: --help)
On the other hand, you can setup a private build agent, then running it as your account (change service account)
Note: Based on my test, it works fine with Hosted Linux Preview agent; it throws error with Hosted agent (not supported platform)