This is a tutorial on how to create a GitHub Actions workflow to run a simple Python code.
The first step is to create a new GitHub Actions workflow. These steps are based on console, you can do the same with cli commands. To do this using console, follow these steps:
-
Navigate to the repository that contains your Python code. If you don’t have a repo create new one with the following code snippet. Save it as
**main.py**print("Hello from github actions commit1")Source code: https://github.com/chandradeoarya/devops-githubactions-python
-
Click on the "Actions" tab.
-
Click on the "New workflow" button.
-
Select "Python application" from the list of suggested workflows.
-
Replace the contents of the newly created
main.ymlfile with the following code:
name: Python application
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run Python script
run: python main.pyThis workflow will run your Python code whenever changes are pushed to the main branch or when pull requests are created against the main branch.
After creating the workflow file, you need to commit and push it to the repository. To do this, follow these steps:
-
Save the changes you made to the workflow file.
-
Add the workflow file to your Git staging area by running the following command:
git add .github/workflows/main.yml
-
Commit the changes by running the following command:
git commit -m "Add GitHub Actions workflow for Python code" -
Push the changes to the repository by running the following command:
git push
Once you've pushed the workflow file to your repository, GitHub Actions will automatically start running the workflow whenever changes are pushed to the main branch or when pull requests are created against the main branch.
To verify that the workflow runs successfully, follow these steps:
- Navigate to the repository that contains your Python code.
- Click on the "Actions" tab.
- Select the "Python application" workflow.
- Verify that the workflow runs successfully and that your Python code is executed.
If the workflow fails, you can click on the individual steps to see more details about what went wrong.