How to manually add "script" in an Azure CI Pipeline? - azure-devops

I have a node.js/typescript/angular project in BitBucket that I want to create a build (CI) pipeline for it on Azure Devops. I used the classic editor to create the pipeline (reason below) and am trying to add the following task(s)/step(s):
npm install #types/node#8.10.52
ng build (ng is angular)
If was to use the YAML configuration, the resulting YAML file looks like the following file below, so how do i create a "script" manually after the Node task i have in classic editor? I only have options to add "npm" as a task, which is why i have added 3 npm tasks as shown in image above with 3 separate custom commands to mimic the steps configuration in the YML file below. Is that the way to do it with custom command?
YAML file npm/angular representation via YAML configuration:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
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'
Reason why Im using classic editor:
When i tried saving the YAML configuration pipeline, i got a "Error from bitbucket: something went wrong" error, which appears to be a write-permission issue based on what i found from Atlassian forums.
So i ended up just using the classic pipeline editor, and this way i was able to select a specific branch (i.e. dev) instead of master (prod) branch.

The way I've handled this is to add a script to your package.json:
"scripts": {
"ng": "ng",
"build": "ng build",
"build-prod": "ng build --configuration=production"
"build-dev": "ng build --configuration=dev"
},
...
Then, you just call run-script from the custom NPM task:
Or you could optionally on the task just call run-script build --prod since you can pass arguments on the task.
These same steps are available in YAML, it would look something like this:
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install node.js'
- task: Npm#1
inputs:
command: 'install'
displayName: npm install
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'run-script build --prod'
displayName: 'npm build'

Related

Azure pipeline - install module from artifact

I am creating a pipeline on Azure devOps to deploy an angular application. Inside the project dependencies there is a library published on the artifact of another project (under the same organization). How to configure my yaml file so that only that library is installed from the artifact registry?
This is my yaml file
pool:
name: Azure Pipelines
steps:
- task: NodeTool#0
displayName: 'Use Node 14.x'
inputs:
versionSpec: 14.x
- task: Npm#1
displayName: 'npm install angular-cli'
inputs:
command: custom
verbose: false
customCommand: 'install -g #angular/cli#12.1.1'
- **TASK TO INSTALL MY LIBRARY FROM ARTIFACT**
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm#1
displayName: 'create build'
inputs:
command: custom
verbose: false
customCommand: 'run build'
- ...task to deploy
You can set the Artifact feed containing your required npm packages as upstream sources for the current project Artifact feed. To add upstream sources to your feed you can refer to Configure upstream sources.
Then you can add the packages you need to the dependencies of your package.json. And use npm task to install the packages.

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.

How to deploy angular universal app in azureDevOps app service

