Is there any way to auto restart after crashing in github workflows

Deleting original file NF - PAID MY DUES - youtube-LLAgke7QprM.f140.m4a (pass -k to keep)
/home/runner/work/_temp/9708cc07-bf79-4478-beda-081cae98f143.sh: line 3:  3270 Segmentation fault      (core dumped) python3 nani.py
Error: Process completed with exit code 139.

I’m trying to host my python bot on git workflows.
It will play music (in Telegram voice chat).
But it keeps crashing after some time.

Is there any way to automatically restart a workflow after any error?

Github repository to check the workflow implementation : https://github.com/Nksama/workflows

Answer

For what I know there is no way to restart a workflow automatically through configurations yet…

However, there are different ways to achieve a similar result using workflows implementation with Github Actions. It won’t be exactly a restart, as the workaround would be to start the workflow again if it fails.


The first option could be to use another workflow with the workflow_run trigger to check if the first workflow ran successfully, and if not, send a dispatch event to run it again (in that case, you would have to add a repository_dispatch trigger to the first workflow).


Another option could be to add 2 steps at the end of your first job (build) to save the job status as an artifact.

      - name: Create file status_job1.txt and write the job status into it
        if: always()
        run: echo ${{ job.status }} > status_job1.txt
      - name: Upload file status_job1.txt as an artifact
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: pass_status_job1
          path: status_job1.txt

Then, a second job would always check the output of the first one:

  job2:
    runs-on: ubuntu-latest
    needs: [job1]
    if: always()
    steps:
      - name: Download artifact pass_status_job1
        uses: actions/download-artifact@v1
        with:
          name: pass_status_job1
      - name: Set the status of Job 1, as output parameter
        id: set_outputs
        run: echo "::set-output name=status_job1::$(<pass_status_job1/status_job1.txt)"
      - name: Show the values of the output
        run: |
          # using the syntax steps.<step_id>.outputs.<output_name> to access the output parameter
          echo "status_job1 = ${{ steps.set_outputs.outputs.status_job1 }}"
          # other steps or commands to perform an operation according to its value.
          [...]

For example, at the end of job2, you could send a dispatch event to the same workflow if the status of the job1 is failure (in that case, you would have to add a repository_dispatch trigger to the workflow as well) to start the workflow again (as I explained, it won’t be exactly a restart as this will create a new workflow run).


Here is an action to create a repository dispatch event, or you can do it directly calling the Github API