Surya.dev
Published on

Deploy Nodejs to Azure DevOps use Jenkins

Authors

You have a Node.js application that you'd like to integrate using Jenkins. Instead of deploying to Azure from Jenkins though, you need to hand over the build artifacts to Azure DevOps to complete the deployment. Perform the necessary steps to create a CI process on the Jenkins server and the CD process in Azure DevOps.

LAB: practice

Log in to the Azure portal using the provided credentials. Create an Azure DevOps Organization

  1. From the top search bar, search for and select Azure DevOps organizations. Image search bar devops organization
  2. Click the link for My Azure DevOps Organizations.

  3. Choose your region, and click Continue. Next click Create new Organization. Image create new organization
  4. Keep clicking Continue and answer any CAPTCHA challenges until you reach the page to set up your project.

  5. In Project Name, name your project MylearnDevOpsEngineer.

  6. Click Create Project. Image create project

Create Personal Access Token

In the top-right corner, click the User settings icon and open Personal access tokens in a new tab.

Image create project
Click New Token. Image new token

Fill in the token details:

  • Name: Enter suryadev.
  • Scopes: Select Full access. Image fill in the token details

Click Create.

Copy the token and save it in a separate file (like a text editor), then click Close. Image copy token and save

Download Self-Hosted Agent

At the top of the page, click the Azure DevOps icon, then select the project. Image select the project

On the bottom left, click Project settings.

Scroll down the left-hand pane and under Pipelines, select Agent pools. Image select the projectWe have here two pools, the Azure Pipelines pool, which are the agents provided by Microsoft.
Select the Default pool. Image select the project
Click New agent and select the Linux tab. here is going to give you instructions on how to download the agent. Image select the project

Click the Copy icon, next to Download to copy the agent URL to your clipboard.

https://vstsagentpackage.azureedge.net/agent/3.238.0/vsts-agent-linux-x64-3.238.0.tar.gz

Paste it to a text file to use in the following steps. Keep this tab and popup open.

The credential i have ready resources. But just to make sure, if you navigate back over to the Azure portal and then go over to your Virtual machines. Image VM ready
You can see here that we have two virtual machines. also, it's important to remember that when you are using the IP address, make sure that you're using the public IP address to SSH into the virtual machine. Image have two VM

you can check this reference my Blog Remote SSH Access Cloud with VSCode

Log in to the IinAgentVM virtual machine using the credentials provided in the lab:

ssh cloud_user@<PUBLIC_IP> 
Example: Image remote ssh server VM IinAgentVM

Change directory to the root:

cd ~

Make and navigate to a new directory called Downloads:

mkdir Downloads && cd Downloads
Example: Image create folders

Run wget against the agent URL you just copied:

wget <AGENT_URL>
Example: Image wget agent URL

Navigate back to the root (cd ~).

Create and navigate to another directory called myagent:

mkdir myagent && cd myagent

Unzip the package you just downloaded. You can copy this same command from the pop-up in Azure DevOps to get the full package and version number:

tar zxvf <PACKAGE>
Example: Image tar URL

Review the contents of the directory (ls).

Configure the Agent

Run the configuration script:

./config.sh

Type y to accept the license agreement.

When prompted for your server URL, go back to Azure DevOps and grab the URL from your browser. It will look something like https://dev.azure.com/MylearnDevOpsEngineerr/ , followed by some numbers (i.e., copy everything up to and not including /MyFirstProject). Paste this in the terminal.

Example: Image config script
  • press Enter to accept the default authentication type of PAT.
  • for personal access token, copy and paste the personal access token you created and saved earlier in this lab.
  • press Enter to accept the default agent pool.
  • press Enter to accept the default name.
  • press Enter to accept the default work folder.
Example output: Image config script

Run the agent interactively so that it is always listening for any jobs:

./run.sh

Set Up the Jenkins Server

Return to the main Azure portal (the first tab you opened), and click the top-left hamburger menu.

Click Virtual machines. Select the pre-provisioned Jenkins machine.

Example output: Image VM IP address jenkins

Copy the Public IP address, and open a new browser tab. Paste in the IP address followed by :8080.

Example output: Image open IP address jenkins NOTE: reference Seetup Jenkins from my blog

Open another terminal window, and log in to the Jenkins server using the credentials password:

ssh cloud_user@<JENKINS_PUBLIC_IP>
Example output: Image login jenkins

Obtain the initial password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password returned in the output.

Example output: Image copy password jenkins

Paste the password into the Jenkins installation wizard browser, and click Continue at the bottom.

Select Install suggested plugins, and wait for the installation process to finish.

Example output: Image wait installation process

On the Create First Admin User page:

  • Enter the Username suryadev_user.
  • Create a new password.
  • For Full name, enter Your Name.
  • For E-mail address, enter your email_address.

Click Save and Continue. Copy the Jenkins URL, save it to a text file to use later, and click Save and Finish.

Create CI Build

Fork the GitHub Repo

  1. In a new browser tab, copy and paste the following URL. Ensure you are logged in to GitHub with a personal account.

  2. In the upper right-hand corner, click Fork. For Owner, select yourself.

  3. Uncheck Copy the main branch only.

    Click Create fork.

    Example output: Image create fork
  4. Verify the following information:

    • The repo is forked to your personal account. (You will see your username in the browser URL.)
    • Click the main branch dropdown, and ensure that you see other branches.

Create the Build

Copy the forked page's URL (it will contain your GitHub username), and return to your Jenkins browser tab

Example output: Image create fork

On the Jenkins is ready! page, click Start using Jenkins.On the left, click New Item.

Example output: Image new item

Under Enter an item name, enter nodejs. Click Freestyle project. Then, click OK.

Example output: Image enter name and select freestyle project

