Search code examples
gitazure-devopsazure-pipelinesazure-virtual-machinecicd

How to configure CI/CD for Azure Virtual Machine?


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.


Solution

  • 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