Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account

In this article, we are going to learn how to copy/upload the files from Git repos to Azure Storage Accounts.

Introduction

Below are 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 references.
  3. Upload any static resources (like images, CSS, JS files)
Advertisements

Prerequisites

Please ensure, you create the below.

  • Create Azure Storage Account
  • Create a Storage Container
Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Empty Container

Copy Files from Git Repository to Azure Storage Account

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

Advertisements

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.

Step1: Download the code

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

Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Checkout Step

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

Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Checkout Step Output

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

Advertisements

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

Step2: Copy files from folder s to Storage Account container

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

Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Copy To Storage Account

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

Advertisements
- 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 shows the files are uploaded successfully.

Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Copy To Storage Account – Output

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.

Azure DevOps – Tips and Tricks – 11 – Copy Files from Git Repository to Azure Storage Account – Copy To Storage Account – Output

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'
Advertisements

Summary:

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

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Advertisements
Advertisements
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s