Github action run python script
Run Python Script ActionWrite Python scripts in an Actions workflow file! Show This action lets you define a custom Python script inside the workflow YAML file. Write your Python code as the 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@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 with: script: | import os print("Directory contents:") for f in os.listdir(): print(f) Handling errorsBy default,
the action will fail if it encounters any errors when trying to run your script. You can override this with the The action sets three outputs:
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@v2 - uses: actions/setup-python@v2 - 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 }} Utility functionsThe action comes bundled with utilities that you can use to interact with the workflow. If you want to disable these utilities you can set 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() Add pathSignature: Prepend to the system path. The change will affect later steps only. Get inputSignature: Returns the value of the given input as a string. Set outputSignature: Sets an output parameter. Set envSignature: Sets an environment variable for use in later steps. DebugSignature: Sends a debug message. The message will be visible only when debug logging has been enabled. WarningSignature: Sends a warning message that will be highlighted with yellow color in the worklow log. ErrorSignature: Sends an error message that will be higlighted with red color in the workflow log. GroupSignature: Creates an expandable group in the workflow log. End groupSignature: Ends a group. All printed lines before calling Add maskSignature: Masks out sensitive data and replaces it with Stop commandsSignature: All commands will stop processing. It allows you to log anything without accidentally triggering workflow commands. Resume commandsSignature: Resume command processing. Get stateSignature: Share environment variables with your workflow's Save stateSignature: Saves a value as an environment variable with the Can I run Python script in GitHub actions?Run Python Script Action. 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.
How do I run Python code in GitHub?Run a Python script from GitHub (console). In Source Type, select GitHub.. In the Source Info box, enter the required information to access the source in the following format: ... . For Command Line, enter parameters for the script execution.. Can I run scripts in GitHub?You can use actions to run scripts and shell commands, which are then executed on the assigned runner.
How do I run an action Pytest in GitHub?To use a pre-installed version of Python or PyPy on a GitHub-hosted runner, use the setup-python action. This action finds a specific version of Python or PyPy from the tools cache on each runner and adds the necessary binaries to PATH , which persists for the rest of the job.
|