Crash after Azure DevOps CI build succeeded - react-native

I have created an Azure DevOps Continuous Integration Pipeline for my React-Native Project. My Yaml is as below:
pool:
name: Azure Pipelines
demands: yarn
steps:
- task: NodeTool#0
displayName: 'Use Node 10.x'
inputs:
versionSpec: 10.x
- task: geeklearningio.gl-vsts-tasks-yarn.yarn-task.Yarn#3
displayName: 'Install NPM modules'
- task: Bash#3
displayName: 'clean '
inputs:
targetType: filePath
filePath: ./android/gradlew
arguments: clean
workingDirectory: android
- task: Bash#3
displayName: 'assembleRelease '
inputs:
targetType: filePath
filePath: ./android/gradlew
arguments: 'assembleRelease -x bundleReleaseJsAndAssets'
workingDirectory: android
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: drop'
inputs:
PathtoPublish: android/app/build/outputs/apk/release
The build succeeded, but the problem is that the APK always crashes on the devices, but when I build the APK manually it does not crash.
Could any one help me?

I see you didnot specify an agent vmImage to build your apk. You should specify the vmImage under the pool to let your apk be built on the desired OS. Since the apk built locally worked fine. You can specify an agent vmImage the same with you local machine.
pool:
vmImage: windows-latest #or ubuntu-latest if your local machine is linux.
You can try using gradle task to build your apk instead of using bash task. Unsigned APKs can only run in an emulator. APKs must be signed to run on a device. So after your apk is generated. you need to use Android Signing task to sign your apk in azure pipeline.
Please check this detailed tutorial for more information.
In case the difference of jdk version causes the issue. You can compare the Jdk version used locally with the jdk version it used on your pipeline by checking the build task log. And make sure the jdk version used in azure pipeline is the same with your local jdk version by setting the JDK version field of gradle task.

Related

Android - `main` module field for "react-native-gesture-handler" lib that could not be resolved in Azure CI & its working fine in local machine

I am working with the azure pipeline for CICD for React native app. I am using macOS Big Sur (11.4) and Android Studio 4.2.2 as a local machine to create and test the app. App working fine on the local machine without any warning or issue. However, when I am pushing the code on the Azure pipeline it is giving me errors as below,
* /Users/runner/work/1/s/node_modules/react-native-gesture-handler/src/index.ts/index(.native|.android.jsx|.native.jsx|.jsx|.android.js|.native.js|.js|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.json|.native.json|.json).
Error: While trying to resolve module `react-native-gesture-handler` from file `/Users/runner/work/1/s/index.js`, the package `/Users/runner/work/1/s/node_modules/react-native-gesture-handler/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/runner/work/1/s/node_modules/react-native-gesture-handler/src/index.ts`. Indeed, none of these files exist:
* /Users/runner/work/1/s/node_modules/react-native-gesture-handler/src/index.ts(.native|.android.jsx|.native.jsx|.jsx|.android.js|.native.js|.js|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.json|.native.json|.json)
* /Users/runner/work/1/s/node_modules/react-native-gesture-handler/src/index.ts/index(.native|.android.jsx|.native.jsx|.jsx|.android.js|.native.js|.js|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.json|.native.json|.json)
at DependencyGraph.resolveDependency (/Users/runner/work/1/s/node_modules/metro/src/node-haste/DependencyGraph.js:436:17)
at Object.resolve (/Users/runner/work/1/s/node_modules/metro/src/lib/transformHelpers.js:317:42)
at resolve (/Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:629:33)
at /Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:645:26
at Array.reduce (<anonymous>)
at resolveDependencies (/Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:644:33)
at /Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:329:33
at Generator.next (<anonymous>)
at asyncGeneratorStep (/Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:137:24)
at _next (/Users/runner/work/1/s/node_modules/metro/src/DeltaBundler/traverseDependencies.js:159:9)
info Run CLI with --verbose flag for more details.
> Task :app:bundleMasterReleaseJsAndAssets FAILED
> Task :app:mergeUatDebugResources
FAILURE: Build failed with an exception.
My observation is that the Azure pipeline does not have macOS Big Sur as Agent. My local machine changes some configurations to support the latest Android studio and macOS and the azure pipeline agent does not support macOS Big Sur reason it is not able to find it
.
NOTE: I have tried all clean processes like yarn, pod, or delete node_module.
[azure-devops]
I have got the solution for the issue.
Actually, the latest version of macOS Big Sur (11.4) was not having a node install on VM.
I did some changes in the pipeline.
pool:
vmImage: macOS-11
steps:
- checkout: self
persistCredentials: true
clean: true
- task: NodeTool#0
displayName: 'Install Node'
inputs:
versionSpec: 'v16.6.2' # you can use your desired version here
- script: yarn install
displayName: Install Dependencies

