6

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.

1
  • My particular issue is that I want to upload files to an FTP host and I also want change the uploaded files' permissions after they are uploaded, which the FTP upload step doesn't seem to allow. So the easiest workaround I was able to think of would be adding a Node script as a post-step to connect to the FTP host again and change the files' permissions. Commented Jun 1, 2019 at 7:53

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the suggestion. Which particular shell should I choose and how would I tell the shell that the file should be executed as a Node script? For example, I tried putting #! with node path to the first line and I have run it with Bash, but I still had syntax error at the second line.
thats entirely up to you, i dont have any ideas about node
Well I'm trying to say that this is general approach to run Node scripts as shell scripts under regular Linux Bash... or any scripts, this approach is not specific for Node. I still get a syntax error though.
yeah, running scripts works just fine for me. here's an example yam file that runs a bash script: dev.azure.com/4c74356b41/_git/…
Looks like exactly what I need. Thanks. I'm trying to find out how to edit the yam/YAML of a release stage. Seems like your yam is saved inside your repository, but mine is probably hidden somewhere as I have used only the visual editor so far. Is it possible to edit?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.