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.
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.
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.
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.
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.
You can skip this for build pipelines as they already have access to the repository.
Add a step to make sure correct Node version is on agent (Node.js Tool Installer):
Add a step to install all required node modules (npm):
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):
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With