Pass Azure Devops $(Build.BuildNumber) to npm task

I want to update the package.json version through azure devops pipeline.
This is the task I used. But it throws error.
- task: Npm#1
displayName: "npm version"
command: "custom"
workingDir: src
verbose: false
customCommand: "version $(Build.BuildNumber)"
How to correctly pass the pipeline build number to npm task.
Thanks in advance.
We can use command npm version <newversion> to update the package.json version. for example, current version is v1.0.1, using command npm version 2.0.0 will update its version to v2.0.0. Please note that the version format must be the same, like major.minor.patch.
In addition, yaml pipeline will set the build numer to be formatted like 20210426.5 by default. If your package.json is not formatted like this, this command npm version $(Build.BuildNumber) will cause issues. You could use UpdateBuildNumber to override the automatically generated build number.
BTW, the following yaml pipeline is for your reference supposed that your package.json is formatted like major.minor.patch. Also you could use custom variables to specify the version number.
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "Hello World"
git config --global user.email "you#example.com"
git config --global user.name "Your Name"
echo "##vso[build.updatebuildnumber]2.1.0"
- task: Npm#1
inputs:
command: 'custom'
workingDir: ''
customCommand: 'version $(Build.BuildNumber)'

How to run XUnit tests with ASP.NET Core dependency in Azure DevOps pipeline?

We're setting up CI/CD for an ASP.NET Core Web API project. We want to run unit tests in the build pipeline from an XUnit project that references the Web API project. This is from our build pipeline:
# azure-pipelines.yml
...
pool:
vmImage: 'windows-latest'
...
- task: UseDotNet#2
inputs:
version: 3.1.x
packageType: runtime
- task: DotNetCoreCLI#2
inputs:
command: build
projects: '**/MyWebApi.csproj'
arguments: '--configuration Release'
- task: DotNetCoreCLI#2
inputs:
command: test
projects: '**/MyUnitTests.csproj'
arguments: '--configuration Release'
...
When we run it, the Web API project is successfully built, but the tests fail to run, giving the following error:
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
Do we need to extract the classes (from Web API project) that we want to test into another library that doesn't depend on AspNetCore framework? Or can we install the framework on the pipeline agent with a task like UseDotNet#2?
Please change your UseDtonet step and ad restore step as follows:
- task: UseDotNet#2
inputs:
version: 3.1.x
packageType: sdk
- task: DotNetCoreCLI#2
inputs:
command: 'restore'
projects: '**/*.csproj'

Install peer dependencies in Azure Pipeline

