Pipeline for Dependabot failing in Azure DevOps - "Bash exited with code '1' "

146 Views Asked by At

I wrote a pipeline for running dependabot in a .NET project in Azure DevOps on weekly schedule. This pipeline runs and creates PR for outdated packages version successfully, the pipeline get fails at some point. This is the error in log -

Running with options: {}
Fetching nuget dependency files for FaigleSolutions/Applications/_git/FsAbacusMfilesIntegration
Parsing dependencies information
  - Updating Microsoft.AspNetCore.Mvc.NewtonsoftJson (from 5.0.5)… submitted
  - Updating Microsoft.Extensions.Hosting.WindowsServices (from 5.0.1)… submitted
  - Updating Newtonsoft.Json (from 13.0.1)… submitted
  - Updating Swashbuckle.AspNetCore (from 5.6.3)… submitted
  - Updating Swashbuckle.AspNetCore.Annotations (from 6.1.3)… submitted
##[error]Bash exited with code '1'.
Finishing: Run Dependabot

Here is my pipeline for dependabot-

trigger: none # Disable CI trigger

schedules:
- cron: '0 2 * * 1' # daily at 2am UTC
  always: true # run even when there are no code changes
  branches:
    include:
      - development
  batch: true
  displayName: Weekly

pool:
  vmImage: 'ubuntu-latest' 

variables:
  - name: PACKAGE_MANAGER
    value: nuget
  - name: PROJECT_PATH
    value: FaigleSolutions/Applications/_git/FsAbacusMfilesIntegration  # path to the repo
  - name: DIRECTORY_PATH
    value: /src/Fs.AbacusMfilesInt # sub-path to the .sln file
  - name: AZURE_HOSTNAME
    value: dev.azure.com
    
steps:
  - script: git clone https://github.com/dependabot/dependabot-script.git
    displayName: Clone Dependabot config repo
    
  - script: |
      cd dependabot-script
      docker build -t "dependabot/dependabot-script" -f Dockerfile .
    displayName:  Build Dependabot Image

  - script: |
      docker run --rm -e AZURE_ACCESS_TOKEN='$(PAT)' -e PROJECT_PATH='$(PROJECT_PATH)' -e DIRECTORY_PATH='$(DIRECTORY_PATH)' -e  PACKAGE_MANAGER='$(PACKAGE_MANAGER)' dependabot/dependabot-script
    displayName: Run Dependabot

The error occurs in Run Dependabot step.

The error message is vague and I couldn't get it. Can anyone please help me to find the issue? Thanks in advance.

1

There are 1 best solutions below

0
On

The issue was related to API Rate Limit for some packages. So I added a delay with caching mechanism to retry the failed script and it worked.

retry_count=0
    while [ $retry_count -lt $(MAX_RETRY_ATTEMPTS) ]; do
        # Check if the cache exists and restore it
        if [ -d $(CACHE_KEY) ]; then
            echo "Restoring Dependabot cache..."
            cp -r $(CACHE_KEY)/* .
        fi

        # Run Dependabot script
        docker run --rm -e AZURE_ACCESS_TOKEN=$(dependabot-pat) -e PROJECT_PATH=$(PROJECT_PATH) -e DIRECTORY_PATH=$(DIRECTORY_PATH) -e PACKAGE_MANAGER=$(PACKAGE_MANAGER) dependabot/dependabot-script

        # Check the exit status of the script
        if [ $? -eq 0 ]; then
            # The script ran successfully, exit the retry loop
            break
        else
            # The script failed, increment the retry count and apply exponential backoff
            retry_count=$((retry_count+1))
            current_delay_seconds=$((BASE_DELAY_SECONDS * (2 ** (retry_count - 1))))
            current_delay_seconds=$(($current_delay_seconds > MAX_DELAY_SECONDS ? $MAX_DELAY_SECONDS : $current_delay_seconds))

            echo "Retry $retry_count: Dependabot script failed. Retrying in $current_delay_seconds seconds..."
            sleep $current_delay_seconds
        fi
    done

    if [ $retry_count -eq $(MAX_RETRY_ATTEMPTS) ]; then
        # Handle the case when maximum retries are reached, e.g., log an error
        echo "Maximum retry attempts reached. The package is still causing issues."
    else
        # Cache the Dependabot script directory for future use
        echo "Caching Dependabot script..."
        mkdir -p $(CACHE_KEY)
        cp -r ./* $(CACHE_KEY)
    fi