Deploy between different environments with variable groups – Azure DevOps

Group variables is a functionality provided by Azure pipelines that let one handle a lot of variables as one entity. It also supports key vault integration but also secrets on comparison with the standard pipeline variables which should not be used as secrets according to Microsoft, as their value can be seen.

In this article I will describe how you can use variable groups for different deployments based on the environment you work. For example the company may want to differentiate the variables for a product on the test and production version. This could be injected and handled on the pipeline accordingly with group variables and parameters.

This article refers to a product which has the same variables (version, password, environment) on test and prod, but their values are different.

In order to create a variable group you should go to Pipelines -> Library -> +Variable group.

The values which I provided on prod variable group are below:

Accordingly the same values exist for test.

The pipeline will trigger when a commit is merged on the main branch.

- main

  - name: environment
    displayName: Where to deploy?
    type: string
    default: test
    - prod
    - test

  vmImage: ubuntu-latest

 - group: ${{parameters.environment}}


- script: |
    echo $(ENV)
    echo $(VERSION)
  displayName: Step 1 - print version and environment

- script: echo $(PASSWORD)
  displayName: Step 2 - print secret

- script: pwd ENV ${{parameters.environment}}
  displayName: Step 3 - print parameter

By running the pipeline the type of environment will be asked

Depending on the selected input, the group variables of the specific category will be printed. If I run with test as input then I will get the test version and env variable.

Create a build pipeline and push Image to external repository with Azure Devops

Azure Devops is the powerful Microsoft product for Devops solutions. In this article I will explain how you can create a build pipeline using predefined actions and tools provided in order to push an image to an external repository like Docker hub.

As a first step you should create two new service connections. As I am using Github, the one will be a github connection and a docker hub connection. To accomplish that you should go to project settings -> service connections and connect your accounts with your password credentials.

When you complete this step, the connected accounts will appear.

Then you should go to pipelines menu and create a new one. My pipeline has the name main-pipeline.

Azure Devops provides a large list of predefined tasks that will make your implementation easier and quicker. In my case I selected the build of a Dockerfile that will be listed in the Github repository.

In more detail the code will be checkout from the repository and the image will be created using a building machine that Azure provides from a shared pool of agents.

You can find the pipeline code below:

# Docker
# Build a Docker image

- master

- repo: self

  tag: 'latest'

- stage: Build
  displayName: Build image
  - job: pushToDocker
    displayName: pushToDocker
    - task: Docker@2
      displayName: Build an image
        containerRegistry: 'geralexgr-docker-repo'
        repository: 'geralexgr/aksjavarepo'
        command: 'buildAndPush'
        Dockerfile: '**/Dockerfile'
        tags: latest
    - task: PublishBuildArtifacts@1
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'

A successful run of the pipeline is shown below. As the code indicates the tag of the image should be the latest.

The created image will be stored on docker hub as indicated in the instructions.

You can then go and pull your image locally to test the result. In my case I would use:

docker pull geralexgr/aksjavarepo:latest

Create a self hosted agent on local machine for Azure Devops

Azure Devops services provide great functionality for deployment solutions. One of them is the local agent creation if you do not have a cloud/hosted available server.

In this article I will explain the procedure on how to create one.

Go to your project on your organisation and select agent pools. You can choose either the default or create a new one.

When selecting your agent pool, you should press on new agent. There you can find instructions on how to create your agent. In my case it is a Mac OS X machine

Download the files and follow the instructions. The configuration steps for Mac OS X ask for the server URL. You should input the below:

Then you should select PAT authentication and generate a new token from your account panel -> personal access tokens.

Input the access permissions for the token and the expiration date. Also copy the key on a safe place. If you do not provide sufficient access on the token, you will get errors during the configuration.

When the configuration is completed agent will try a test connection.

When the above step is finished, try to run the agent.

./ on macosx

If the procedure is succeful you will see your machine ready to accept jobs

Also the agent should appear online under agents tab