I want to build npm packages via Azure DevOps. My build pipeline fails because peer dependencies are not installed. Is there a way to install the peer dependencies from the package.json?
Below is my sample azure-pipelines.yml file for building and publishing my npm package.
pool:
name: Azure Pipelines
demands: npm
vmImage: 'ubuntu-latest'
steps:
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm#1
displayName: 'npm install project'
inputs:
workingDir: 'projects/my-project'
verbose: false
- task: Npm#1
displayName: 'ng build'
inputs:
command: custom
verbose: false
customCommand: 'run ng build -- --prod'
- task: Npm#1
displayName: 'npm publish'
inputs:
command: publish
workingDir: 'dist/my-project'
verbose: false
publishEndpoint: NPM
condition: contains(variables['Build.SourceBranch'], 'master')
Bonus question: How do I apply a tag when publishing?
For this issue ,first, you need to make sure that the Working folder specified in the npm install task contains target package.json.
Secondly, do all the dependent packages you need to use exist in the public feed? If not, you need to use packages from your Azure Artifacts feed.
In addition, you need to make sure that the dependent packages are specified in your package.json. If everything is ok, can you run successfully locally?
Update:
The automatic install of peer dependencies was explicitly removed with npm 3.
Here is the case you can refer to .

Azure Pipelines "Cache#2" fails with "##[error]The system cannot find the file specified"

