Cancelling a workflow on github - github

I am running into an snag with cancelling workflows on github. As per github documentation cancelling a workflow should send ctrl+c to the workflow process but I am not able to capture ctrl+c in the workflow.
workflow:
name: test
on:
workflow_dispatch:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: make
run: python ./sleep.py
sleep.py
#!/usr/bin/python
import time
import signal
import sys
def wait():
try:
print("Going to sleep now.", file=sys.stderr)
time.sleep(30)
print("Woke up.", file=sys.stderr)
except:
print("Caught an exception.", file=sys.stderr)
def signal_handler(sig, frame):
print('Ctrl+C!')
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
wait()
When workflow is cancelled, the script sleep.py never catches SIGINT.
Tried workflow with if !cancelled with same outcome:
- name: make
if: ${{!cancelled()}}
run: python ./sleep.py
Thank you.

Related

Run github actions if branch has updated files from a specific directory

File structure:
apps
-- app-1
-- app-2
libs
-- lib-1
-- lib-2
We have tests that should run only in case if files were changed in lib-2.
I have tried to do
on:
push:
paths:
- 'libs/lib-2/**'
But it runs tests only when files from lib-2 were pushed in a commit but not running if some others were pushed after that.
Imagine tests are failed for lib-2, then developer have submitted files from lib-1 in the next commit and tests just wouldn't run for previous changes and github will consider checks as a success.
Is there a way to run actions if files from a certain directory were changed in a branch no matter in what commit?
I have designed a solution thanks to #guifalourd.
name: 'UI-kit Tests'
on:
pull_request:
branches:
- proto
- develop
- staging
- master
jobs:
filter-ui-kit:
runs-on: ubuntu-latest
name: Filter Ui kit
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 0
- name: Get changed files in the docs folder
id: changed-files-specific
uses: tj-actions/changed-files#v34
with:
files: libs/ui-kit/**
- name: Run step if any file(s) in the docs folder change
if: steps.changed-files-specific.outputs.any_changed == 'true'
run: echo UI-kit is affected
- name: Prevent from running
if: steps.changed-files-specific.outputs.any_changed != 'true'
run: exit 1
test:
timeout-minutes: 60
runs-on: ubuntu-latest
needs: [filter-ui-kit]
steps:
... test actions goes there

Invalid workflow file Github Actions (CF CLI)

I'm trying to get this github action to work but once committed it gives me this error:
Invalid workflow file: .github/workflows/main.yml#L1
No steps defined in steps and no workflow called in uses for the following jobs: build
Anyone have any idea what this might depend on?
Below is the code I used:
name: Deploy to Cloud Foundry
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-18.04
# Build your app here
deploy:
runs-on: ubuntu-18.04
needs: build
steps:
- uses: citizen-of-planet-earth/cf-cli-action#master
with:
cf_api: https://api.my-cloud-foundry.com
cf_username: ${{ secrets.CF_USER }}
cf_password: ${{ secrets.CF_PASSWORD }}
cf_org: AwesomeApp
cf_space: Development
command: push -f manifest-dev.yml
Thanks in advance to everyone
Following the Workflow Syntax for Github Actions, you'll identify that some fields are mandatory.
At the workflow level, you need to have at least a trigger (configure with the on field) and a list of jobs specified.
Then, in that list of jobs, you have to specify at least one job, where each of those jobs needs at least the runner and the steps (or the uses for reusable workflow) field configured.
Example of the minimum configurations you would use for a job:
on: push
jobs:
job1:
runs-on: ubuntu-latest
steps:
- name: Print a greeting
run: echo 'Hello World'
job2: # reusable workflow scenario
uses: owner/repo/.github/workflows/reusable-workflow.yml#main

How to run a script at the end of a job, even if the job was cancelled?

This is the workflow I'm currently using on GitHub
name: Windows10 - CI
on: [ push ]
jobs:
run-test:
runs-on: [ self-hosted, windows, DO ]
steps:
- uses: actions/checkout#v2
with:
clean: false
- name: Run nds2 CI - Sanity Test
if: github.ref == 'refs/heads/master'
run: cd c:\actions-runner\_work\nds2\nds2 ; python3 ci_host.py --master
- name: Run nds2 CI - Build Installer
if: github.ref != 'refs/heads/master'
run: cd c:\actions-runner\_work\nds2\nds2 ; python3 ci_host.py
I have a windows 10 computer which listens for an incoming job by using the GitHub runner.
Upon an incoming job, if a push is being made to the master branch the script ci_host.py is being run with the '--master' flag which spins up a VM and runs multiple tests on it. Eventually, at the end of the tests, the script restores the VM to a pre-configured snap shot.
So basically what I'm trying to achieve is, when the job is being canceled through the GitHub actions web interface then the script which handles the test is being canceled mid-job and doesn't have the chance to restore the VM to its prior clean state (snapshot).
How can I run a script which will be run at the end of the workflow even if the job was canceled?
So no matter what happens my VM could be restored to its clean state
Thanks in advance for your help :)
You can use Job status check functions to execute a step depending on what happened on the job before it (or not):
success: Returns true when none of the previous steps have failed or been canceled.
always: Always returns true, even when canceled. A job or step will not run when a critical failure prevents the task from running. For example, if getting sources failed.
cancelled: Returns true if the workflow was canceled.
failure: Returns true when any previous step of a job fails.
Example of use:
steps:
...
- name: Execute if the job succeeded
if: ${{ success() }}
- name: Execute if the job failed
if: ${{ failure() }}
- name: Execute if the job was cancelled
if: ${{ cancelled() }}
- name: Always execute
if: ${{ always() }}

Github Actions: using workflow_run based on new tags

I have two workflows: CI (for continuous integration) and CD (for continuous delivery). Both are working fine individually. My goal is to run the CD workflow only when:
A new tag like v1.1.1 is created on the master branch
The CI workflow is finished
To achieve my goal I'm using the workflow_run event. These are the snippets of my workflows files:
ci.yml:
name: CI
on:
push:
tags: v[1-9]+.[0-9]+.[0-9]+
pull_request:
branches: [develop, hotfix*]
cd.yml
name: CD
on:
workflow_run:
workflows: [CI]
branches: [master]
types:
- completed
The current behavior is: when a tag is created in the master branch only the CI workflow run. I've tried putting tags: v[1-9]+.[0-9]+.[0-9]+ in the workflow_run but the behavior is the same.
My question is: how can I achieve my goal? Is it possible?
According to docs you can only use branches option and not tags for workflow_run so I'm afraid that's why your current setting doesn't work.
You have some alternatives though:
You can turn your CD workflow into action and run it as part of your CI with condition:
.github/actiond/cd/action.yml:
name: CD
description: Run CD
runs:
using: composite
steps:
- run: echo "Success!"
shell: bash
CI:
name: CI
on:
push:
tags: v[1-9]+.[0-9]+.[0-9]+
pull_request:
branches: [develop, hotfix*]
jobs:
sucess:
name: Log success
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: echo "Success!"
- name: Run CD
if: github.event_name == 'push' && contains(github.event.ref, '/tags/')
uses: ./.github/actions/cd
Have it as a separate job that is dependant on CI job using needs option
Converting it to action makes for better encapsulation IMO although requires some work.
You need to put "" around the name of the triggering workflow in cd.yml:
name: CD
on:
workflow_run:
workflows: ["CI"]

Can we implement Github workflow which runs always?

Is there straight way how to make GitHub action run on any event
e.g.
name: always
on: *
jobs:
run:
runs-on: ubuntu-latest
steps:
- run: "echo \"this job runs on any event...\""
Want to send from custom action notification on any event occurring in the repo!