# Node.js

Learn how to build and deploy Node.js application.

<Hint type="success">

📚 Buddy Tutorial: [Learn how to develop a simple Node.js Discord bot](/tutorials/how-to-build-a-discord-bot-in-node-js-for-beginners)

</Hint>

Node.js in an open-source environment that runs on the Javascript engine allowing you to invoke JavaScript code

## How to setup CI/CD pipeline for Node.js

With Buddy, you can create a pipeline that builds, tests, and deploys Node applications on a push to Git. The configuration is super simple and setting up your CI/CD process takes 5 to 10 minutes.

![CI/CD pipeline example for Node](/docs/quickstart/node/node-1.png 640x399)

However, before we get started, let's gather some basic knowledge on CI/CD key features:

- **Continuous Integration** – assumes that every single change to source code is built and tested automatically, and then integrated into a remote repository, with the build status returned to the sender. Automated testing, including unit tests, is one of the main assumptions of Continuous Integration, allowing developers to check their code for possible bugs at the early stage of development process.

- **Continuous Deployment** – means real-time deployment to staging or production environment (or web service), under condition that the tested code is valid and no longer requires bug fixes.


## 1. Create a new project

Go to **Projects** and click **Create new project**. Select your Git hosting provider (Buddy, GitHub, Bitbucket, GitLab, or Private Server) and enter your project name.

![Create new project](/docs/_global/create-new-project.png 640x366)

## 2. Add a new pipeline

Navigate to **Pipelines** and click **New pipeline**. In the form, configure the following:

- **Name** - Enter a descriptive name for your pipeline (e.g., "Deploy to production")
- **ID** - Optional identifier for the pipeline
- **CPU** - Select architecture: **x64** or **ARM64**
- **Resources per pipeline run** - Choose resource allocation (default: Workspace default)
- **[Definition Location](/docs/pipelines/introduction/advanced-settings.md#definition-location)** - Optionally check "Store configuration in a Git repository" to use YAML files

Click **Add pipeline** to create it.

![Add new pipeline](/docs/_global/add-new-pipeline.png 640x497)

## 3. Configure workflow: contexts and triggers

After creating the pipeline, go to the **Workflow** tab to configure:

![Configure Workflow](/docs/_global/configure-workflow.png 640x387)

* **Contexts** - Click `+` to add Git branch/tag context or Environment. See [Contexts](/docs/pipelines/introduction/contexts.md).

* **Triggers** - Click `+` to add Git events, Schedule, or Webhook. See [Workflow Configuration](/docs/pipelines/introduction/creating-pipeline/workflow-configuration.md).


## 4. Add actions

Buddy lets you choose from dozens of predefined actions. In this example, we'll add 4 actions that perform the following tasks:

* Build and test Node.js app: download dependencies \(npm, yarn, etc.\), run automated tests, compile assets \(npm tasks, webpack, etc.\)
* Upload code to server together with compiled assets
* Restart application
* Send notification to Slack

### 4.1 Build your Node.js application

<Hint type="success">

Build actions in Buddy are run in isolated containers run from official Docker images. When the pipeline is run, Buddy pulls the container, runs build commands, and uploads the results to the pipeline filesystem.

</Hint>

Look up and click **Node.js** on the action list to add it to the pipeline: 

![Action list](/docs/quickstart/node/node-2.png 640x543)

The <u>Run</u> tab lets you determine the commands to execute. The default commands are:

```bash
npm install
npm test
```
![Default build commands](/docs/quickstart/node/node-3.png 640x366)

#### 4.1.2 Node version

You can change the version of Node and install missing packages & tools in the runtime environment tab:

![Node action image](/docs/quickstart/node/node-4.png 640x493)

<Hint type="tip">

If your tests require a database to run, you can [attach it in the Services](/docs/actions/services-and-databases.md) tab:

![Services tab](/docs/quickstart/node/node-5.png 640x311)

</Hint>

### 4.2 Deploy application to server

The compiled application needs to be uploaded to the server. Head to the **Deploy & Remote OPS** section and select your upload action (**SFTP** in our case):

![File transfer actions](/docs/_global/transfer-actions-sftp.png 640x147)

When adding the action, you can choose what and where should be uploaded:

![SFTP target](/docs/_global/sftp-target-tab.png 640x239)

<Hint type="info">

[Targets](/docs/targets/introduction.md) allow to save pre-configured transfer server settings to be used across multiple actions.

</Hint>

#### 4.2.1 Node modules

<Hint type="warning">

Make sure to add `node_modules` to the ignore paths (**Transfer** tab) - we will install them on the server after the deployment in the next step.

![Ignore paths config](/docs/quickstart/node/node-6.png 640x533)

</Hint>

### 4.3 Install modules & restart application

Once the app is deployed, you can run additional commands on your server with the SSH action:

![SSH action selection](/docs//_global/add-ssh-action.png 640x221)

In this case, we shall run `npm install` and restart the application with a script:

![Restarting the application](/docs/quickstart/node/node-7.png 640x361)

### 4.4 (Optional) Send notification to Slack

You can configure Buddy to send your team a [message](/docs/actions/notifications.md) after the deployment. In this example we'll use Slack:

![Notification actions](/docs/_global/add-slack-notification.png 640x183)

<Hint type="info">

If you add this action in the <u>On Failure</u> tab, Buddy will only send the message if something goes wrong with your build or deployment.

![On failure notification](/docs/_global/slack-on-failure.png 640x208)

</Hint>

## 5. Summary

**Congratulations!** You have just automated your entire delivery process 🍾🙌. Make a push to the selected branch and watch Buddy fetch, build, and deploy your project to the production environment. With Continuous Delivery applied, you can now focus on what's really important: developing awesome apps! 🔥

<Hint type="success">

Bear in mind that this article is only a brief example of what Buddy can do. You can create additional pipelines for staging and production environments, integrate with your favorite services \(AWS, Google, Azure\), trigger tests on pull requests, build Docker images, and push them to the registry—the possibilities are unlimited.

</Hint>

<Hint type="tip">

If you want us to create a delivery pipeline for your project, drop a line to [support@buddy.works](mailto:support@buddy.works) – we'll be happy to help!

</Hint>

## Additional resources
- [How to set up a Continuous Deployment pipeline with Buddy](/docs/pipelines/pipeline-examples/continuous-deployment-pipeline.md)
<Hint type="info">
<actionsBox name="Node.js" slug="node-js" />
</Hint>


---
Original source: https://buddy.works/docs/basics/workflows/node