Extract local.settings.json file from zip file using YAML

Sanjana Pushkar 0 Reputation points
2024-08-03T19:55:09.71+00:00

I have a local.settings.json file inside a .zip folder, from which i want to extract the environment variables and add them while deploying the function app in a YAML pipeline. Different functions will have different zip files, so the local.settings.json file is dynamic. I want to use this in the below task inside a YAML pipeline to set the app settings using the JSON file.

AzureFunctionApp@2

I have tried extracting the JSON file, and adding to the task using the below code, but it doesn't seem to work. Please help me out on this.

- script: |
            echo "Extracting zip file"
            unzip $(System.DefaultWorkingDirectory)/EXCO.AzureFunctions_01Aug.zip -d $(Pipeline.Workspace)/extracted
          displayName: 'Extract Zip File'

        - script: |
            cat $(Pipeline.Workspace)/extracted/**/local.settings.json
          displayName: 'Print local.settings.json content'  

        - script: |
            CONFIG_FILE=$(Pipeline.Workspace)/extracted/**/local.settings.json
            ENV_VARS=$(jq -r 'to_entries | map("\(.key)=\(.value | @sh)") | .[]' $CONFIG_FILE)
            echo $ENV_VARS
            echo $ENV_VARS > env_vars.txt
          displayName: 'Parse local.settings.json'
 
    - job: DeployFunctionApp
      dependsOn: DownloadBuild
      steps:     
      - task: AzureFunctionApp@2
        inputs:
          connectedServiceNameARM: 'DevServiceConnector'
          appType: 'functionApp'
          appName: 'sanjanatestfademo'
          package: '$(System.DefaultWorkingDirectory)/**/*.zip'
          appSettings: '-settings @$(Pipeline.Workspace)/extracted/**/local.settings.json'
          deploymentMethod: 'auto'
        displayName: 'Set Environment Variables in Function App'
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,073 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Konstantinos Passadis 19,166 Reputation points MVP
    2024-08-04T15:05:52.7766667+00:00

    Hello @Sanjana Pushkar

    Welcome to Microsoft QnA!

    Try this :

    • script: | echo "Extracting zip file" unzip $(System.DefaultWorkingDirectory)/EXCO.AzureFunctions_01Aug.zip -d $(Pipeline.Workspace)/extracted displayName: 'Extract Zip File'
    • script: | echo "Printing local.settings.json content" cat $(Pipeline.Workspace)/extracted/**/local.settings.json displayName: 'Print local.settings.json content'
    • script: | echo "Parsing local.settings.json" CONFIG_FILE=$(find $(Pipeline.Workspace)/extracted/ -name "local.settings.json") ENV_VARS=$(jq -r '.Values | to_entries | map("(.key)=(.value)") | .[]' $CONFIG_FILE) echo "$ENV_VARS" > $(Pipeline.Workspace)/env_vars.txt displayName: 'Parse local.settings.json'
    • script: | echo "Setting environment variables" while IFS= read -r line; do
      echo "##vso[task.setvariable variable=${line%=*}]${line#*=}"
      
      done < $(Pipeline.Workspace)/env_vars.txt displayName: 'Set Environment Variables'
    • job: DeployFunctionApp dependsOn: DownloadBuild steps:
      • task: AzureFunctionApp@2 inputs: connectedServiceNameARM: 'DevServiceConnector' appType: 'functionApp' appName: 'sanjanatestfademo' package: '$(System.DefaultWorkingDirectory)/**/*.zip' appSettings: '$(Pipeline.Workspace)/env_vars.txt' deploymentMethod: 'auto' displayName: 'Deploy Function App with Environment Variables'

    The script loops through each environment variable, sanitizing the value (e.g., adding quotes when necessary) and uses the ##vso[task.setvariable] command to set the variable securely within the pipeline environment.

    Let me know how it went !

    --

    I hope this helps!

    The answer or portions of it may have been assisted by AI Source: ChatGPT Subscription

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.