Skip to content

Commit 8b219eb

Browse files
authored
Update README.md
1 parent 07221ca commit 8b219eb

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,153 @@ Metro Fare Calculator is a simple web application designed to help users calcula
66

77
The application is built using the Django framework and is designed to be responsive and easy to use. Users simply select their starting station and ending station from a dropdown menu, and the application calculates the fare based on the distance between the two stations.
88

9+
I have created CI/CD pipelines to automatically deploy code updates on Github repository to pythonanywhere web application.
10+
11+
Automating Code Deployment from GitHub to PythonAnywhere
12+
13+
Overview
14+
15+
This document provides a step-by-step guide to automatically deploy code updates from a GitHub repository to a PythonAnywhere web application using GitHub Actions and the PythonAnywhere API.
16+
17+
1. Prerequisites
18+
19+
Before setting up automation, ensure you have:
20+
21+
A PythonAnywhere account (Free or Paid)
22+
23+
A GitHub repository with your project
24+
25+
API access enabled on PythonAnywhere (for Free users)
26+
27+
A web app running on PythonAnywhere
28+
29+
2. Enable API Access on PythonAnywhere
30+
31+
To use the PythonAnywhere API:
32+
33+
Go to PythonAnywhere Account Settings
34+
35+
Scroll to API Token and click "Create API Token" (if not already created).
36+
37+
Copy the API Token, as it will be needed later.
38+
39+
3. Clone Your GitHub Repository on PythonAnywhere
40+
41+
Open the Bash console on PythonAnywhere.
42+
43+
Run the following command to clone your repository:
44+
45+
git clone https://github.com/yourusername/yourrepository.git
46+
47+
Navigate to your project directory:
48+
49+
cd yourrepository
50+
51+
4. Automate Deployment Using GitHub Actions
52+
53+
GitHub Actions will automatically update your PythonAnywhere code whenever you push changes.
54+
55+
Step 1: Add API Token as a GitHub Secret
56+
57+
Go to your GitHub repository → Settings → Secrets and variables → Actions
58+
59+
Click "New Repository Secret"
60+
61+
Add the API Token:
62+
63+
Name: PA_API_TOKEN
64+
65+
Value: Paste your PythonAnywhere API token
66+
67+
Click "Save".
68+
69+
Step 2: Create a GitHub Actions Workflow
70+
71+
In your GitHub repository, create a file:
72+
📄 .github/workflows/deploy.yml
73+
74+
name: Deploy to PythonAnywhere
75+
76+
on:
77+
push:
78+
branches:
79+
- master
80+
81+
jobs:
82+
deploy:
83+
runs-on: ubuntu-latest
84+
85+
steps:
86+
- name: Checkout repository
87+
uses: actions/checkout@v4
88+
89+
- name: Update code on PythonAnywhere
90+
run: |
91+
curl -s -X POST \
92+
"https://www.pythonanywhere.com/api/v0/user/${{ secrets.PA_USERNAME }}/consoles/38366532/send_input/" \
93+
-H "Authorization: Token ${{ secrets.PA_API_TOKEN }}" \
94+
-d '{"input": "git pull origin master\n"}' \
95+
-H "Content-Type: application/json"
96+
97+
- name: Reload PythonAnywhere Web App
98+
run: |
99+
curl -s -X POST \
100+
"https://www.pythonanywhere.com/api/v0/user/${{ secrets.PA_USERNAME }}/webapps/${{ secrets.PA_USERNAME }}.pythonanywhere.com/reload/" \
101+
-H "Authorization: Token ${{ secrets.PA_API_TOKEN }}"
102+
103+
Step 3: Modify the Workflow File
104+
105+
Replace your_username with your PythonAnywhere username.
106+
107+
Replace yourrepository with your GitHub repository name.
108+
109+
Replace your_console_id (Find it by listing consoles via API: https://www.pythonanywhere.com/api/v0/user/your_username/consoles/).
110+
111+
Ensure the correct branch (main) is specified in the workflow.
112+
113+
5. Testing the Deployment
114+
115+
Push a change to your GitHub repository.
116+
117+
Go to GitHub → Actions and verify the workflow execution.
118+
119+
If successful, check your PythonAnywhere project for the updated code.
120+
121+
The web app will automatically reload after deployment.
122+
123+
6. Manual Deployment (If Needed)
124+
125+
If GitHub Actions is not used, you can manually pull updates:
126+
127+
cd yourrepository
128+
git pull origin main
129+
130+
To reload the web app manually:
131+
132+
touch /var/www/your_username_pythonanywhere_com_wsgi.py
133+
134+
7. Troubleshooting
135+
136+
Issue
137+
138+
Solution
139+
140+
Authentication error
141+
142+
Ensure the API token is correctly added in GitHub Secrets.
143+
144+
Changes not reflecting
145+
146+
Verify git pull works in the PythonAnywhere console.
147+
148+
Console ID issue
149+
150+
Use API to list active consoles and find the correct console_id.
151+
152+
Conclusion
153+
154+
This guide helps automate deployment from GitHub to PythonAnywhere using API calls. Once set up, every push to GitHub updates the PythonAnywhere project automatically and reloads the web app. 🚀
155+
9156
![Metro Fare - Google Chrome 19-04-2023 20_39_29 (2)](https://user-images.githubusercontent.com/130206125/233120574-f736e375-7014-452c-b1e9-cc00664972f8.png)
10157

11158
![Screenshot (40)](https://user-images.githubusercontent.com/130206125/233118575-af100009-d9e5-47f9-8d16-b4e9e46a37f9.png)

0 commit comments

Comments
 (0)