A couple of weeks ago I did a post about how I did a devops pipeline which could take a copy of a sample I had and copy it to my GitHub repo for sharing with the community even though I tend to keep all of my samples in an Azure DevOps private repo because thats where ive kept all of my personal stuff for years but sometimes you want to share certain samples publically. Based on the feedback I got a few other people found this useful.

The sample I did in the last article was using a classic pipeline but I also had to do this for a solution which was using a yaml pipeline so I thought id share that one too.

The pipeline will do the following:

  • Import variables from an Azure DevOps library which contains my github details (more later)
  • The pipeline is in the repo that we want to use to copy across, but you can do other repo references yourself in the pipeline if your scenario differs
  • The pipeline makes a local folder called git along side the repo folder
  • The pipeline will clone the remote repo to the local git folder
  • The pipeline will replace whats in the repo with the code from the Azure Devops repo that you want to copy across. There is a bash step to configure what you want to share if for example you have a repo with a few samples in but you only want to share specific ones
  • The pipeline will then push the changes back to github and label it with the build number from the pipeline run

The pipeline yaml is below.

     

#None trigger will allow us to run the pipeline manually when required
trigger: none


stages:
- stage: Build
  jobs:
    - job: Build
      variables:
        - group: GitHub_Settings  
        - name: GITHUB_REPO_NAME
          value: 'IntegrationPlaybook-Samples-Azure-Infrastructure'  
        - name: GITHUB_REPO_URL
          value: '$(GITHUB_ACCOUNT_URL)/$(GITHUB_REPO_NAME)' 
        - name: GITHUB_PUSH_URL
          value: '$(GITHUB_PUSH_URL_BASE)/$(GITHUB_REPO_NAME)' 

      pool:
        vmImage: 'windows-2019'
        name: Azure Pipelines
      
    
      steps:
      #Display variables for troubleshooting
      - task: dutchworkz.DisplayAllVariables.DisplayAllVariables.DutchWorkzToolsAllVariables@1
        displayName: 'Show all build variables in build output.'

      #List out the file structure to help troubleshooting
      - task: PowerShell@2
        displayName: 'List files in the Working Directory'
        inputs:
            targetType: 'inline'
            script: 'Get-ChildItem -Path "$(System.ArtifactsDirectory)" -recurse'    
            workingDirectory: '$(System.DefaultWorkingDirectory)'    
      
      #Create a folder to download the Git Repo to
      - bash: |
            pwd
            mkdir Git   
        workingDirectory: '$(System.DefaultWorkingDirectory)'
        displayName: 'Create folder for Git'

      #Download the Git Repo to the git folder we created
      - bash: |
            pwd
            git config --global user.name $GITHUB_USER_DISPLAYNAME
            git config --global user.email $GITHUB_USER_EMAIL
            git init

            echo "Git Url:"
            echo "$GITHUB_REPO_URL"

            git remote add origin $GITHUB_REPO_URL
            git clone $GITHUB_REPO_URL

        workingDirectory: '$(System.DefaultWorkingDirectory)/Git'
        displayName: 'Clone Github Repo'

     #Clear the downloaded repo so we can copy back in just the files we wanrt
      - task: DeleteFiles@1
        displayName: 'Delete files from $(System.DefaultWorkingDirectory)/Git/$(GITHUB_REPO_NAME)'
        inputs:
            SourceFolder: '$(System.DefaultWorkingDirectory)/Git/$(GITHUB_REPO_NAME)'
            Contents: '**/*'


     #Push the changes back to Github
      - bash: |

           #Copy Infra-Main Solution
 from the working directory to the local repo.  Add any other copying here
           cp -rf ./Infra-Main ./Git/$GITHUB_REPO_NAME


        workingDirectory: '$(System.DefaultWorkingDirectory)'
        displayName: 'Copy files to the local repo'

      #List out the file structure of the git folder to help troubleshooting
      - task: PowerShell@2
        displayName: 'List files in Git Directory'
        inputs:
            targetType: 'inline'
            script: 'Get-ChildItem -Path "$(System.DefaultWorkingDirectory)/Git" -recurse'    
            workingDirectory: '$(System.DefaultWorkingDirectory)/Git' 

      #Push the changes back to Github
      - bash: |
           pwd           
           git add .
           git status
           git commit -m "Release: ${BUILD_BUILDNUMBER}"
           git push $GITHUB_PUSH_URL
        workingDirectory: '$(System.DefaultWorkingDirectory)/Git/$(GITHUB_REPO_NAME)'
        displayName: 'Github Commit and Push'

     

GitHub Settings

In Azure DevOps I created a library with the settings for github in, these are imported into the pipeline at runtime. Below you can see the variables used so you can set them up too.

Remember from the first article the url for pushing back to GitHub is in the below format:

https://[Github Username]:[Github PAT]@github.com/[GitHub Repo Owner]/[Github Repo Name]

This means that the GITHUB_PUSH_URL_BASE variable would be in the below format when you set it up with your own username and password:

https://[Github Username]:[Github PAT]@github.com/[GitHub Repo Owner]/

Once your settings are in place you can put a copy of this pipeline in any repo you want to create a pipeline to share some code from your Azure DevOps to your GitHub repo. You would add the pipeline yaml file, change the GITHUB_REPO_NAME variable in the yaml to your repo name and finally create a pipeline which points to the yaml file and you are then good to run on demand when you want to update your github repo with changes from Azure DevOps.

 

Buy Me A Coffee