I'm using Azure Pipelines with hosted builds to build a web project. Our build times were hitting 10-15 minutes, with most (5-10 minutes) of the time spent doing npm install. To speed this up, I'm trying to use the Cache task (https://learn.microsoft.com/en-us/azure/devops/pipelines/caching/?view=azure-devops).
However, when the auto-added task Post-job: Cache runs, it always errors out with:
##[error]The system cannot find the file specified
The host server is Windows Server 2017.
Here is my entire build YAML
# Node.js with Vue
# Build a Node.js project that uses Vue.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- develop
pool:
name: Default
variables:
FONTAWESOME_NPM_AUTH_TOKEN: $(FONTAWESOME_NPM_AUTH_TOKEN_VARIABLE)
npm_config_cache: $(Pipeline.Workspace)/.npm
steps:
- task: DutchWorkzToolsAllVariables#1
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- task: Cache#2
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
path: $(npm_config_cache)
cacheHitVar: NPM_CACHE_RESTORED
- task: Npm#1
displayName: 'npm install'
inputs:
command: 'install'
condition: ne(variables.NPM_CACHE_RESTORED, 'true')
- task: Npm#1
displayName: 'npm run build'
inputs:
command: 'custom'
customCommand: 'run build'
- task: CopyFiles#2
inputs:
SourceFolder: '$(Build.Repository.LocalPath)\dist'
Contents: '**'
TargetFolder: '$(Build.StagingDirectory)'
CleanTargetFolder: true
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
Cache task output:
Starting: Cache
==============================================================================
Task : Cache
Description : Cache files between runs
Version : 2.0.0
Author : Microsoft Corporation
Help : https://aka.ms/pipeline-caching-docs
==============================================================================
Resolving key:
- npm [string]
- "Windows_NT" [string]
- package-lock.json [file] --> F93EFA0B87737CC825F422E1116A9E72DFB5A26F609ADA41CC7F80A039B17299
Resolved to: npm|"Windows_NT"|rbCoKv9PzjbAOWAsH9Pgr3Il2ZhErdZTzV08Qdl3Mz8=
Information, ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session zzzzz
Information, Getting a pipeline cache artifact with one of the following fingerprints:
Information, Fingerprint: `npm|"Windows_NT"|rbCoKv9PzjbAOWAsH9Pgr3Il2ZhErdZTzV08Qdl3Mz8=`
Information, There is a cache miss.
Information, ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session zzzzz
Finishing: Cache
Post-job: Cache output:
Starting: Cache
==============================================================================
Task : Cache
Description : Cache files between runs
Version : 2.0.0
Author : Microsoft Corporation
Help : https://aka.ms/pipeline-caching-docs
==============================================================================
Resolving key:
- npm [string]
- "Windows_NT" [string]
- package-lock.json [file] --> 2F208E865E6510DE6EEAA6DB0CB7F87B323386881F42EB63E18ED1C0D88CA84E
Resolved to: npm|"Windows_NT"|OQo0ApWAY09wL/ZLr6fxlRIZ5qcoTrNLUv1k6i6GO9Q=
Information, ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session zzzzz
Information, Getting a pipeline cache artifact with one of the following fingerprints:
Information, Fingerprint: `npm|"Windows_NT"|OQo0ApWAY09wL/ZLr6fxlRIZ5qcoTrNLUv1k6i6GO9Q=`
Information, There is a cache miss.
Information, ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session zzzzz
##[error]The system cannot find the file specified
Finishing: Cache
How can I fix my build definition so the caching works?
#Levi Lu-MSFT was right in his comment but there's a gotcha.
#FLabranche has a working solution in his answer but I believe reasoning is not quite right.
The problem
npm install and #Cache task are looking for the npm cache at different locations. Consider the flow when pipeline runs for the first time:
#Cache task: does nothing since there's no cache yet.
npm i (or npm ci) task: installs packages in node_modules/ and updates the npm cache at default location. Default location is ~/.npm on Linux/Mac and %AppData%/npm-cache on Windows. On Linux hosted cloud agent the absolute path will be /home/vsts/.npm.
(... more tasks from your pipeline)
Post-job #Cache task (added implicitly): reads the npm cache found at user-provided location to store it for future reuse. User-provided location is set by the npm_config_cache: $(Pipeline.Workspace)/.npm variable. On Linux hosted cloud agent the absolute path will be /home/vsts/work/1/.npm.
As a result, #Cache task fails with tar: /home/vsts/work/1/.npm: Cannot open: No such file or directory.
Solution
Make npm install and #Cache task use the same npm cache location.
One option suggested by Levi Lu is to update the npm config with npm config set cache $(npm_config_cache) --global but it won't work in the pipeline (at least it didn't work for me in an Azure-hosted Linux agent): Error: EACCES: permission denied, open '/usr/local/etc/npmrc'
npm ci --cache $(npm_config_cache) updates the npm cache location for a single call and it does work in this case. It feels a bit hacky though since --cache option is not even documented on the npm website.
All in all this code worked for me:
variables:
NPM_CACHE_FOLDER: $(Pipeline.Workspace)/.npm
steps:
- task: Cache#2
displayName: Cache npm dependencies
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
restoreKeys: |
npm | "$(Agent.OS)"
npm
path: $(NPM_CACHE_FOLDER)
- script: npm ci --cache $(NPM_CACHE_FOLDER)
displayName: 'Install npm dependencies'
...
You can log into your Windows Server 2017 server and check if the folder $(Pipeline.Workspace)/.npm is created and the dependencies are stored inside.
I copied and tested your yaml. It worked both on local agent(win2019) and cloud agents. You can try to run your pipeline on the cloud agents or other agents with newer system to check if it is the agent that cause this error.
The keys generated with your package-lock.json differ between the two tasks.
It happens when the file is modified. Here, they're modified by your npm install task.
You can use the restoreKeys option when configuring the Cache task to fall back onto the latest cache entry.
And I think you don't need the 'npm install' task.
Could you try replacing this :
- task: Cache#2
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
path: $(npm_config_cache)
cacheHitVar: NPM_CACHE_RESTORED
- task: Npm#1
displayName: 'npm install'
inputs:
command: 'install'
condition: ne(variables.NPM_CACHE_RESTORED, 'true')
By this definition :
- task: Cache#2
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
restoreKeys: |
npm | "$(Agent.OS)"
npm
path: $(npm_config_cache)
displayName: Cache npm
- script: npm ci --cache $(npm_config_cache)
Yesterday, I was able to get it working with no issue at all on a self-hosted machine agent by using this:
- task: Cache#2
inputs:
key: '**/package-lock.json, !**/node_modules/**/package-lock.json, !**/.*/**/package-lock.json'
path: '$(System.DefaultWorkingDirectory)/node_modules'
displayName: 'Cache Node Modules'
Today, trying to work on a hosted agent today and this doesn't cut it at all. Aggh, Back to the grinding board. Anyhow, maybe could work for you on your self-hosted pipeline
This seems to be related to this open issue.
I have resolved the problem by switching the build agent pool to hosted and using windows-latest image.
pool:
vmImage: 'windows-latest'