AzureTipsAndTricks/blog/tip237.md

97 строки
5.8 KiB
Markdown

---
type: post
title: "Tip 237 - Setup an Azure Pipeline with Node.js"
excerpt: "Learn how to setup an Azure Pipeline with Node.js"
tags: [DevOps, Languages & Frameworks]
share: true
date: 2019-12-08 02:00:00
---
::: tip
:fire: If you are interested in live coding then follow me on [Twitch](https://twitch.tv/mbcrump) for developing on Azure and more.
:bulb: Learn more : [Build, test, and deploy JavaScript and Node.js apps](https://docs.microsoft.com/azure/devops/pipelines/languages/javascript?view=azure-devops?WT.mc_id=docs-azuredevtips-azureappsdev).
:tv: Watch the video : [How to build a Node.js application with Azure Pipelines](https://www.youtube.com/watch?v=z_4sMQKE_zw&list=PLLasX02E8BPCNCK8Thcxu-Y-XcBUbhFWC&index=3&t=22s)
:::
### Setup an Azure Pipeline with Node.js
#### Build a Node.js application with Azure Pipelines
A great way to improve the quality of your application is to automatically build it whenever you make changes. This enables you and your team to see if the application still works and fix errors quickly. You can easily set up a build pipeline with [Azure Pipelines](https://azure.microsoft.com/services/devops/pipelines?WT.mc_id=azure-azuredevtips-azureappsdev), which is part of [Azure DevOps](https://azure.microsoft.com/services/devops?WT.mc_id=azure-azuredevtips-azureappsdev).
In this post, we'll create an Azure Pipeline that builds and tests a [Node.js](https://nodejs.org/) application.
#### Prerequisites
If you want to follow along, you'll need the following:
* An Azure DevOps account. If you don't have one yet, you can start by [creating a free account here](https://azure.microsoft.com/services/devops?WT.mc_id=azure-azuredevtips-azureappsdev)
* A GitHub account. If you don't have one yet, you can [create a free account here](https://github.com/join?WT.mc_id=code-azuredevtips-azureappsdev)
#### Create an Azure Pipeline that builds and tests a Node.js application
Let's automate the build process of a Node.js app! For this example, we'll use an [existing Node application from GitHub](https://github.com/MicrosoftDocs/pipelines-javascript?WT.mc_id=code-azuredevtips-azureappsdev). Before we can start, we need to fork it:
1. Go to [https://github.com/MicrosoftDocs/pipelines-javascript](https://github.com/MicrosoftDocs/pipelines-javascript?WT.mc_id=code-azuredevtips-azureappsdev) and sign in with your GitHub account
2. Click the **Fork** button at the top of the repository. This will fork the repository to your account so that we can use it in our pipeline
Now we'll create the pipeline to build and test the Node application:
1. Go to the [Azure DevOps portal](https://dev.azure.com/?WT.mc_id=azure-azuredevtips-azureappsdev) and sign in
2. Create a **new project** and click on it when you are done
<img :src="$withBase('/files/36mainview.png')">
(Azure DevOps project)
3. In the "Welcome to the project!" pane, click on **Pipelines** to start creating a new Azure Pipeline. This will start a wizard
4. First, we need to indicate where our code is. It is in GitHub, so select **GitHub**
<img :src="$withBase('/files/36choosegithub.png')">
(Code choices in the Azure Pipelines wizard)
5. Next, you'll see a list of repositories in your GitHub account. Select the **forked repository** that contains the **sample Node.js** application
a. You might have to authorize Azure DevOps to use the GitHub repository. If so, click **Approve & Install**
6. In the next step, select **Node.js** to configure the pipeline for Node.js
<img :src="$withBase('/files/36choosenodejs.png')">
(Configuration choices in the Azure Pipelines wizard)
7. The next screen shows you the **YAML code** that defined the pipeline. You can learn more about YAML [here](https://learnxinyminutes.com/docs/yaml/). This code determines that node.js will be used and that the pipeline will perform a build. This code will only build the code and not execute any unit tests. Let's change that:
a. In the **script** section of the YAML code, add the following to enable the pipeline to run the unit tests in the code:
```
npm test
```
b. **Below the scripts section**, add the following code to publish the test results to the build results:
```
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '**/TEST-*.xml'
```
<img :src="$withBase('/files/36yammel.png')">
(YAML code of the Azure Pipeline)
8. Now click **Save and run** to save the pipeline and start it. The pipeline will now start building and testing the code. When it is done, you'll see the results like in the image below:
<img :src="$withBase('/files/36buildresults.png')">
(Results of the Azure Pipeline)
9. The build was successful! In the screen of the build results, you can click the **Tests** tab to see the results of the Unit Tests
10. In the test result screen, you can see the Unit Tests that ran (which is only one test in this case), and what the result of the run was
<img :src="$withBase('/files/36testresults.png')">
(Test results of the Azure Pipeline)
That's it. We have set up a pipeline that automatically builds and tests the application when changes are made to it. From here, you can set up more pipelines, like a release pipeline, that takes the build result and deploys it to a server. You can learn more about that [here](https://docs.microsoft.com/azure/devops/pipelines/ecosystems/javascript?view=azure-devops#build-and-push-image-to-container-registry&WT.mc_id=azure-azuredevtips-azureappsdev).
#### Conclusion
Setting up a Continuous Integration (CI) process, which builds your code automatically, is one of the simplest ways to increase the quality of your applications. It is very easy to set up a CI pipeline with [Azure Pipelines](https://azure.microsoft.com/services/devops/pipelines?WT.mc_id=azure-azuredevtips-azureappsdev), regardless of the programming language that you use or where your code is. Go and check it out!