Search code examples
gitnext.jsazure-devopsyamlcicd

##[warning]'git config --get remote.origin.url' failed with exit code: 1, output: '' - Azure Devops CI/CD Pipeline Permission Issue


I get these errors whenever i tried running my pipelines on my created agent ,lets say agent_test.

When I run pipelines for first time it works and if retried to run after that, same issues occur.

##[warning]'git config --get remote.origin.url' failed with exit code: 1, output: ''
##[error]One or several exceptions have been occurred.
##[error]System.UnauthorizedAccessException: Access to the path '/home/ubuntu/projectfile/myagent/_work/1/s/.next/cache/images/Nn1+Lvq9fpQugwxAHfYtWM8Fp0OWmIqb38i9HtcZ67Q=/60.1698820786123.R-Lptnv+qGQlQiip-n7JqnPeS4MDNx4NR4C9HBtCcNk=.webp' is denied.
---> System.IO.IOException: Permission denied
 --- End of inner exception stack trace ---

Before Build script, it gives error and stop running, the stop process name is "Checkout project-file@development to s"

My YML Script is :

trigger:
- development

pool:
  name: 'projectfile'
  
stages:      
- stage: SetPermissions
  jobs:
  - job: SetPermissions
    displayName: 'Set Permissions'
    steps:
    - script: |
        echo "Cleaning the repository..."
        git clean -ff  # Clean the repository to avoid permission issues
        git reset --hard HEAD
        echo "Setting permissions for the working directory..."
        chown -R ubuntu:ubuntu $(System.DefaultWorkingDirectory)
        chmod -R u+rw $(System.DefaultWorkingDirectory)
        
      displayName: 'Set Permissions'
      continueOnError: true

- stage: BuildAndDeploy
  jobs:
  - job: Build
    displayName: 'Build'
    steps:
    - script: |
        echo "Current working directory is: $(System.DefaultWorkingDirectory)"
        echo "Install npm"
        npm install
        pwd       
        echo "Building the project..."
        npm run build
        echo "Installing pm2..."
        npm install pm2
      displayName: 'Npm install and Build'
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(System.DefaultWorkingDirectory)'
        artifact: 'userside_dev_artifact'
      displayName: 'Publish Build Artifacts'
    - script: |
        echo "Deploying the project..."
        pwd

        # Check if PM2 connection is already established
        if ! pm2 list | grep -q "projectfile"; then
          echo "PM2 process not found. Running 'npm run pm2'..."
          npm run pm2
        else
          echo "PM2 process is already running. Skipping 'npm run pm2'."
        fi

        pm2 restart all
        echo "Deployment successful!"
      displayName: 'Deploy'
      workingDirectory: $(System.DefaultWorkingDirectory)

Weird things is, when i use chown -R ubuntu:ubuntu _work and chmod -R u+rw _work then pipeline run successfully and again if pipeline tried to run for second times, then it gives same above error. I tried different way, but still this issue has not been resolve. Provide me the solution to fix.

Note that : The above script is for user side which is build in next and same issue arise in admin panel build on react, but the issue doesnt arise in backend which is build in nodejs.


Solution

  • The issue has been fixed. By default azure dont allow root access neither sudo command.so what i did is :

    In config.sh, ADD THIS COMMAND

    export AGENT_ALLOW_RUNASROOT="1"
    

    Similarly, In run.sh, add same command using nano editor or any editor:

    export AGENT_ALLOW_RUNASROOT="1"
    

    and then we can do,

    sudo ./config.sh
    

    So, by this way we can provide root access to all the directory in azure devops

    Article I have wrote, how i fixed the issue: https://medium.com/@sandipdulal/system-unauthorizedaccessexception-azure-devops-ci-cd-pipeline-permission-issue-fix-033bfaf4bb8c