azuredebuggingtimeoutcicdazure-bicep

Intermittent Timeout Errors in Bicep Analyse Step of CI Pipeline


I have a question regarding Bicep deployments. Specifically, I am using ps-rule-assert to check my templates before deployment. However, I am encountering an intermittent issue where the analyse step of the CI pipeline fails with a timeout error. Confusingly, sometimes the run succeeds without the timeout issue.

My templates are quite small, so I do not understand why I am getting a timeout error in the first place. Here are my questions:

  1. Why does the behaviour vary (sometimes it completes, sometimes it times out)?
  2. Is there a way to increase the timeout window? Perhaps it is set too small.
  3. I am building the Bicep files before analysing them. I immediately copy the build files to a staging directory. Is this the correct approach?

Note: The build and deployment works without the analyse step. There are no errors if I run the build and deployment by itself without the assert task.

        steps:
          - task: PowerShell@2
            displayName: "Build Bicep Artifact"
            inputs:
              targetType: "inline"
              filePath: "$(ArtifactsPath)"
              script: |
                az bicep build --file **/apps/bicep_templates/savedsearches.bicep --outdir $(Build.ArtifactStagingDirectory);
                az bicep build --file **/apps/bicep_templates/workbook.bicep --outdir $(Build.ArtifactStagingDirectory)

            # "inputType repository" mandatory for Azure Devops Deployments
          - task: ps-rule-assert@2
            displayName: Analyze Azure template files
            inputs:
              inputType: repository
              # The path PSRule will look for files to validate.
              inputPath: "workbooks/apps/bicep_templates/"
              # The working directory PSRule is run from  
              path: "workbooks/apps/"
              modules: "PSRule.Rules.Azure"
              baseline: "Azure.GA_2023_09"
              summary: true

Error message:

 ##[error]Bicep (0.30.23) compilation of '/__w/1/s/workbooks/apps/bicep_templates/workbook.bicep' failed with: Bicep compilation hasn't completed within the timeout window. This can be caused by errors or warnings. Check the Bicep output by running bicep build and addressing any issues.
##[error]Bicep (0.30.23) compilation of '/__w/1/s/workbooks/apps/bicep_templates/savedsearches.bicep' failed with: Bicep compilation hasn't completed within the timeout window. This can be caused by errors or warnings. Check the Bicep output by running bicep build and addressing any issues.

Update: The build task takes 25s, the analyse task times out at 15s. Here are the versions I am using:

  PSRule: '2.9.0'
  PSRule.Rules.Azure: '1.30.1'
  Bicep: 0.30.23

Above, I have included the CI code and the error message I am receiving. Help is much appreciated.


Solution

  • Intermittent Timeout Errors in Bicep Analyse Step of CI Pipeline

    Thanks @Thomas for your valuble inputs and i do agree with it because you're always tries to focus on bicep configuration. Which may not be the cause every time.

    As per the documentation shared in comments you also need to analyze the generated arm template. To do this, you can modify your CI pipeline to first convert the Bicep files to ARM templates and then analyze the ARM templates using PSRule.

    In the first place convert the bicep to arm template

    az bicep build --file path/to/your/bicepfile.bicep --outdir path/to/output
    

    Now use PSRule analyze the generated ARM template

    - task: ps-rule-assert@2
      displayName: Analyze ARM template files
      inputs:
        inputType: repository
        inputPath: "path/to/output/"
        path: "path/to/output/"
        modules: "PSRule.Rules.Azure"
        baseline: "Azure.GA_2023_09"
        summary: true
    

    By analyzing the ARM templates directly, you can avoid the intermittent timeout issues related to Bicep compilation and streamline your CI pipeline.

    And also, as mentioned in the query you can also try increase the time duration settings in the PSRule configuration file

    i.e., you can set the AZURE_BICEP_FILE_EXPANSION_TIMEOUT option to a higher value, such as 30 or 60 second which should be added as part of configuration.

    Refer:

    https://azure.github.io/PSRule.Rules.Azure/using-bicep/

    https://azure.github.io/PSRule.Rules.Azure/troubleshooting/

    https://github.com/Azure/PSRule.Rules.Azure/blob/main/docs/using-bicep.md