sudo:不存在 tty,并且在 github 操作中没有指定 askpass 程序

sudo: no tty present and no askpass program specified in github actions

嘿,所以我有这个 github 操作来保存包锁和 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.

我不太确定我做错了什么。当我 google 左右时,安装的语法应该有效,从我在以下内容中看到的:

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 文件,但访问被拒绝。任何帮助都是极好的! :)

为此,您需要允许不带密码的 sudo。

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

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

/etc/sudoers

在这一行添加:

admin ALL=(ALL) NOPASSWD: ALL

一切正常。

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

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

sudo visudo

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

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

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

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

visudo 方法行之有效,并且仍然比较安全。

无效的地方:显然,您可以使用 -S 强制 Sudo 从标准输入读取,因此一些简单的操作,例如将 GitHub 秘密传送到 sudo 命令应该有效。这会很好,因为您不必修改每一个该死的服务器。

所以我在我的 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 脚本,运行 上面的一些命令;仅添加脚本名称本身是不够的。