In a wonderful world where deploying an application is incredibly fast and amazing, where do we start? With Azure DevOps of course! In part 1 of CI/CD with Azure DevOps we are going to create a CI build.
As you can see above, we are pulling the latest Redis image, using the Deployment type for Kubernetes, creating two containers, and opening the default Redis port which is 6379.
Now that we have our code, we need a place to store it. Azure Repos is the option we'll go with as it's built in.
From our Azure DevOps project we're going to go to Repos > Files.
Pre-requisites
1. Azure account (https://azure.microsoft.com/en-us/free/)
2. Azure DevOps account (https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/create-organization?view=azure-devops)
3. An Azure DevOps Project Created (https://docs.microsoft.com/en-us/azure/devops/organizations/projects/create-project?view=azure-devops)
3. An Azure DevOps Project Created (https://docs.microsoft.com/en-us/azure/devops/organizations/projects/create-project?view=azure-devops)
First things first - What are building? We're going to build Redis containers. If you don't know what Redis is, it's an open-source application for database cache. It stores in-memory key-value data. Instead of your customer having to constantly make queries to the database, Redis stores values in cache.
The first thing we want to do is ensure we have our Kubernetes manifest with the proper values.
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
selector:
matchLabels:
app: redis
replicas: 2
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redistest
image: redis:latest
ports:
- containerPort: 6379
As you can see above, we are pulling the latest Redis image, using the Deployment type for Kubernetes, creating two containers, and opening the default Redis port which is 6379.
Now that we have our code, we need a place to store it. Azure Repos is the option we'll go with as it's built in.
From our Azure DevOps project we're going to go to Repos > Files.
If this is a new project, you'll see a screenshot similar to the above with your project details. This is because it's a new project with no existing repos. Let's go ahead and clone that down to our desktop. In the first section "Clone to your computer" copy the HTTPS link.
While still in PowerShell (I'm using Windows Terminal with PowerShell Core)
The reason why we want to do this is for segregation and so we can choose what path we want in the build pipeline. In production, you could have a repo with multiple files ranging from YAML to PowerShell to C# (.cs) to Python (.py). This is an attempt to get us into a best practice of clean repos.
Now we can open up that empty repo in VSCode and put our Redis Kubernetes Manifest inside.
Let's create a new file and write our Redis YAML file in it then save to the Redis directory.
To do that, click on File > New File > Navigate to your repo directory > Navigate to the Redis directory > Rename your file with a .yml extension and click save.
While still in PowerShell (I'm using Windows Terminal with PowerShell Core)
cd
into the repo and run mkdir redis
to create a new directory.The reason why we want to do this is for segregation and so we can choose what path we want in the build pipeline. In production, you could have a repo with multiple files ranging from YAML to PowerShell to C# (.cs) to Python (.py). This is an attempt to get us into a best practice of clean repos.
Now we can open up that empty repo in VSCode and put our Redis Kubernetes Manifest inside.
Let's create a new file and write our Redis YAML file in it then save to the Redis directory.
To do that, click on File > New File > Navigate to your repo directory > Navigate to the Redis directory > Rename your file with a .yml extension and click save.
Once back in VSCode, you should see something similar to the following:
Now we're ready to commit out code to the repo. Open back up PowerShell and run the following;
If we head back over to Azure Repos, we can see our commit.
Great! Now we have some code up in Azure and we're ready to create our build. Let's head over to Pipelines > Builds in the left pane right underneath Repos.
If your project is new you won't see any builds.
Go ahead and click "New pipeline" > click on "Use the classic editor > confirm your source is Azure Repos with the appropriate Team project, Repository, and branch > click continue
Awesome! So now we have our build, the repo where our code is coming from, and a default agent. Let's click on the agent and give it a name. In my case, I'm going to name it "RedisContainer". You can name it whatever you'd like.
For the agent pool, we can choose "Hosted VS2017". This is how and where you want your build to run for a specific platform. As you can see, there are options for OS X and Ubuntu. Everything else can be left blank.
Now we're ready to start adding some tasks. Click on the "+" button next to your agent name. Search for "Copy files" and "Publish build artifacts". Click on the "add" button next to both so they get added to your build.
Now that we have our build steps, let's click on "Copy Files to:" as it requires some information.
The first thing is the Display Name. I typically like to keep that default so I know what the build is doing. However, you could change it to something like "Copy Files To: For Redis Container Code
The second thing you'll see if the Source Folder. Click the three dots and choose your Redis folder.
For contents let's specify the type of file we want. In our case, it's a YAML file, so we'll type in "*.yml".
For the target folder, we're going to use the pre-defined build variable "$(Build.ArtifactStagingDirectory)". This build variable specifies the path on the agent where artifacts are currently stored to copy over before being push to the destination. For more information on pre-defined Azure DevOps variables, please visit: https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
For our Publish Artifact: drop step, we can leave that default (notice our pre-defined variable exists there).
Under the "Save & Queue" option, click "Save". Keep the folder as the default "\"
Congrats! You have specified your first build. Now let's go ahead and queue it up! Click on your build and select "Queue" on the top right and click "run".
Now we're ready to commit out code to the repo. Open back up PowerShell and run the following;
git add . (This git command adds the files to the staging area)
git commit -m "Pushing Redis Kubernetes manifest" (This git command adds a message for the commit so all other contributors or viewers can see what changes)
git push origin master
(This git command pushes the changes up to your specified branch. In our case, it's master)If we head back over to Azure Repos, we can see our commit.
Great! Now we have some code up in Azure and we're ready to create our build. Let's head over to Pipelines > Builds in the left pane right underneath Repos.
If your project is new you won't see any builds.
Go ahead and click "New pipeline" > click on "Use the classic editor > confirm your source is Azure Repos with the appropriate Team project, Repository, and branch > click continue
At the "Select a template" page let's choose "Empty job".
For the agent pool, we can choose "Hosted VS2017". This is how and where you want your build to run for a specific platform. As you can see, there are options for OS X and Ubuntu. Everything else can be left blank.
Now we're ready to start adding some tasks. Click on the "+" button next to your agent name. Search for "Copy files" and "Publish build artifacts". Click on the "add" button next to both so they get added to your build.
Now that we have our build steps, let's click on "Copy Files to:" as it requires some information.
The first thing is the Display Name. I typically like to keep that default so I know what the build is doing. However, you could change it to something like "Copy Files To: For Redis Container Code
The second thing you'll see if the Source Folder. Click the three dots and choose your Redis folder.
For contents let's specify the type of file we want. In our case, it's a YAML file, so we'll type in "*.yml".
For the target folder, we're going to use the pre-defined build variable "$(Build.ArtifactStagingDirectory)". This build variable specifies the path on the agent where artifacts are currently stored to copy over before being push to the destination. For more information on pre-defined Azure DevOps variables, please visit: https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
For our Publish Artifact: drop step, we can leave that default (notice our pre-defined variable exists there).
Under the "Save & Queue" option, click "Save". Keep the folder as the default "\"
Congrats! You have specified your first build. Now let's go ahead and queue it up! Click on your build and select "Queue" on the top right and click "run".

If your build ran successfully, you should see something similar to the screenshot below.
Next up we'll create a release from our build! Stay tuned.
Comments
Post a Comment