Copying Files from Git Repository to Azure Storage in Azure DevOps

Below are a few scenarios where you would like to upload files from Git repos to Azure Storage Account

  1. Upload the Linked ARM (Azure Resource Manager) Templates to Storage Accounts for execution.
  2. Uploading any reports for future reference.
  3. Upload any static resources (like images, CSS, and JS files)

Prerequisites

Please ensure, you create the below.

  • Create an Azure Storage Account
  • Create a Storage Container
    Dashboard

Let’s now understand how to copy the files from the git repository to Azure Storage within the Azure DevOps CI/CD pipelines.

Below are the steps to be performed in order to implement the copy functionality.

  1. Download the code from Azure DevOps Git Repository to Azure Pipelines Agent
  2. Copy the Files from the Agent Directory to Azure Storage Container using Azure CLI.

Step 1. Download the code

In order to download the code from the Azure DevOps. Git repository, we need to add a (optional) step called checkout using the below code

Storage

Below is the output of the step once it is added to the YAML Pipeline.

Azure DevOps

As shown in the above screen capture, the checkout step downloads the code from the git repo to a folder called s (/home/vests/work/1/s). This folder can be referenced using an Azure DevOps pre-defined variable called System.DefaultWorkingDirectory

Once the code is available, we need to add another Azure DevOps task which could upload the code from the Agent’s local folders to Azure Storage Account. Let’s understand how to implement the same.

Step 2. Copy files from folder s to the Storage Account container

Let’s now add a new Azure CLI step to execute the Azure CLI command which copies the files/folders from the local Agent directory to the Azure Storage Account as shown below.

Azure CLI

Once you click on the Add button in the previous step, the below YAML code will be added.

- task: AzureCLI@2
  displayName: Copy Files
  inputs:
    azureSubscription: 'azure-dev-serviceconnection'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: 'az storage blob upload-batch --source $(System.DefaultWorkingDirectory) --destination $(ContainerName) --account-name $(StorageAccountName) --overwrite'

The above command is self-explanatory. Please feel free to post your questions in the comments box at the bottom of this article.

Once you run the pipeline, you can see the logs which show the files are uploaded successfully.

Pipeline

Let’s also verify if the files have been uploaded to the Storage Container. Below is the screen capture of the Container where the files are uploaded.

Overview

Below is the complete YAML Code.

variables:
  
  - name: StorageAccountName
    value: azdevopsdeveusstrgacc1
  
  - name: ContainerName
    value: ado-tips

steps:
  
  - checkout: self
  
  - task: AzureCLI@2
    displayName: Copy Files
    inputs:
      azureSubscription: 'azure-dev-serviceconnection'
      scriptType: 'pscore'
      scriptLocation: 'inlineScript'
      inlineScript: 'az storage blob upload-batch --source $(System.DefaultWorkingDirectory) --destination $(ContainerName) --account-name $(StorageAccountName) --overwrite'

Summary

In this article, we have learned how to write the Azure DevOps – YAML-based pipeline for uploading the files from Azure DevOps – Git repo to an Azure Storage Account.