CI/CD Pipeline using YAML file
This is blog is intended to share information about creating a CI / CD Pipeline using YAML file.
Case Study
Create a CI/CD pipeline using YAML for a web application
So, let’s start with lets creating a project
Use cmd to create a project and then bind it to a solution
- Create Solution
dotnet new sln -o Test_CI_CDPipeline
- Then change the directory
cd Test_CI_CDPipeline
- Then create a Web Project inside Test_CI_CDPipeline
dotnet new mvc -n Test_CI_CDPipeline
- Now bind the project to solution
dotnet sln HelloWorldApp.sln add HelloWorldApp.Web\HelloWorldApp.Web.csproj
- Need to restore all the dependencies
dotnet restore
- Now, build the solution. Do not restore dependencies while we are building the solution.
dotnet build –no-restore –configuration release
- Publish the solution
dotnet publish –no-build –configuration release.
Now a project is created, built, and published on local Machine. The aim is to achieve the same using a continuous integration pipeline. For this one need to have an Azure DevOps account. In that, create an organization (if not created already) and then create a project. You can either create a private project or a public project.
Before that, one needs to initialize git in the location machine. Go to the working folder (location of the location file).
- Initiate the git
git init
The purpose of git is to host the source code. Ensure that binaries created while building a project should not be included. For this add gitignore file to the project.
- Now, you need to stage all the content
git add .
- Commit the content
git commit -m “—Name-it—”
- Now you can push this to the Azure DevOps repo.
Copy and paste the command and everything from the local machine will be pushed to Azure Repo.
Create Pipeline
Go to Pipelines on the Left panel and click on Create Pipeline. It gives two options YAML or Classic editor. Select Azure Repos Git (YAML) as it will be used to push the local project in further steps
Select the project created earlier
Select the type of project you are creating
After this, you will see a YAML file. Here one needs to edit the file and make changes as per the need. Below is a sample code
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
trigger:
- master
pool:
vmImage: ubuntu-latest
variables:
buildConfiguration: 'Release'
steps:
- task: DotNetCoreCLI@2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)' # Update this to match your need
- task: DotNetCoreCLI@2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifactName: 'myWebsiteName'
Trigger: specifies which branches cause a continuous integration build to run
Pool: specifies which pool to use for a job of the pipeline. A pool specification also holds information about the job’s strategy for running.
Steps: specifies a list of tasks that need to be run
Task: specifies commands/tasks that need to perform to achieve a build file.
- Build the project first
- Publish the Project
- Publish it as an Artifact
Click save and run.
Below shows the artifact created
Now, this artifact needs to be consumed by an Azure website. For this, one needs to create a web application. Create an app service for that in the Azure portal.
We need to create a service connection to connect Azure resource. Go to Project Setting, then service connections
Create a New connection and select Azure Resource Manager
Choose Automated (here you need to login with the same credentials that you use for the Azure portal) and click next
Choose the subscription and Resource group. Give a name to the connection
Now, proceed with creating a release pipeline
Select the template you want to use and then apply.
Choose the artifact from where you want the files to be picked up and click add
Click on Job to a stage
Add your Azure subscription, select the app service you have created, and save it.
Then click on Create release
Click on Deploy
Once you are done deploying, click on the URL from the App service