Unit V Building Devops Pipelines Using Azure Notes
Unit V Building Devops Pipelines Using Azure Notes
Create Github Account, Create Repository, Create Azure Organization, Create a new pipeline, Build a sample
code, Modify azure-pipelines. yaml file.
Contents
1. Create Github Account ------------------------------------------------------------------------------------------------------------ 1
2. Create Repository ------------------------------------------------------------------------------------------------------------------ 3
3. Create Azure Organization ------------------------------------------------------------------------------------------------------- 5
4. Create a new pipeline ------------------------------------------------------------------------------------------------------------- 6
5. Build a sample code --------------------------------------------------------------------------------------------------------------- 8
6. Modify azure-pipelines. yaml file--------------------------------------------------------------------------------------------- 11
2 MARKS:---------------------------------------------------------------------------------------------------------------------------------- 14
Creating a GitHub account is a straightforward process. Here’s a step-by-step guide to help you set up
your GitHub account:
1. On the GitHub homepage, click the "Sign up" button in the upper right corner of the page.
1. Email Address:
o Enter a valid email address you want to use for your GitHub account.
2. Password:
o Choose a strong password. GitHub will show you the strength of your password as you type.
3. Username:
o Pick a unique username that will be your GitHub handle. This username will be part of your
GitHub profile URL.
4. Verification:
o GitHub may ask you to complete a CAPTCHA to prove you're not a robot.
4. Choose a Plan
1. Free Plan:
1
o The free plan provides unlimited public repositories and up to 3 private repositories with
limited features.
2. Paid Plans:
o If you need more private repositories or additional features, you can choose from the paid
plans.
3. Click "Continue":
o After choosing a plan, click the "Continue" button.
1. Personalization:
o GitHub will offer to customize your experience with some optional settings. You can fill these
out or skip them by clicking "Skip this step".
2. Email Preferences:
o Set your email preferences for notifications and updates from GitHub.
1. Sign In:
o After verifying your email, sign in to GitHub with your new credentials.
2. Complete Your Profile:
o You can now set up your GitHub profile by adding a profile picture, bio, and other details.
8. Explore GitHub
1. Create a Repository:
o Once logged in, you can create a new repository by clicking the "New" button on the
repositories page.
2. Install Git:
o To start using Git with GitHub, install Git on your local machine if you haven't already. You can
download it from git-scm.com.
3. Configure Git:
o Configure Git with your GitHub credentials using the following commands in your terminal:
bash
2
2. Create Repository
Creating a repository on GitHub allows you to start organizing and managing your projects. Here’s a
step-by-step guide on how to create a repository on GitHub:
1. Log In to GitHub
1. Repository Name:
o Name: Enter a name for your repository. The name must be unique within your account or
organization.
2. Description (Optional):
o Description: Provide a brief description of what your repository is about. This is optional but
recommended.
3. Visibility:
o Public: The repository is visible to everyone on the internet.
o Private: The repository is visible only to you and people you explicitly share it with.
4. Initialize This Repository with (Optional):
o README file: A README file provides information about your project. You can initialize your
repository with a README file by checking this box.
o .gitignore: Choose a .gitignore template to specify files and directories to ignore. You can
select a template that matches your project’s language or framework.
o License: Add a license to specify the terms under which your code can be used by others.
GitHub offers various common licenses to choose from.
5. Add a .gitignore Template (Optional):
o If you selected .gitignore, choose a template relevant to your project type from the
dropdown menu.
6. Choose a License (Optional):
o If you want to add a license, select one from the dropdown menu. GitHub offers several
options such as MIT, GPL, Apache, etc.
1. Create Repository:
o After filling in the details, click the "Create repository" button at the bottom of the page.
3
5. Add Files to Your Repository
Once the repository is created, you can start adding files to it. You have several options for adding
files:
bash
bash
cd repository-name
bash
git add.
git commit -m "Initial commit"
git push origin main
4
3. Create Azure Organization
Creating an Azure organization involves setting up an Azure Active Directory (Azure AD) tenant,
which serves as the foundational directory service for managing users, groups, and applications in
your Azure environment. Here’s a step-by-step guide to creating an Azure organization:
4. Additional Configurations
1. Configure Applications:
o You can register and configure applications in Azure AD by going to "App registrations". This
is where you manage application access and settings.
2. Set Up Multi-Factor Authentication (MFA):
o For enhanced security, configure MFA by going to "Security" > "Multi-Factor Authentication".
5
3. Set Up Conditional Access Policies:
o Implement conditional access policies to control how and when users can access resources by
navigating to "Security" > "Conditional Access".
Creating a new pipeline in Azure DevOps allows you to automate the build, test, and deployment
processes for your application. Azure DevOps Pipelines support Continuous Integration (CI) and
Continuous Deployment (CD). Here’s a detailed guide on how to create a new pipeline:
1. Select a Project:
o From the Azure DevOps homepage, select the project where you want to create the pipeline.
If you don’t have a project, you can create one by clicking "New Project" and following the
prompts.
1. Open Pipelines:
o In the left-hand menu, click on "Pipelines".
2. Create Pipeline:
o Click the "New Pipeline" button.
1. Select a Repository:
o Choose the source for your code. You can select from various options such as:
Azure Repos Git: If your code is stored in Azure Repos.
GitHub: If your code is stored in GitHub.
Bitbucket Cloud: If your code is stored in Bitbucket.
GitLab: If your code is stored in GitLab.
Other Git: For other Git repositories.
Import a repository: If you need to import from another service.
6
o Authenticate and authorize access if necessary.
2. Configure Pipeline YAML:
o Starter Pipeline: You can start with a basic YAML pipeline template or use an existing YAML
file.
o Existing YAML File: If you have a YAML file ready, you can select it from your repository. This
file defines your pipeline's steps and configuration.
3. Use Classic Editor (Optional):
o If you prefer a graphical interface, click on "Use the classic editor" instead of configuring the
YAML file. This will guide you through a UI-based configuration.
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '5.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
7
7. Monitor Pipeline Execution
1. Pipeline Runs:
o Monitor the execution of your pipeline from the "Pipelines" section. You can view logs, status,
and details of each run.
2. Review Results:
o Review build and deployment results, including any errors or warnings. Analyze logs and
metrics to ensure everything runs smoothly.
Building a sample code typically involves writing a small application or script to demonstrate a
particular concept, technology, or framework. For simplicity, I'll provide a basic example of a sample
code using a popular language, Python, to illustrate fundamental programming concepts.
This Python script will create a simple command-line calculator that can perform basic arithmetic
operations: addition, subtraction, multiplication, and division.
1. Sample Code
# Simple Calculator
return x + y
return x - y
return x * y
if y == 0:
8
return "Error! Division by zero."
return x / y
def main():
while True:
operation = input("Choose operation (add, subtract, multiply, divide) or 'exit' to quit: ").strip().lower()
if operation == 'exit':
break
continue
try:
except ValueError:
continue
if operation == 'add':
9
print(f"Result: {subtract(num1, num2)}")
if __name__ == "__main__":
main()
2. Explanation
1. Functions: The code defines four functions (add, subtract, multiply, divide) that perform
basic arithmetic operations.
o add: Returns the sum of two numbers.
o subtract: Returns the difference between two numbers.
o multiply: Returns the product of two numbers.
o divide: Returns the quotient of two numbers and handles division by zero.
2. main Function: This is the main function where the program starts.
o It presents a menu to the user to choose an operation or exit.
o It takes user input for the operation and numbers.
o Based on the operation chosen, it calls the appropriate function and prints the result.
o It handles invalid operations and non-numerical inputs gracefully.
3. Error Handling: The script includes basic error handling for division by zero and non-
numeric input.
4. Loop: The while loop allows the user to perform multiple calculations until they choose to
exit.
To run the code, save it in a file named calculator.py, and execute it using Python:
bash
python calculator.py
You’ll interact with the command line to perform calculations using the provided operations.
10
6. Modify azure-pipelines. yaml file
Modifying an azure-pipelines.yml file allows you to customize and enhance your Azure DevOps
pipeline configurations. Below is a basic guide on how to modify a YAML pipeline file to achieve
different tasks, such as adding steps, setting variables, or integrating testing and deployment.
Here's a simple YAML pipeline file for a .NET application that includes stages for building, testing,
and deploying. I'll show you how to modify it for various needs.
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- task: CopyFiles@2
inputs:
contents: '**/*.dll'
targetFolder: '$(Build.ArtifactStagingDirectory)'
displayName: 'Copy files to: $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
displayName: 'Publish Artifacts'
To change which branches trigger the pipeline, modify the trigger section:
yaml
trigger:
branches:
11
include:
- main
- development
yaml
variables:
buildConfiguration: 'Release'
outputDirectory: '$(Build.ArtifactStagingDirectory)/output'
To add a new step, such as running a script or a task, append it to the steps section:
yaml
yaml
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '7.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
To add a deployment stage, you can define a new stage and include deployment tasks:
yaml
stages:
- stage: Build
jobs:
- job: Build
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'Build project'
- script: dotnet test --configuration $(buildConfiguration)
displayName: 'Run tests'
- stage: Deploy
jobs:
- job: Deploy
steps:
12
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'specific'
project: '$(System.TeamProjectId)'
pipeline: '1'
buildVersionToDownload: 'latest'
downloadType: 'single'
artifactName: 'drop'
- task: AzureWebApp@1
inputs:
azureSubscription: 'your-service-connection'
appName: 'your-app-name'
package: '$(Pipeline.Workspace)/drop/**/*.zip'
6. Configure Notifications
To set up notifications for the pipeline’s success or failure, use the following YAML:
yaml
notifications:
- event: success
recipients:
- email@example.com
subject: Pipeline Succeeded
body: The pipeline has completed successfully.
- event: failure
recipients:
- email@example.com
subject: Pipeline Failed
body: The pipeline has failed. Please check the logs for details.
Additional Tips
Test Changes: After modifying the YAML file, commit and push the changes to your repository. Azure
DevOps will automatically trigger the pipeline if it’s configured to do so.
Validate YAML Syntax: Ensure your YAML file is correctly formatted. You can use online YAML
validators or the Azure DevOps pipeline editor to validate syntax.
Use Templates: For complex pipelines, consider using YAML templates to modularize your pipeline
configuration.
This guide provides a starting point for modifying your Azure Pipelines YAML file to suit your
project’s needs. Adjust the pipeline configuration based on your specific requirements and
environments.
13
2 MARKS:
1. How do you create a new repository on GitHub and what are the options available
during initialization?
Answer:
2. Explain how to set up a basic Azure DevOps pipeline for a Java project using
Maven.
Answer:
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Maven@3
inputs:
mavenVersionOption: 'Default'
goals: 'clean install'
displayName: 'Build with Maven'
4. Save and Run: Save the YAML file and commit it.
Answer:
1. Update YAML:
yaml
14
stages:
- stage: Build
jobs:
- job: Build
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
- stage: Deploy
jobs:
- job: Deploy
steps:
- task: AzureWebApp@1
inputs:
azureSubscription: 'your-service-connection'
appName: 'your-app-name'
package: '$(Pipeline.Workspace)/drop/**/*.zip'
displayName: 'Deploy to Azure Web App'
4. Describe how to add and use environment variables in an Azure DevOps pipeline
YAML file.
Answer:
1. Define Variables:
yaml
variables:
buildConfiguration: 'Release'
outputDirectory: '$(Build.ArtifactStagingDirectory)/output'
2. Use Variables:
yaml
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'Build project'
- task: CopyFiles@2
inputs:
contents: '**/*.dll'
targetFolder: '$(outputDirectory)'
displayName: 'Copy files'
3. Save and Commit: Save the YAML file and commit it.
15
5. How can you create a GitHub repository using the GitHub CLI?
Answer:
6. What is the purpose of the trigger section in an Azure DevOps YAML pipeline
file?
Answer: The trigger section defines which branches or paths will automatically start the pipeline
when changes are detected.
7. How can you include a deployment step in an Azure DevOps pipeline YAML file to
deploy to an Azure Kubernetes Service (AKS)?
Answer:
1. Update YAML:
yaml
stages:
- stage: Deploy
jobs:
- job: Deploy
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'your-service-connection'
scriptType: 'bash'
scriptPath: 'deploy.sh'
arguments: '-f deployment.yaml'
displayName: 'Deploy to AKS'
8. Describe how to use GitHub Actions to automate a workflow for a Node.js project.
Answer:
yaml
name: Node.js CI
on:
16
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm install
- run: npm test
env:
CI: true
Answer:
1. Update YAML:
yaml
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
Answer:
1. Create Pipeline: Go to Azure DevOps and select "Pipelines" > "New Pipeline".
2. Select GitHub: Choose GitHub as the source and authenticate if necessary.
3. Configure: Follow the setup steps to configure pipeline settings and YAML file.
11. What is the purpose of the pool section in an Azure DevOps pipeline YAML file?
Answer: The pool section specifies the type of agent or virtual machine that will be used to run the
pipeline jobs.
17
12. How do you configure continuous integration (CI) for a Python project using
Azure DevOps?
Answer:
yaml
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
- script: pytest
displayName: 'Run tests'
13. How can you define a custom Docker image for a job in an Azure DevOps
pipeline?
Answer:
1. Update YAML:
yaml
pool:
vmImage: 'ubuntu-latest'
container: 'my-custom-image:latest'
14. Describe the process for setting up a GitHub Actions workflow to automatically
deploy a website to GitHub Pages.
Answer:
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm install
- run: npm run build
- uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
15. How can you set up an Azure DevOps pipeline to automatically trigger on pull
requests?
Answer:
1. Update YAML:
yaml
trigger:
branches:
exclude:
- '*'
pr:
branches:
include:
- main
16. What is the purpose of the steps section in an Azure DevOps YAML pipeline file?
Answer: The steps section defines a sequence of tasks or scripts to be executed in the pipeline job.
19
17. How can you configure a pipeline in Azure DevOps to use a self-hosted agent
pool?
Answer:
1. Update YAML:
yaml
pool:
name: 'MySelfHostedAgentPool'
18. Describe how to add a manual approval step before deployment in an Azure
DevOps pipeline.
Answer:
1. Update YAML:
yaml
stages:
- stage: Deploy
jobs:
- job: Deploy
steps:
- task: AzureWebApp@1
inputs:
azureSubscription: 'your-service-connection'
appName: 'your-app-name'
package: '$(Pipeline.Workspace)/drop/**/*.zip'
displayName: 'Deploy to Azure Web App'
- stage: Approval
dependsOn: Deploy
jobs:
- job: Approval
steps:
- script: echo "Manual Approval Needed"
displayName: 'Manual Approval'
Answer:
20
yaml
on:
schedule:
- cron: '0 0 * * *'
jobs:
run:
runs-on: ubuntu-latest
steps:
- run: echo "Running scheduled job"
Answer:
1. Create .gitignore File: Add a .gitignore file in the root of your repository.
2. Add Patterns: Specify patterns for files and directories to be excluded from version control.
gitignore
node_modules/
*.log
.env
21