- Published on
Deploy Nodejs to Azure DevOps use Jenkins
- Authors
- Name
- Surya Harahap
- @suryaharahap18
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
- From the top search bar, search for and select Azure DevOps organizations.
Click the link for My Azure DevOps Organizations.
- Choose your region, and click Continue. Next click Create new Organization.
Keep clicking Continue and answer any CAPTCHA challenges until you reach the page to set up your project.
In Project Name, name your project
MylearnDevOpsEngineer
.- Click 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.
Fill in the token details:
- Name: Enter
suryadev
. - Scopes: Select
Full access
.
Click Create.
Download Self-Hosted Agent
Azure DevOps
icon, then select the project. On the bottom left, click Project settings.
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.
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. 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:
Change directory to the root:
cd ~
Make and navigate to a new directory called Downloads:
mkdir Downloads && cd Downloads
Example:
Run wget against the agent URL you just copied:
wget <AGENT_URL>
Example:
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:
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:
- 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:
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:
Copy the Public IP address, and open a new browser tab. Paste in the IP address followed by :8080.
Example output: 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:
Obtain the initial password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password returned in the output.
Example output:
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:
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
In a new browser tab, copy and paste the following URL. Ensure you are logged in to GitHub with a personal account.
In the upper right-hand corner, click Fork. For Owner, select yourself.
Uncheck Copy the
main
branch only.Click Create fork.
Example output: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:
On the Jenkins is ready! page, click Start using Jenkins.On the left, click New Item.
Example output:
Under Enter an item name, enter nodejs. Click Freestyle project. Then, click OK.
Example output:
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:
Click Add build step, and select Execute shell from the dropdown menu.
Example output:
In the Command field, enter:
zip -r archive.zip node_modules package.json server.js
Example output:
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:
On the left-hand menu, select Build now.
Example output:
In the lower left, click on your build, which should appear as #1.
Example output:
On the left-hand menu, click Console Output. Observe that it successfully finished.
Example output:
On the left-hand menu, scroll up and select Status.
Under Build artifacts, you should see the archive.zip
file.
Example 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:
Click Show advanced settings.
Example output:
For Storage account, click Create new. Enter a unique name. Under File share, select Create new, and type fileshare. Click Create storage.
Example output:
Create Azure Service Connection
Once you are connected to the Cloud Shell, get the subscription ID:
Get-AzSubscription
Example output:
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:
On the New service connection menu, select Azure Resource Manager. Then, click Next.
Select Service principal (manual), and then click Next.
Example output:
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:
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:
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:
Create Jenkins Service Connection
In the top right-hand corner, click New service connection. Select Jenkins, and click Next.
Example output:
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:
Under Service Connection Name, enter Jenkins. Click Grant access permission to all pipelines. Click Verify and save.
Example output:
Create the Pipeline
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:On the left-hand menu, click the Pipelines icon. Then, select Releases.
Example output:Click New pipeline.
Example output:Under Select a template, click Empty job, and then Apply.
Example output:Click Add an artifact.
In the Add an artifact pane, click 5 more artifact types.
Example output:Select Jenkins from the artifact types.
In the Service connection dropdown menu, select Jenkins.
In the Jenkins Job dropdown, select nodejs.
Click Add.
Example output:Under Stage 1, click the 1 job, 0 task link.
Select the Agent job task on the left.
Example output:On the right pane, change the selection for Agent pool to Default.
Example output:Click the plus sign (+) next to Agent job.
Example output:In the search bar of the Add tasks pane, type Azure web. Select Azure Web App, and click Add.
Example output: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:Click Save, near the top right. Click OK.
Example output: Example output:Near the top right, next to Save, click Create release.
Example output:Under Artifacts, select the only available version. Click Create.
Example output:In the green bar on the top, click the link for Release-1. You can close out any pop-ups.
Example output:Under the Stage 1 box, click Logs to view the progress.
Example output:Wait until the Azure web application has successfully deployed. This may take several minutes.
Example output: