How to configure CI/CD for Azure Virtual Machine?

375 Views Asked by At

I'm just starting my first steps on the subject and I need help please. My application runs with Docker. I have a Azure Virtual Machine also with Docker installed, and the source code repository in DevOps. It is taking me a while to configure the CI/CD to deploy my application.

2

There are 2 best solutions below

0
On BEST ANSWER

I'm creating again and answering my own question because when I created it another time, it turned out to be "too big, and no one was going to give me a 10-page answer." Now after a lot of effort I achieved the goal and I want to share the knowledge acquired.

What I did was:

  • Connect to my VM using ssh.
  • Configure ssh on the VM and in DevOps, to be able to clone the repo using ssh from the VM (empty passphrases).
  • Clone the repo to the VM using ssh.
  • Add a self-hosted build agent to my VM and register it on my Azure DevOps project (Thanks to Mr. @Shamrai, check his answer for more info).
  • Create an Environment in DevOps to be able to point to my VM in the Pipeline.
  • Create the Pipeline

Pipeline:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:
- deployment: VMDeploy
  displayName: Deploy to VM
  environment:
   name: VM-ENV
   resourceName: VM
   resourceType: virtualMachine
  strategy:
     runOnce:
        deploy:
          steps:
          - checkout: self
            clean: true
            persistCredentials: true
          - script: git -C ~/Dir/to/repo pull
          - script: docker compose -f ~/Dir/to/repo/docker-compose.yml up -d

I hope it is helpful to someone else

2
On

You must add a self-hosted build agent to your VM and register it on your Azure DevOps project: Self-hosted Linux agents. Then you may run your pipelines on the VM (use the correct pool value). Here you may find a similar question: Create docker image using azure build pipeline, self-hosted agent and docker-desktop