I'm facing a problem deploying an angular universal app in azure web service I followed this step https://stackoverflow.com/a/53616516/10979521 but a got an error says
##[error]Error: Publish using webdeploy options are supported only when using Windows agent.
I guess the issue occurs in creating an app service, in my app service settings
(
*publish (code)
*runtime stack (.NET Core 2.2)
*Operating System (Windows)
)
# Node.js with Angular
# Build a Node.js project that uses Angular.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
npm run build:ssr
displayName: 'build the project'
- task: CopyFiles#2
displayName: 'Copy dist files to staging'
inputs:
SourceFolder: '$(Build.SourcesDirectory)/dist'
TargetFolder: '$(Build.ArtifactStagingDirectory)/app/dist'
- task: CopyFiles#2
displayName: 'Copy server.js to the root'
inputs:
SourceFolder: '$(Build.ArtifactStagingDirectory)/app/dist'
Contents: server.js
TargetFolder: '$(Build.ArtifactStagingDirectory)/app'
- task: DeleteFiles#1
displayName: 'Delete the dist/server.js'
inputs:
SourceFolder: '$(Build.ArtifactStagingDirectory)/app/dist'
Contents: server.js
- task: AzureRmWebAppDeployment#3
displayName: 'Azure App Service Deploy: website'
inputs:
azureSubscription: 'my subscription'
WebAppName: 'my website'
Package: '$(Build.ArtifactStagingDirectory)/app'
GenerateWebConfig: true
WebConfigParameters: '-Handler iisnode -NodeStartFile server.js -appType node'
UseWebDeploy: true
RemoveAdditionalFilesFlag: true
Edit 2 (9/3/2020)
Microsoft is moving away from release pipelines.
"Classic Release Pipelines"
https://learn.microsoft.com/en-us/azure/devops/pipelines/release/?view=azure-devops
New "Pipelines"
https://learn.microsoft.com/en-us/azure/devops/pipelines/create-first-pipeline?view=azure-devops&tabs=java%2Cyaml%2Cbrowser%2Ctfs-2018-2
Edit:
I realized after I typed this up that maybe your problem is you have "UseWebDeploy: true" and maybe you can fix your problem by setting this to false. Below is a screenshot taken from the same task setup in a release pipeline.
I still think your best option is to remove the deploy task from your build pipeline as outlined below since build pipelines are not meant to be used in this way. But that is up to you.
Original Answer:
Remove this part from your build pipeline:
- task: AzureRmWebAppDeployment#3
displayName: 'Azure App Service Deploy: website'
inputs:
azureSubscription: 'my subscription'
WebAppName: 'my website'
Package: '$(Build.ArtifactStagingDirectory)/app'
GenerateWebConfig: true
WebConfigParameters: '-Handler iisnode -NodeStartFile server.js -appType node'
UseWebDeploy: true
RemoveAdditionalFilesFlag: true
Add a publish step to the end of you build.yml file. This will allow your release pipeline to pick up the artifacts from your build.
- task: PublishBuildArtifacts#1
Setup a release pipeline to deploy your build.
Create a new pipeline
Add the artifact from your build
Then add a stage (ie Dev, QA, Production)
Select "empty job"
Edit the stage and add tasks
Add the Azure App Service task
Update the version number on the Azure App Service Deploy to version 3 (the same one you are using in your yaml - AzureRmWebAppDeployment#3).
Fill in the values in the task the same way you have it in your yaml.
The default value for the "Package or folder" option probably isn't correct. You can use the 3 dots on the right to navigate and select the correct folder. If you click this and see nothing then this is most likely because you haven't kicked off a build yet with the above "PublishBuildArtifact#1" change.
Keep working your way down and expanding the options to find your other configuration settings.
If you are having trouble with anything you can verify the task is setup properly by scrolling to the top of the task and clicking "View YAML". Then you can compare to your original yaml.
Hopefully this helps.

Azure DevOps Pipeline issue

I am struggling to get a simple build and deploy working and was hoping for some assistance. Could anyone review the steps and also why the Publish Artifacts does not work? It's a simple Angular 7 project.
Error:
[section]Starting: Publish Artifact: dist
========================================================================== Task: Publish Build Artifacts Description: Publish build
artifacts to Azure Pipelines/TFS or a file share Version:
1.142.2 Author : Microsoft Corporation Help : More Information
[warning]Directory 'D:\a\1\s\dist' is empty. Nothing will be added to build artifact 'dist'.
[section]Finishing: Publish Artifact: dist
YAML:
pool:
vmImage: Hosted VS2017
demands: npm
steps:
- script: |
echo Write your commands here
mkdir dist
echo Use the environment variables input below to pass secret variables to this script
displayName: 'Command - mkdir dist'
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm#1
displayName: 'npm build'
inputs:
command: custom
verbose: false
customCommand: 'build --prod'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: dist'
inputs:
PathtoPublish: dist
ArtifactName: dist
- task: FtpUpload#1
displayName: 'FTP Upload: dist'
inputs:
credentialsOption: inputs
serverUrl: ‘xxx’
username: Tester2
password: 'Tester$2'
rootDirectory: dist
filePatterns: '*'
remoteDirectory: /
trustSSL: true
Azure DevOps Pipeline issue
The Publish Build Artifacts task is used to publish build artifacts to Azure Pipelines, TFS, or a file share.
But, just like Daniel and Andrey said, although you add the npm build, you did not set the installed folder to be dist. So the result of npm build will not be saved in the dist folder. In this case, the folder dist is empty.
Besides, to save the build result to the dist folder, you can try to use the option -- -op like following:
run ng build --prod -- -op ..\..\dist
The ..\..\dist should use relative path based on the project.json file.
Check the document JavaScript frameworks: AngularJS for some more details info.
Hope this helps.

Azure Devops handle special character in yaml

I have problem with the Azure Devops yaml script, as it doesn't pick up my variable properly to build my ReactJS project. Below is the script, but somehow the build failed at git push, and the username does't get picked up
pool:
vmImage: 'Ubuntu 16.04'
steps:
- task: NodeTool#0
inputs:
versionSpec: '9.8.0'
displayName: 'Install Node.js'
- script: |
npm install --no-save
npm run build
git push https://"$(azure.Username)":$(azure.Password)#$(azure.AppName).scm.azurewebsites.net:443/$(azure.AppName).git master
displayName: 'npm install and build'
My solution as per the yml file below, and you can set the variable to have "$" sign in it. Another better way is to use the steps in Azure DevOps itself.
image: node:9.8.0
clone:
depth: full
pipelines:
default:
- step:
script:
- npm install --no-save
- npm run build
- git push https://$AZURE_LOGIN:$AZURE_PASSWORD#$AZURE_APP_NAME.scm.azurewebsites.net:443/$AZURE_APP_NAME.git master
The correct answer is to wrap the variable in single quotes as such: '$(myVariable)'. This prevents the yaml parser from parsing the contents of the variable