Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps Pipelines run a Node.js script as a step

What's the easiest way to run a Node.js script as a step in Azure DevOps Pipeline (release step)?

I tried a few approaches and it seems like this is not as straightforward as I'd like it to be. I'm looking for a general approach idea.

My last attempt was adding the script to the source repository under tools/script.js. I need to run it in a release pipeline (after build), from where I can't access the repo directly, so I have added the entire repo as a second build artifact. Now I can reach the script file from the release agent, but I haven't found a way to actually run the script, there seems to be no option to run a Node.js script on an agent in general.

like image 468
Kaspi Avatar asked May 31 '19 23:05

Kaspi


People also ask

How do I run an azure pipeline script?

CMD files. Azure Pipelines puts your inline script contents into a temporary batch file (. cmd) in order to run it. When you want to run a batch file from another batch file in Windows CMD, you must use the call command, otherwise the first batch file is terminated.

How do I deploy a node JS application on Azure DevOps?

Give the build definition the name DevDeploy and select Hosted from the Default agent queue menu. Click the Copy Publish Artifact: drop build task to see the details of this task. Click the Add Task button under the list of build tasks. Select the Azure App Service Deploy task and click the Add button next to it.

How do I run NPM on Azure pipeline?

You can either commit a . npmrc file to your source code repository and set its path or select a registry from Azure Artifacts. Select this option to use feeds specified in a . npmrc file you've checked into source control.


1 Answers

Option 1: Visual Pipeline Editor

1) Create the script file you want to run in your build/release steps

You can add the file to your Azure project repository, e.g. under tools/script.js and add any node modules it needs to run to your package.json, for example:

npm install --save-dev <module>

Commit and push, so your changes are online and Azure can see them.

2) Add your repo as an artifact for your release pipeline

You can skip this for build pipelines as they already have access to the repository.

enter image description here

enter image description here

3) Edit your release pipeline to ensure environment

Add a step to make sure correct Node version is on agent (Node.js Tool Installer):

enter image description here

Add a step to install all required node modules (npm):

enter image description here

4) Add your node script step

Use the Bash step to run your node script, make sure the working directory is set to root of the project (where package.json is located):

enter image description here

Option 2: YAML

you have a script\shell step where you can execute custom commands, just use that to achieve the goal. Agent have node installed on them, one thing you might need to do is use the pick node version step to use the right node version for your script

Example:

trigger:
- master

pool:
  vmImage: 'Ubuntu-16.04'

steps:
- checkout: self
  persistCredentials: true
  clean: true

- bash: |
    curl $BEDROCK_BUILD_SCRIPT > build.sh
    chmod +x ./build.sh
  displayName: My script download
  env:
    BEDROCK_BUILD_SCRIPT: https://url/yourscript.sh

- task: ShellScript@2
  displayName: My script execution
  inputs:
  scriptPath: build.sh
like image 101
4c74356b41 Avatar answered Sep 27 '22 19:09

4c74356b41