Azure-Pipeline Build Docker image

Azure-Pipeline Build Docker image

Create Pipeline using Azure Devops

Azure DevOps is a set of cloud-based collaboration tools for software development, encompassing a wide range of features for project planning, version control, continuous integration and delivery (CI/CD).

key components and features of Azure DevOps:

  1. Azure Boards: Azure Boards is a flexible Agile project management tool for planning, tracking, and managing software development projects. It supports Scrum, Kanban, and custom Agile methodologies, allowing teams to create and prioritize user stories, tasks, bugs, and epics.

  2. Azure Repos: Azure Repos provides Git and Team Foundation Version Control (TFVC) repositories for version control of your source code. It offers features such as branch management, pull requests, code reviews, and integration with popular IDEs and code editors.

  3. Azure Pipelines: Azure Pipelines is a fully-featured CI/CD service that enables you to automate build, test, and deployment processes for your applications. It supports building and deploying applications to various platforms, including cloud services, containers, mobile devices, and on-premises environments. Azure Pipelines integrates with Azure Repos, GitHub, Bitbucket, and other Git repositories.

  4. Azure Artifacts: Azure Artifacts is a package management service that allows you to create, host, and share packages such as NuGet, npm, Maven, and Python packages. It enables teams to manage dependencies and artifacts across projects and streamline the software delivery process.

  5. Azure Test Plans: Azure Test Plans provides a comprehensive testing solution for manual and exploratory testing, including test case management, test execution, and tracking of test results. It integrates with Azure Boards and Azure Pipelines to facilitate end-to-end testing workflows.

  6. Azure DevOps Repos: Azure DevOps Repos is a component of Azure DevOps that provides Git repositories for source control of your code.

Create First Azure pipeline :

Prerequisite:

Sign Up for Azure DevOps: If you don't have an Azure DevOps account, sign up for a free account at dev.azure.com.

To Create Azure pipeline--Go to Azure Devops Portal-->Go to Organization--> Click on New project As per snapshot

Enter Project name details and click on Create button

Now Go to your Project-->Azure Devops-->Go to Repos Click on import respository

Copy and clone any project link form github and import

https://github.com/Akv1998/example-voting-app.git

After import successful you will see projects files will be visible in azure repos.

Now Create Azure pipeline for this porject.

Click on pipeline-->Create pipeline-->Select Repository As azure repos git-->Select Azure-Devops respos-->Configure pipeline with build docker image--> Now it will ask for Docker file-->Select Docker file for directory--> Click on validate and configure

A yml page file will be open by default

Now go through the .yml file write agent pool-->(refere previous blog for setting up self hosted agent)-->https://abhivishwa.hashnode.dev/azure-devops-cicd-how-to-create-cicd-pipeline-on-self-hosted-agent

Now write .yml file like this add pool for self hosted agent in my case 'mycomputer' is name of selfhosted agent in my own machine.

The self-hosted agent receives the job from Azure Pipelines and executes the defined tasks and steps in the pipeline.

This is Docker file for a nodejs application:

FROM node:18-slim

# add curl for healthcheck
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl tini && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /usr/local/app

# have nodemon available for local dev use (file watching)
RUN npm install -g nodemon

COPY package*.json ./

RUN npm ci && \
 npm cache clean --force && \
 mv /usr/local/app/node_modules /node_modules

COPY . .

ENV PORT 80
EXPOSE 80

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["node", "server.js"]

Below YAML file generated by azure pipeline:


trigger:
 paths :
  include:
    - result/*

pool: 
 name: 'mycomputer'

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    steps:
    - task: Docker@2
      displayName: Build an image
      inputs:
        command: build
        dockerfile: '$(Build.SourcesDirectory)/result/Dockerfile'
        tags: |
          $(tag)

Now click on Save and run button

For the first time azure will ask for the permission to execute the task on self hosted agent (Make sure that agent is running and ready in background to take the jobs)

Now click on Athorize resources

Now Azure pipeline has been completed successfully image has been build with docker.

Conclusion:

In this details blog we coverd about azure Pipeline has create an azure pipeline project ,take code from azure repos build and image with docker and self hosted agent run successfuly build job on azure pipeline.