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.

Kaspi
  • 3,538
  • 5
  • 24
  • 29
  • 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. – Kaspi Jun 01 '19 at 07:53

1 Answers1

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
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • 1
    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. – Kaspi Jun 01 '19 at 07:47
  • thats entirely up to you, i dont have any ideas about node – 4c74356b41 Jun 01 '19 at 07:58
  • 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. – Kaspi Jun 01 '19 at 08:34
  • yeah, running scripts works just fine for me. here's an example yam file that runs a bash script: https://dev.azure.com/4c74356b41/_git/fabrikate?path=%2Fazure-pipeline.yml&version=GBmaster – 4c74356b41 Jun 01 '19 at 09:04
  • 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? – Kaspi Jun 01 '19 at 10:38
  • if you are using the visual editor, you can just add a script\shell task and configure it. you dont have a yaml configuration for the build if you are using visual designer (but you can export it into yaml and create a new build that would use yaml) – 4c74356b41 Jun 01 '19 at 10:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194286/discussion-between-kaspi-and-4c74356b41). – Kaspi Jun 01 '19 at 10:55
  • I have updated your answer with a guide. Thank you for your kind help. – Kaspi Jun 01 '19 at 13:34
  • no probs, thanks for updating the answer :) feel free to accept it as well :) – 4c74356b41 Jun 01 '19 at 13:52