Select the Source Code Management tab, and select Git.

Paste the repo URL into the Repository URL field (i.e., https://github.com/<YOUR_USERNAME>/content-az400-lab-resources)

In Branch Specifier, enter */jenkins. From the left menu, click Build Steps.

Example output: Image source code management git

Click Add build step, and select Execute shell from the dropdown menu.

Example output: Image add build step

In the Command field, enter:

zip -r archive.zip node_modules package.json server.js
Example output: Image command field

Under Post-build Actions, click Add post-build action, and select Archive the artifacts from the the dropdown menu.

In Files to archive, enter

archive.zip 

Click Save.

Example output: Image file ti archive

On the left-hand menu, select Build now.

Example output: Image file ti archive

In the lower left, click on your build, which should appear as #1.

Example output: Image select build 1

On the left-hand menu, click Console Output. Observe that it successfully finished.

Example output: Image select console output

On the left-hand menu, scroll up and select Status.

Under Build artifacts, you should see the archive.zip file.

Example output: Image select console output

Create CD Pipeline

Set Up Cloud Shell

Return to your main Azure portal browser tab, and open Cloud Shell by clicking on the icon immediately to the right of the search bar (looks like a caret (>) inside of a square).

Select PowerShell.

Example output: Image select powershell

Click Show advanced settings.

Example output: Image show advanced settings

For Storage account, click Create new. Enter a unique name. Under File share, select Create new, and type fileshare. Click Create storage.

Example output: Image show advanced settings

Create Azure Service Connection

Once you are connected to the Cloud Shell, get the subscription ID:

Get-AzSubscription
Example output: Image get azSubscription

Go back to your browser tab with the MyLearning-DevOps page in Azure DevOps, and click Project Settings in the lower left-hand corner.

On the left side, under Pipelines, click Service connections.

Click Create service connection.

Example output: Image service connection

On the New service connection menu, select Azure Resource Manager. Then, click Next.

Select Service principal (manual), and then click Next.

Example output: Image service principal manual

Go back to the Cloud Shell, and copy the subscription ID under Id.

Paste the subscription ID into the field under Subscription Id (in the New Azure service connection pane).

Example output: Image Subscription id

Back in PowerShell, copy the subscription name under Name.

Return to Azure DevOps, and paste the subscription name into the field under Subscription Name.

Under Service Principal Id and Service Principal key, paste in the principal ID and principal key provided in your hands-on lab credentials (listed as Application Client ID and Secret, respectively). These credentials are in a box labeled Service Principal. You may have to scroll or click the arrow to the right of your provided Azure and Jenkins credentials to see this box.

Return to PowerShell, and copy the tenant ID under TenantID.

Return to Azure DevOps, and paste the tenant ID into the field under Tenant ID.

Click Verify to check the settings.

Example output: Image Verify

In the Service connection name field, name the service connection SP for service principal.

Click Grant access permission to all pipelines.

Click Verify and save.

Example output: Image Verify and save

Create Jenkins Service Connection

In the top right-hand corner, click New service connection. Select Jenkins, and click Next.

Example output: Image new service connection jenkins

Paste the root Jenkins URL you copied and saved to a text file earlier in the lab into the Service URL field.

Click the checkbox for Accept untrusted SSL certificates.

For Username, enter suryadev, and type in the password you created while setting up the Jenkins installation.

Click Verify to make sure verification succeeded.

Example output: Image verification succeeded

Under Service Connection Name, enter Jenkins. Click Grant access permission to all pipelines. Click Verify and save.

Example output: Image Grant access permission

Create the Pipeline

  1. To enable you to see the Releases option under the Pipelines icon,select AzureDevOps in the top right. In the bottom right, click Organization settings. Under the Pipelines category on the left, choose Settings. Turn off the Disable creation of classic build pipelines and the Disable creation of classic release pipelines options.

    Example output: Image disable of release pipelines
  2. On the left-hand menu, click the Pipelines icon. Then, select Releases.

    Example output: Image release
  3. Click New pipeline.

    Example output: Image new pipeline
  4. Under Select a template, click Empty job, and then Apply.

    Example output: Image new pipeline
  5. Click Add an artifact.

  6. In the Add an artifact pane, click 5 more artifact types.

    Example output: Image new pipeline
  7. Select Jenkins from the artifact types.

  8. In the Service connection dropdown menu, select Jenkins.

  9. In the Jenkins Job dropdown, select nodejs.

  10. Click Add.

    Example output: Image new pipeline
  11. Under Stage 1, click the 1 job, 0 task link.

  12. Select the Agent job task on the left.

    Example output: Image select agent job
  13. On the right pane, change the selection for Agent pool to Default.

    Example output: Image change the selection agent pool
  14. Click the plus sign (+) next to Agent job.

    Example output: Image click the plus sign
  15. In the search bar of the Add tasks pane, type Azure web. Select Azure Web App, and click Add.

    Example output: Image change the selection agent pool
  16. Click the Azure Web App Deploy tile, and set the following values:

    • Azure subscription: Select SP.

    • App type: Select Web App on Linux.

    • App name: Select the only app name that's available. (If the name does not auto-populate, you can get the name from the Azure portal.)

    Example output: Image change the selection agent pool
  17. Click Save, near the top right. Click OK.

    Example output: Image save Example output: Image click OK
  18. Near the top right, next to Save, click Create release.

    Example output: Image click release
  19. Under Artifacts, select the only available version. Click Create.

    Example output: Image click release
  20. In the green bar on the top, click the link for Release-1. You can close out any pop-ups.

    Example output: Image release-1
  21. Under the Stage 1 box, click Logs to view the progress.

    Example output: Image click release
  22. Wait until the Azure web application has successfully deployed. This may take several minutes.

    Example output: Image successfully deployed