appleboy/ssh-action

The startup script could not be executed properly

Closed this issue · 2 comments

Describe the bug

name: Go

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # 检出代码
      - uses: actions/checkout@v4

      # 设置 Go 环境
      - name: 设置 Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.23.3'

      # 构建项目
      - name: 构建项目
        run: |
          DEPLOY_TIME=$(date +"%Y%m%d_%H%M%S")
          CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o webshop_${DEPLOY_TIME} -ldflags="-s -w" .

      # 部署构建文件到服务器
      - name: 上传文件
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          source: webshop_*
          target: /www/wwwroot/webshop/server/
          overwrite: true

      # 启动新版本服务
      - name: 平滑切换并启动新版本
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          script: |
            cd /www/wwwroot/webshop/server
            # 停止旧版本服务
            sh stop.sh || true
            # 启动新版本
            nohup sh start.sh > /dev/null 2>&1 &

Here's my latest yml config and here's the full info with debug:true enabled

with:
    host: ***
    username: ***
    password: ***
    debug: true
    script: cd /www/www***/webshop/server
  # 停止旧版本服务
  sh stop.sh || true
  # 启动新版本
  nohup sh start.sh > /dev/null 2>&1 &
  
    port: 22
    protocol: tcp
    timeout: [3](https://github.com/cccqcccq/webshop/actions/runs/12174387449/job/33956294455#step:7:3)0s
    command_timeout: 10m
    proxy_port: 22
    proxy_protocol: tcp
    proxy_timeout: 30s
    capture_stdout: false
Run echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
  echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    GITHUB_ACTION_PATH: /home/runner/work/_actions/appleboy/ssh-action/master
Run entrypoint.sh
  entrypoint.sh
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    GITHUB_ACTION_PATH: /home/runner/work/_actions/appleboy/ssh-action/master
    INPUT_HOST: ***
    INPUT_PORT: 22
    INPUT_PROTOCOL: tcp
    INPUT_USERNAME: ***
    INPUT_PASSWORD: ***
    INPUT_PASSPHRASE: 
    INPUT_KEY: 
    INPUT_KEY_PATH: 
    INPUT_FINGERPRINT: 
    INPUT_PROXY_HOST: 
    INPUT_PROXY_PORT: 22
    INPUT_PROXY_USERNAME: 
    INPUT_PROXY_PASSWORD: 
    INPUT_PROXY_PASSPHRASE: 
    INPUT_PROXY_KEY: 
    INPUT_PROXY_KEY_PATH: 
    INPUT_PROXY_FINGERPRINT: 
    INPUT_TIMEOUT: 30s
    INPUT_PROXY_TIMEOUT: 30s
    INPUT_COMMAND_TIMEOUT: 10m
    INPUT_SCRIPT: cd /www/www***/webshop/server
  # 停止旧版本服务
  sh stop.sh || true
  # 启动新版本
  nohup sh start.sh > /dev/null 2>&1 &
  
    INPUT_SCRIPT_FILE: 
    INPUT_ENVS: 
    INPUT_ENVS_FORMAT: 
    INPUT_DEBUG: true
    INPUT_ALL_ENVS: 
    INPUT_REQUEST_PTY: 
    INPUT_USE_INSECURE_CIPHER: 
    INPUT_CIPHER: 
    INPUT_PROXY_USE_INSECURE_CIPHER: 
    INPUT_PROXY_CIPHER: 
    INPUT_SYNC: 
    INPUT_CAPTURE_STDOUT: false
Will download drone-ssh-1.8.0-linux-amd6[4](https://github.com/cccqcccq/webshop/actions/runs/12174387449/job/33956294455#step:7:4) from https://github.com/appleboy/drone-ssh/releases/download/v1.8.0
======= CLI Version =======
Drone SSH version 1.8.0
===========================
======CMD======
cd /www/www***/webshop/server
# 停止旧版本服务
sh stop.sh || true
# 启动新版本
nohup sh start.sh > /dev/null 2>&1 &
======END======
2024/12/0[5](https://github.com/cccqcccq/webshop/actions/runs/12174387449/job/33956294455#step:7:5) 06:26:04 Process exited with status 143 from signal TERM
Error: Process completed with exit code 1.

Here are two shells I wrote

start.sh

# 设置程序路径和日志路径
PROGRAM_DIR="/www/wwwroot/webshop/server/"
LOG_FILE="/www/wwwroot/webshop/server/logs/webshop.log"

# 获取最新的程序文件
get_latest_program() {
    # 查找匹配的文件并获取最新的文件
    first_file=$(ls -t "$PROGRAM_DIR"webshop_* 2>/dev/null | head -n 1)
    second_file=$(ls -t "$PROGRAM_DIR"webshop_* 2>/dev/null | head -n 2 | tail -n 1)
    
    if [[ -z "$first_file" ]]; then
        echo "$(date +'%Y-%m-%d %H:%M:%S') - 未找到符合规则的文件。" >> "$LOG_FILE"
        exit 1
    fi

    # 如果第二个文件存在,删除它
    if [[ -n "$second_file" ]] && [[ "$first_file" != "$second_file" ]]; then
        rm -f "$second_file"
        echo "$(date +'%Y-%m-%d %H:%M:%S') - 删除了第二个文件: $second_file" >> "$LOG_FILE"
    fi
    
    echo "$first_file"
}

# 启动程序函数
start_program() {
    local program="$1"
    echo "$(date +'%Y-%m-%d %H:%M:%S') - 启动程序: $program ..." >> "$LOG_FILE"
    nohup "$program" >> "$LOG_FILE" 2>&1 &
}

# 检查并启动程序函数
check_and_start() {
    local program="$1"
    if ! pgrep -f "$program" > /dev/null; then
        echo "$(date +'%Y-%m-%d %H:%M:%S') - 程序未运行,启动中: $program ..." >> "$LOG_FILE"
        start_program "$program"
    else
        echo "$(date +'%Y-%m-%d %H:%M:%S') - 程序正在运行: $program ..." >> "$LOG_FILE"
    fi
}

# 主逻辑
first_file=$(get_latest_program) # 获取最新文件
# 确保程序可执行
chmod +x "$first_file"
start_program "$first_file"      # 初次启动程序

while true; do
    sleep 120
    check_and_start "$first_file"
done

stop.sh

pkill -f "start.sh"
pkill -f "/www/wwwroot/webshop/server/webshop_*"

I don't know why the error occurs, the program uploads to the server normally and stops, but it fails to start
Here is my previous YML configuration, and the full execution was successful

name: Go

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 检出代码
      - uses: actions/checkout@v4

      # 设置 Go 环境
      - name: 设置 Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.23.3'

      # 构建项目,启用静态编译以确保在 CentOS 上运行
      - name: 构建
        run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o webshop -ldflags="-s -w" .

      # 停止守护程序和主程序
      - name: 停止程序
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          script: |
            sh /www/wwwroot/webshop/server/stop.sh || true

      # 上传文件到服务器,
      - name: 上传构建文件到服务器
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          source: webshop
          overwrite: true
          target: /www/wwwroot/webshop/server

      # 远程运行脚本,启动守护程序,必须进入到项目目录下否则.env文件找不到
      - name: 启动守护程序
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          script: |
            cd /www/wwwroot/webshop/server
            nohup sh start.sh > /dev/null 2>&1 &

When I split the stop and start into two actions, it was normal

name: Go

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 检出代码
      - uses: actions/checkout@v4

      # 设置 Go 环境
      - name: 设置 Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.23.3'

      # 构建项目
      - name: 构建项目
        run: |
          DEPLOY_TIME=$(date +"%Y%m%d_%H%M%S")
          CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o webshop_${DEPLOY_TIME} -ldflags="-s -w" .

      # 上传文件到服务器
      - name: 上传文件
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          source: webshop_*
          target: /www/wwwroot/webshop/server/
          overwrite: true

  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      # 停止服务
      - name: 停止服务
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          debug: true
          script: |
            sh /www/wwwroot/webshop/server/stop.sh || true

      # 停止服务
      - name: 启动服务
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          debug: true
          script: |
            cd /www/wwwroot/webshop/server
            nohup sh start.sh > /dev/null 2>&1 &

This is not the ssh-action issue.