Highlight

Need to have Git Integration like functionality for Azure Logic Apps? Here’s how :)

In this short article I wanted to share a script which allows you to implement git like functionality of pulling the code from Azure Logic App Standard into your DevOps repo!

Pipeline Code

name: LogicAppCI_$(Date:yyyyMMdd)$(Rev:.r)

trigger:
  branches:
    include:
    - main
    - releases/*
  paths:
    include:
    - pipelines/sso-cicd-pipeline.yml

schedules:
- cron: '0 0 * * *'
  displayName: Daily midnight build
  branches:
    include:
    - main
  always: true

pool:
  vmImage: ubuntu-latest

variables:
  - group: ladev
  - name: envName
    value: dev
  - name: connectionName
    value: "az"

resources:
  repositories:
  - repository: az-logic-apps-standard-cicd 
    type: git
    name: az-logic-apps-standard-cicd
    ref: ci/dev

stages:
  - stage: Build
    displayName: Build ${{ variables.envName }}
    jobs:
    - job: GetArfitacts
      steps:
      - checkout: az-logic-apps-standard-cicd
        persistCredentials: true
      - script: |
          echo $(Build.SourcesDirectory)
          ls $(Build.SourcesDirectory) *          
      - script: |
          git config user.email $(logicAppName)@marczak.io
          git config user.name $(logicAppName)
          git pull -f
          rm -rf $(Build.SourcesDirectory)/code
          mkdir $(Build.SourcesDirectory)/code          
        displayName: Remove old code
        workingDirectory: $(System.DefaultWorkingDirectory)
      - task: AzurePowerShell@5
        displayName: Download Logic App Code
        inputs:
          azureSubscription: ${{ variables.connectionName }}
          scriptType: filePath
          scriptPath: $(Build.SourcesDirectory)/pipelines/scripts/download-la-code.ps1
          scriptArguments:
            -SubscriptionId $(SubscriptionId) 
            -ResourceGroupName $(resourceGroupName) `
            -LogicAppName $(logicAppName) `
            -WorkingDirectory $(Build.SourcesDirectory)/code
          azurePowerShellVersion: latestVersion
          pwsh: true
      - script: |
          git diff
          git add -A
          git commit -m "CI $(Build.BuildNumber)"
          git push origin HEAD:ci/dev          
        displayName: Add changes to git
        workingDirectory: $(System.DefaultWorkingDirectory)
      - task: PublishPipelineArtifact@1
        inputs:
          targetPath: $(Build.SourcesDirectory)/code
          artifact: LogicAppCode

and add this script into your repo under /pipelines/scripts/download-la-code.ps1 path

param(
    [Parameter(Mandatory=$true)]
    [string]
    $SubscriptionId, 

    [Parameter(Mandatory=$true)]
    [string]
    $ResourceGroupName, 

    [Parameter(Mandatory=$false)]
    [string]
    $LogicAppName,

    [Parameter(Mandatory=$false)]
    [string]
    $WorkingDirectory
)

$files = "host.json", "connections.json", "parameters.json", ".funcignore"
Write-Output $files

$files | ForEach-Object {
    $hostFile = Invoke-AzRestMethod `
        -Uri "https://$LogicAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$_" `
        -ResourceId "https://management.azure.com/" 
    if ($hostFile.StatusCode -eq 200) {
        $hostFile.Content > "$WorkingDirectory/$_"
    } else {
        Write-Output "Download failed for $_"
        Write-Output $hostFile.Content
    }
}

$workflows = Invoke-AzRestMethod `
    -Path "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$LogicAppName/workflows?api-version=2018-11-01"
$workflowsObject = $workflows.Content | ConvertFrom-Json
$workflowsObject.value | ForEach-Object {
    $name = $_.name -replace '.*/'
    $hostFile = Invoke-AzRestMethod `
        -Uri "https://$LogicAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$name/workflow.json" `
        -ResourceId "https://management.azure.com/" 
    New-Item -Path "$WorkingDirectory/$name/workflow.json" -ItemType File -Force
    $hostFile.Content > "$WorkingDirectory/$name/workflow.json"
}

Adam Marczak

Programmer, architect, trainer, blogger, evangelist are just a few of my titles. What I really am, is a passionate technology enthusiast. I take great pleasure in learning new technologies and finding ways in which this can aid people every day. My latest passion is running an Azure 4 Everyone YouTube channel, where I show that Azure really is for everyone!

Did you enjoy the article?

Share it!

More tagged posts