Write Python scripts in an Actions workflow file!
This action lets you define a custom Python script inside the workflow YAML file. Write your Python code as the script
argument, and use the YAML multiline string feature to define multiline scripts.
The only requirement is that you set up the Python environment before running the action. Here is an example workflow that prints the repository root directory contents to the Actions logs:
name: Run Script
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- uses: jannekem/run-python-script-action@v1
with:
script: |
import os
print("Directory contents:")
for f in os.listdir():
print(f)
By default, the action will fail if it encounters any errors when trying to run your script. You can override this with the fail-on-error
input.
The action sets three outputs:
stdout
contains any text that your program prints to the consolestderr
contains any text that is routed to STDERR, such as exception messageserror
is a string with either"true"
or"false"
depending on if errors were present or not, use this to check for errors when you opt not to fail the step
Look at the following snippet to see how the error handling works in practice:
name: Run Script
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- uses: jannekem/run-python-script-action@v1
id: script
with:
fail-on-error: false
script: |
print("Doing something that will fail")
a = []
a[10]
- name: Print errors
if: steps.script.outputs.error == 'true'
run: |
printenv "SCRIPT_STDOUT"
printenv "SCRIPT_STDERR"
env:
SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }}
SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}
The action comes bundled with utilities that you can use to interact with the workflow. If you want to disable these utilities you can set util: false
as an input for the step. You can call these functions directly from your script
without having to import anything.
Example:
- uses: jannekem/run-python-script-action@v1
with:
script: |
add_path("/usr/local/test")
set_env("HELLO", "WORLD")
group("Messages")
print("Sending a message")
warning("There might be an issue")
end_group()
Signature: add_path(path)
Prepend to the system path. The change will affect later steps only.
Signature: get_input(name)
Returns the value of the given input as a string.
Signature: set_output(name, value)
Sets an output parameter.
Signature: set_env(name, value)
Sets an environment variable for use in later steps.
Signature: def set_summary(value)
Sets an environment variable to display as job summary.
Signature: debug(message)
Sends a debug message. The message will be visible only when debug logging has been enabled.
Signature: warning(message)
Sends a warning message that will be highlighted with yellow color in the worklow log.
Signature: error(message)
Sends an error message that will be higlighted with red color in the workflow log.
Signature: group(title)
Creates an expandable group in the workflow log.
Signature: end_group()
Ends a group. All printed lines before calling end_group()
belong to the previously defined group.
Signature: add_mask(value)
Masks out sensitive data and replaces it with ***
. The value can be a string ("my sensitive data"), or it can point to an environment variable ("$DB_PASSWORD").
Signature: stop_commands()
All commands will stop processing. It allows you to log anything without accidentally triggering workflow commands.
Signature: resume_commands()
Resume command processing.
Signature: get_state(name)
Share environment variables with your workflow's pre:
and post:
actions. See official documentation for more details.
Signature: save_state(name, value)
Saves a value as an environment variable with the STATE_
prefix. See official documentation for more details.