title | description | ms.assetid | ms.reviewer | ms.topic | ms.custom | ms.date | monikerRange |
---|---|---|---|---|---|---|---|
Build and publish a Node.js package |
Learn how to use Azure Pipelines to build, deploy, and test a Node.js app. |
5BB4D9FA-DCCF-4661-B52B-0C42006A2AE5 |
vijayma |
quickstart |
devx-track-js, freshness-fy22q2 |
07/08/2024 |
<= azure-devops |
[!INCLUDE version-lt-eq-azure-devops]
In this quickstart, you use a pipeline to create a Node.js package with Node Package Manager (npm) and publish a pipeline artifact. You learn how to use Azure Pipelines to build, deploy, and test your JavaScript apps.
::: moniker range=">=azure-devops"
- A GitHub account where you can create a repository. Create a GitHub account for free.
- An Azure DevOps organization. Create one for free.
- An Azure DevOps project. Create one using the Azure DevOps Project Creation Wizard.
- The ability to run pipelines on Microsoft-hosted agents. You need to request the free grant of parallel jobs or purchase a parallel job. ::: moniker-end
::: moniker range="< azure-devops"
- A GitHub account where you can create a repository. Create a GitHub account for free.
- An Azure DevOps organization. Create one for free.
- An Azure DevOps project. Create one using the Azure DevOps Project Creation Wizard.
- A self-hosted agent. To create one, see Self-hosted agents. ::: moniker-end
Fork the sample Express.js server app.
- Go to the js-e2e-express-server repository.
- Select Fork in the upper-right corner of the page.
- Select your GitHub account. By default, the fork is named the same as the parent repository, but you can name it something different.
Important
During the following procedures, you might be prompted to create a GitHub service connection or redirected to GitHub to sign in, install Azure Pipelines, or authorize Azure Pipelines. Follow the onscreen instructions to complete the process. For more information, see Access to GitHub repositories.
- In your Azure DevOps project, select Pipelines > Create Pipeline, and then select GitHub as the location of your source code.
- On the Select a repository screen, select your forked sample repository.
- On the Configure your pipeline screen, select Starter pipeline. Azure Pipelines generates a YAML file called azure-pipelines.yml for your pipeline.
- Select the dropdown caret next to Save and run, select Save, and then select Save again. The file is saved to your forked GitHub repository.
- On the next screen, select Edit.
Edit your azure-pipelines.yml file as follows.
-
Replace the contents of the file with the following code. The code updates the Node.js tool installer task to use Node.js version 16 LTS.
trigger: - main pool: vmImage: 'ubuntu-latest' steps: - task: UseNode@1 inputs: version: '16.x' displayName: 'Install Node.js' - script: | npm install displayName: 'npm install' - script: | npm run build displayName: 'npm build' - script: npm test displayname: 'npm test'
-
Add the following new tasks to the pipeline:
-
The copy files task copies the files from the src and public folders to the build artifact staging directory.
-
The publish pipeline artifact task gets the files from the artifact staging location and publishes them as artifacts to be output with pipeline builds.
- task: CopyFiles@2 inputs: sourceFolder: '$(Build.SourcesDirectory)' contents: | src/* public/* targetFolder: '$(Build.ArtifactStagingDirectory)' displayName: 'Copy project files' - task: PublishPipelineArtifact@1 inputs: artifactName: e2e-server targetPath: '$(Build.ArtifactStagingDirectory)' publishLocation: 'pipeline' displayName: 'Publish npm artifact'
-
Select Validate and save, then select Save, select Run, and select Run again.
After your pipeline runs, verify that the job ran successfully and that you see a published artifact.
::: moniker range=">=azure-devops-2020"
:::image type="content" source="media/node-artifact-run.png" alt-text="Screenshot of successful pipeline run with an artifact.":::
::: moniker-end
-
Select GitHub as the source for your code.
-
Under Repository, select your forked repository, and select Continue.
-
Select Empty pipeline, and select Apply.
-
Under Agent specification in the right pane, select ubuntu-latest.
-
In the left pane, add the following tasks to the pipeline in order. For each task, select the + in Agent job 1, select the task at right and select Add, and configure the task if necessary.
-
npm
-
npm
- Display name: npm test
- Command: custom
- Command and arguments: test
-
Publish Test Results
-
Archive files
- Root folder or file to archive: $(System.DefaultWorkingDirectory)
- Prepend root folder name to archive paths: Uncheck
-
Publish build artifacts
-
-
Select Save & queue, and then select Save and run.
:::image type="content" source="media/classic-node.png" alt-text="Screenshot of the classic pipeline. ":::
Congratulations, you successfully created and ran a pipeline that built and tested a Node.js package. You can build, test, and deploy Node.js apps as part of your Azure Pipelines continuous integration and continuous delivery (CI/CD) system.
[!div class="nextstepaction"] Configure JavaScript