How to Run Python Script in GitHub Actions ?
Last Updated :
26 Apr, 2024
A tool available on GitHub that can help you automate chores in your software projects is called GitHub Actions. It enables you to design workflows that, when executed automatically, carry out actions like as deploying, testing, and even sending out notifications. It basically works like a small robot assistant that handles your monotonous tasks, saving you time and energy when managing your projects. This is a detailed guide on how to use GitHub actions to run Python scripts.
- Flexibility: GitHub Actions allows for adjustable workflow definition. Workflows can be adjusted to match the particular needs of your project by interacting with various resources and services.
- Integration: It interfaces with GitHub repositories with ease, allowing you to create workflows in reaction to a variety of events, including pushes, pull requests, and the creation of issues.
- Community Contributions: GitHub Actions offers a vast library of pre-built actions created by the community, so you do not have to start from scratch when adding everyday tasks to your workflows.
- Visibility: GitHub Actions gives you Runtransparency and a knowledge of the automated processes that comprise your project through making it easy for you to monitor the status of your workflows within your repository.
Step-By-Step to Runscript repository Python Script in GitHub Actions
Step 1: Create a Python script repository on GitHub.
Step 2: Here is the created repository named the Python script.

Step 3: Here is the a simple python script named as the python.py.
# Simple Python script to print "Hello, world!"
print("Hello, world!")

Step 4: Here is the sample ci/cd file on GitHub action to run the python script.
name: Python Script Workflow
on:
push:
branches:
- main
jobs:
run-python-script:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Run Python script
run: python python.py

name: Python Script Workflow
The name of the workflow, "Python Script Workflow," is specified on this line. The GitHub Actions interface will show this name.
on:
push:
branches:
- main
The event that starts the workflow is specified by the on keyword. In this instance, the workflow is initiated upon each push event to the repository's main branch.
jobs:
run-python-script:
runs-on: ubuntu-latest
We define a job called "run-python-script" under the jobs key. The steps outlined in this assignment will be carried out. It states that the newest version of Ubuntu will be used for the job's execution.
steps:
- name: Checkout code
uses: actions/checkout@v2
In order to make the repository's code available for next phases, this step verifies it. It makes use of a pre-made GitHub Action for checking out code called actions/checkout@v2.
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
Using the actions/setup-python@v5 action, the Python environment is configured in this phase. Version 3.10 of Python is required to be utilized. Additional inputs can be passed to the action using the with keyword.
- name: Run Python script
run: python python.py
This step finally executes the python.py Python script. To run a command inside the job's environment, use the run keyword. The Python script is executed in this instance using the python command.
Step 5: Click on Action then click on the cicd file named as the blank.yml.

Step 6: Click on the run python script you will find the console output of the ci/cd pipeline.

Step 7: Here is the console output of the ci/cd action file named as the blank.yml

Step 8: Click on the run python script we will find the detailed console output of the python script.

Deploying Applications
Deployment to Servers: Python applications can be pushed to distant servers via GitHub Actions via SSH or other remote access techniques.
- Strategies: Use SSH actions to connect to the server and copy application files using SCP or SFTP.
- Best Practices: Securely manage SSH keys and credentials using GitHub Secrets.
Deployment to Cloud Platforms: GitHub Actions may utilize platform-specific APIs or CLI to deploy Python apps to cloud computing systems such as AWS, Azure, Google Cloud, and more.
- Strategies: Utilize cloud provider-specific actions or CLI commands to deploy applications.
- Best Practices: Leverage cloud-native services like AWS Elastic Beanstalk, Azure App Service, or Google App Engine for managed deployments.
Deployment to Containers: Docker images for Python applications can be generated via GitHub Actions and then deployed to container orchestration platforms like Docker Swarm or Kubernetes.
- Strategies: Use Docker build and push actions to build Docker images and push them to container registries.
- Best Practices: Follow containerization best practices like minimizing image size, using multi-stage builds, and leveraging layer caching.
Conclusion
GitHub Actions offers flexibility, integration, and visibility to transform the way we automate tasks in software projects. Powerful automation capabilities can be unlocked by studying deployment options and following the detailed instructions for executing Python scripts in GitHub Actions. With the help of GitHub Actions, we can optimize our procedures and save time and effort when managing projects, regardless of whether they are being deployed to servers, cloud platforms, or containers. GitHub Actions is a crucial tool in contemporary software development which encourages efficiency and collaboration because to its extensive community contributions and transparent processes. You can obtain detailed instructions on how to launch a Python script using GitHub actions by following the aforementioned steps.
Similar Reads
How to Run Bash Script in Github Actions ?
GitHub Actions are helpful resources for coding. They automate processes in your GitHub projects, save you time and effort. It is possible that GitHub Actions will automate the steps involved in testing, deploying, and alerting users of your code. Because you can configure them to run at specified t
5 min read
How to run bash script in Python?
If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python
2 min read
How to Skip a Job in GitHub Actions ?
With the help of GitHub Actions, developers can effectively automate a range of processes and workflows right within their repositories. You can develop, test, and publish your code using GitHub Actions without ever leaving the GitHub website. It provides an adaptable and configurable workflow frame
6 min read
How To Embed Python Code In Batch Script
Embedding Python code in a batch script can be very helpful for automating tasks that require both shell commands and Python scripting. In this article, we will discuss how to embed Python code within a batch script. What are Batch Scripts and Python Code?Batch scripts are text files containing a se
4 min read
How To Set Up Merge Queues in GitHub Actions
Managing pull requests (PRs) efficiently in a collaborative development environment is crucial to ensure that the codebase remains stable and conflict-free. With GitHub Actions, automating the PR merging process with merge queues can help streamline your workflow, reduce manual intervention, and ens
6 min read
How to use AWS CLI in GitHub Actions ?
Through a command-line interface, Amazon offers a powerful tool called the Amazon Web Services Command Line Interface, or AWS CLI, which makes managing AWS resources easier. The importance of this is that it can simplify the process of utilizing AWS services straight from the terminal, removing the
4 min read
How to Add GitHub Actions Secrets ?
When it comes to safely managing sensitive data in your workflowsâlike access tokens, API keys, and other credentialsâGitHub Actions secrets are essential. By using these tricks, you can securely access and save private information without exposing it to the source code of your repository. You may i
5 min read
How To Create a Pull Request in GitHub?
Pull requests are an important part of collaborative software development on GitHub. They allow developers to propose changes, review code, and discuss improvements before integrating new code into a project. This guide will walk you through the process of creating a pull request in GitHub, ensuring
2 min read
The Matrix Strategy in GitHub Actions
GitHub Actions provides a platform for automating your CI/CD pipelines, allowing you to run workflows directly from your GitHub repositories. One of the most powerful features of GitHub Actions is the matrix strategy, which allows to run a single job in multiple configurations. This can include diff
5 min read
How to check any script is running in linux using Python?
Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by
2 min read