Starting a Tmux session from Github Action doesn't persist once the action completes

376 Views Asked by At

I have a self-hosted Github Action with the main function of running a bash script to deploy a Django site. I cd into the directory, and then execute the bash script. This works fine, and I can see the print statements (DEPLOY_FOLDER AND CONTAINER_NAME) from the bash script in the Github Action GUI.

name: Deploy Workflow

on:
  workflow_dispatch:

jobs:
  deploy:
    runs-on: [self-hosted, emts]
    steps:
    - name: CD and then deploy
      shell: bash
      run: |
        cd /export/home/scottc/
        ./deployserver.sh test test_deploy

HOWEVER, within my deployserver.sh script, I am starting a (detached) TMUX new session. I receive a notice that my job completed successfully, but when I check tmux sessions (tmux ls) after the job completes, I don't see any running.

#!/bin/bash

echo $1
echo $2
DEPLOY_FOLDER=$1
CONTAINER_NAME=$2

tmux new -d -s $CONTAINER_NAME -- pipenv run python server_web/run_server.py

I have updated the deployserver.sh script to the below and found that a tmux session is actually being created, but must somehow get torn down when the job completes. I know this because after printing the DEPLOY_FOLDER and CONTAINER_NAME, I see tmux ls prints test_deploy: 1 windows (created Thu May 18 11:00:49 2023) [80x24] (while the job is still active but sleeping for 10 seconds). If I run tmux ls on the server during those 10 seconds, I do see an active window. However when the 10 seconds completes and the Github Action ends, the tmux session dissapears with it.

#!/bin/bash

echo $1
echo $2
DEPLOY_FOLDER=$1
CONTAINER_NAME=$2

tmux new -d -s $CONTAINER_NAME -- pipenv run python server_web/run_server.py

sleep 3

tmux ls

sleep 10

What do I need to change for the tmux session to persist after the github action completes?

2

There are 2 best solutions below

2
chepner On

By default, a shell session only lasts as long as it has an active window, a window only remains active as long as it has an active pane, and a pane only remains active until the command it is executing exits. When run_server.py exits, the initial pane is destroyed, taking the initial window with it and killing the session.

One way to keep it alive is to set the remain-on-exit option on the pane. (With no -t option, set-option affects the currently active pane. With only one server, one session, one window, and one pane, that happens to be the one running your command.)

tmux new -d ...
tmux set-option -p remain-on-exit
0
Scott C. On

Solution: Instead of running ./deployserver.sh test test_deploy use RUNNER_TRACKING_ID="" && (./deploysensei.sh test test_deploy)

The problem was evident in the "Complete Job" logs on Github:

Cleaning up orphan processes
Terminate orphan process: pid (1050875) (tmux: server)
Terminate orphan process: pid (1050876) (bash)
Terminate orphan process: pid (1051071) (pipenv)
Terminate orphan process: pid (1051075) (python)

I can see that the action was terminating orphan processes, including tmux. When you reassign the Github env variable RUNNER_TRACKING_ID, then during the teardown process it doesn't terminate the existing processes.

More details here.