我正在使用 GitHub Actions 设置 CI/CD 管道,以将 Go 后端应用程序部署到远程 Ubuntu 服务器。该工作流程构建应用程序,使用 scp 传输二进制文件,并使用 sudo systemctl 重新启动服务器上的服务。
但是,当工作流程运行时,在 SSH 步骤中出现以下错误:
sudo: a terminal is required to read the password; either use the -S option to read
from standard input or configure an askpass helper
sudo: a password is required
Error: Process completed with exit code 1.
这是我工作流程的相关部分:
- name: Deploy to Server
env:
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
run: |
scp -P $DEPLOY_PORT ./app $DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH/app
ssh -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_HOST "sudo systemctl restart my-app.service"
我尝试过的: 无密码 sudo:
我将 DEPLOY_USER ALL=(ALL) NOPASSWD:ALL 添加到 /etc/sudoers 文件中,但它不起作用。 sudo 仍然提示输入密码。 回显密码:
尝试将密码添加到 GitHub Secrets 并通过 echo $SUDO_PASSWORD | 传递它sudo -S,但并没有解决问题。 替代部署方法:
将 sudo 命令封装在服务器上的 shell 脚本中,但仍然提示输入密码。 目标: 在使用 sudo 时,如何配置服务器或调整 GitHub Actions 工作流程以避免交互式终端或密码提示?任何有关安全解决此问题的建议将不胜感激!我可以在不将密码添加到“秘密”中的情况下完成吗
name: SSH Deploy
on: push:
branches:
- main # Replace with your branch name
jobs: deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
ssh -o StrictHostKeyChecking=no <username>@<IP> << 'EOF'
cd /www/wwwroot/65.1.85.176/
git pull origin feature/audio-module
EOF