【问题标题】:sudo: no tty present and no askpass program specified in github actionssudo:没有 tty 存在,也没有在 github 操作中指定 askpass 程序
【发布时间】:2021-10-26 00:00:44
【问题描述】:

嘿,我有这个 github 操作来保存 package-lock 和 package.json 文件,如下所示:

# workflow that generates the package files for my webapp :)

name: generate-package-files

# Controls when the action will run.
on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      myWebappBranch:
        description: 'my webapp branch'
        required: true
        default: 'someBranch'
      packageRepos:
        description: 'repos built out for the job.'
        required: true
        default: aPackage

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  generate_files:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      #set up node js
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install build essentials
        run: sudo apt-get -y install build-essential

      #sets up SSH so we can checkout the needed repos.
      - name: Setup SSH
        uses: webfactory/ssh-agent@v0.5.3
        with:
          ssh-private-key: ${{ secrets.BITBUCKET_SSH_KEY }}

      - name: Check out the webapp for building and make sure it exists first.
        run: (test -d my-app) || git clone git@bitbucket.org:my-repo/my-webapp.git

      # Runs a set of commands using the runners shell
      - name: build package files
        working-directory: my-webapp
        run: |
          git checkout ${{ github.event.inputs.myWebappBranch }}
          git pull
          npm ci
          npm install ${{ github.event.inputs.packageRepos }}

      - name: save files
        uses: actions/upload-artifact@v2
        with:
          name: package-lock
          path: package*

看起来不错,不幸的是,当我运行它时,我得到:

Run sudo apt-get -y install build-essential
sudo: no tty present and no askpass program specified
Error: Process completed with exit code 1.

我不太确定我做错了什么。当我四处搜索时,安装的语法应该可以工作,从我所看到的:

https://code-maven.com/slides/github-ci/install-packages-on-ubuntu-linux-in-github-actions

https://docs.github.com/en/actions/using-github-hosted-runners/customizing-github-hosted-runners#installing-software-on-ubuntu-runners

似乎是正确的解决方案。我已尝试编辑 sudoers 文件,但访问被拒绝。任何帮助都是极好的! :)

【问题讨论】:

    标签: ubuntu github yaml github-actions apt-get


    【解决方案1】:

    为此,您需要允许使用没有密码的 sudo。

    sudo 打印此错误消息,如果它尝试提示输入密码但不能。

    【讨论】:

      【解决方案2】:

      所以我和我的团队一起讨论了这个问题,我们想通了。我们更新了 runner 机器上的 sudoers 文件。

      /etc/sudoers
      

      在这一行添加:

      admin ALL=(ALL) NOPASSWD: ALL
      

      一切正常。

      【讨论】:

      • 这基本上是关闭 sudo - 这不是一个大的安全漏洞吗?
      【解决方案3】:

      任何运行调用 Sudo 的程序的 GitHub 操作都会发生这种情况。这里和其他类似问题中的大多数其他答案基本上都会关闭 sudo,这显然很危险。此外,这仅适用于自托管的 GitHub 运行器,因为您无法修改常规 GitHub 虚拟机。

      一种方法是关闭您需要调用的特定命令的询问密码提示。我这样做了:

      sudo visudo
      

      注意:如果你弄乱了这个文件,你会破坏你的系统。 小心点,小心点;我们在打猎兔兔。

      然后为您的 github 操作中的特定命令添加如下所示的行:

      webmaster ALL = NOPASSWD: /exact/path/to/your/command and_then_any_params
      

      在这种情况下,我只是重新启动了一个网络服务,所以我也没有任何问题为本地终端上的任何人关闭它。

      visudo 方法有效,并且仍然适度安全。

      不起作用的事情:显然,您可以使用 -S 强制 Sudo 从标准输入读取,因此像管道 GitHub 密码这样简单的操作应该可以工作。这会很好,因为您不必修改每一个被破坏的服务器。

      所以我在我的 GitHub YAML 中尝试了这个:

      printf "${{ secrets.WEB_SU_PASSWORD }}\n" | sudo -S myscript myparams 
      

      这是它在 GitHub 操作日志中的样子:

      # printf "***\n" | sudo -S myscript myparams
      shell: /bin/bash -e {0}
      sudo: no tty present and no askpass program specified
      

      所以,同样的基本问题。为了时间,我只是在做上面列出的 visudo 方法。为了保证安全,我没有执行尚未被黑客入侵的系统管理员推荐的ALL 命令。

      更新:

      您的 visudo 文件中的实际行可能要复杂得多,并且可能需要一些摆弄。例如,我只需要运行一个命令并最终将其更改为:

      erp ALL = NOPASSWD: /usr/sbin/service
      erp ALL = NOPASSWD: /usr/sbin/service nginx *
      erp ALL = NOPASSWD: /usr/sbin/service supervisord *
      erp ALL = NOPASSWD: /bin/systemctl
      erp ALL = NOPASSWD: /bin/systemctl * nginx
      erp ALL = NOPASSWD: /bin/systemctl * supervisord
      erp ALL = NOPASSWD: /usr/bin/supervisorctl
      erp ALL = NOPASSWD: /usr/sbin/nginx
      

      该命令是一个运行上述某些命令的 python shell 脚本;仅添加脚本名称本身是不够的。

      【讨论】:

        最近更新 更多