In this article, we are going to learn how to copy/upload the files from Git repos to Azure Storage Accounts.
Below are few scenarios where you would like to upload files from Git repos to Azure Storage Account
- Upload the Linked ARM (Azure Resource Manager) Templates to Storage Accounts for execution.
- Uploading any reports for future references.
- Upload any static resources (like images, CSS, JS files)
Please ensure, you create the below.
- Create Azure Storage Account
- Create a Storage 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.
Below are the steps to be performed in order to implement the copy functionality.
- Download the code from Azure DevOps Git Repository to Azure Pipelines Agent
- 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
Below is the output of the step once it is added to the YAML Pipeline.
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
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.
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 shows the files are uploaded successfully.
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.
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'
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