Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Github Action as sudo

I am using a fastlane Github action. I need to run it as the root user.

      - name: Deploy
        uses: maierj/[email protected]
        with:
          lane: 'alpha'
          subdirectory: 'android-staging'

How can I run this as sudo?

like image 862
user82395214 Avatar asked Jan 18 '26 08:01

user82395214


1 Answers

As @itsjack mentioned, You either have to use SUDO_ASKPASS with a script that writes your sudoer user's password to the stream or use the actual echo command with sudo -S to make sudo read it from the pipeline.
I think it's much safer to store the password as a GitHub secret and echo it to the pipeline before using sudo -S; like this:

jobs:
  Backup:
  runs-on: self-hosted
  steps:
    - name: Backup
      working-directory: ${{ secrets.PRODUCTIONWEBROOT }}
      run: echo ${{ secrets.USERPWD }} | sudo -S tar czf ./tmp/${{ format('Backup_Before_{0}.tar.gz', github.run_id) }} ./wwwroot

You can even use sudo as a user like www-data if you want:

jobs:
  Backup:
  runs-on: self-hosted
  steps:
    - name: Backup
      working-directory: ${{ secrets.PRODUCTIONWEBROOT }}
      run: echo ${{ secrets.USERPWD }} | sudo -S -u www-data tar czf ./tmp/${{ format('Backup_Before_{0}.tar.gz', github.run_id) }} ./wwwroot

REMEMBER: the USERPWD secret must be the password to the user who runs Github's self-hosted service. I mean the user you used to install the runner or the specific user you used to install the runner service using .svc.sh install USERNAME

like image 60
Achilles Avatar answered Jan 20 '26 23:01

Achilles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!