|
1 | 1 | # Python Anywhere CI/CD pipeline POC |
2 | 2 |
|
| 3 | +# Automating Code Deployment from GitHub to PythonAnywhere |
3 | 4 |
|
4 | | - |
5 | | -I have created CI/CD pipelines to automatically deploy code updates on Github repository to pythonanywhere web application. |
6 | | - |
7 | | -Automating Code Deployment from GitHub to PythonAnywhere |
8 | | - |
9 | | -Overview |
| 5 | +## Overview |
10 | 6 |
|
11 | 7 | 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. |
12 | 8 |
|
13 | | -1. Prerequisites |
| 9 | +## 1. Prerequisites |
14 | 10 |
|
15 | 11 | Before setting up automation, ensure you have: |
16 | 12 |
|
17 | | -A PythonAnywhere account (Free or Paid) |
18 | | - |
19 | | -A GitHub repository with your project |
| 13 | +- A PythonAnywhere account (Free or Paid) |
| 14 | +- A GitHub repository with your project |
| 15 | +- API access enabled on PythonAnywhere (for Free users) |
| 16 | +- A web app running on PythonAnywhere |
20 | 17 |
|
21 | | -API access enabled on PythonAnywhere (for Free users) |
22 | | - |
23 | | -A web app running on PythonAnywhere |
24 | | - |
25 | | -2. Enable API Access on PythonAnywhere |
| 18 | +## 2. Enable API Access on PythonAnywhere |
26 | 19 |
|
27 | 20 | To use the PythonAnywhere API: |
28 | 21 |
|
29 | | -Go to PythonAnywhere Account Settings |
30 | | - |
31 | | -Scroll to API Token and click "Create API Token" (if not already created). |
| 22 | +1. Go to **PythonAnywhere Account Settings** |
| 23 | +2. Scroll to **API Token** and click **"Create API Token"** (if not already created). |
| 24 | +3. Copy the API Token, as it will be needed later. |
32 | 25 |
|
33 | | -Copy the API Token, as it will be needed later. |
| 26 | +## 3. Clone Your GitHub Repository on PythonAnywhere |
34 | 27 |
|
35 | | -3. Clone Your GitHub Repository on PythonAnywhere |
36 | | - |
37 | | -Open the Bash console on PythonAnywhere. |
38 | | - |
39 | | -Run the following command to clone your repository: |
| 28 | +Open the Bash console on PythonAnywhere and run the following commands: |
40 | 29 |
|
| 30 | +```bash |
41 | 31 | git clone https://github.com/yourusername/yourrepository.git |
42 | | - |
43 | | -Navigate to your project directory: |
44 | | - |
45 | 32 | cd yourrepository |
| 33 | +``` |
46 | 34 |
|
47 | | -4. Automate Deployment Using GitHub Actions |
| 35 | +## 4. Automate Deployment Using GitHub Actions |
48 | 36 |
|
49 | 37 | GitHub Actions will automatically update your PythonAnywhere code whenever you push changes. |
50 | 38 |
|
51 | | -Step 1: Add API Token as a GitHub Secret |
52 | | - |
53 | | -Go to your GitHub repository → Settings → Secrets and variables → Actions |
54 | | - |
55 | | -Click "New Repository Secret" |
56 | | - |
57 | | -Add the API Token: |
| 39 | +### Step 1: Add API Token as a GitHub Secret |
58 | 40 |
|
59 | | -Name: PA_API_TOKEN |
| 41 | +1. Go to **GitHub repository → Settings → Secrets and variables → Actions** |
| 42 | +2. Click **"New Repository Secret"** |
| 43 | +3. Add the API Token: |
| 44 | + - **Name:** `PA_API_TOKEN` |
| 45 | + - **Value:** Paste your PythonAnywhere API token |
| 46 | +4. Click **"Save"**. |
60 | 47 |
|
61 | | -Value: Paste your PythonAnywhere API token |
62 | | - |
63 | | -Click "Save". |
64 | | - |
65 | | -Step 2: Create a GitHub Actions Workflow |
| 48 | +### Step 2: Create a GitHub Actions Workflow |
66 | 49 |
|
67 | 50 | In your GitHub repository, create a file: |
68 | | -📄 .github/workflows/deploy.yml |
69 | 51 |
|
| 52 | +📄 `.github/workflows/deploy.yml` |
| 53 | + |
| 54 | +```yaml |
70 | 55 | name: Deploy to PythonAnywhere |
71 | 56 |
|
72 | 57 | on: |
@@ -94,63 +79,54 @@ jobs: |
94 | 79 | run: | |
95 | 80 | curl -s -X POST \ |
96 | 81 | "https://www.pythonanywhere.com/api/v0/user/${{ secrets.PA_USERNAME }}/webapps/${{ secrets.PA_USERNAME }}.pythonanywhere.com/reload/" \ |
97 | | - -H "Authorization: Token ${{ secrets.PA_API_TOKEN }}" |
98 | | - |
99 | | -Step 3: Modify the Workflow File |
100 | | - |
101 | | -Replace your_username with your PythonAnywhere username. |
102 | | - |
103 | | -Replace yourrepository with your GitHub repository name. |
104 | | - |
105 | | -Replace your_console_id (Find it by listing consoles via API: https://www.pythonanywhere.com/api/v0/user/your_username/consoles/). |
106 | | - |
107 | | -Ensure the correct branch (main) is specified in the workflow. |
| 82 | + -H "Authorization: Token ${{ secrets.PA_API_TOKEN }}" |
| 83 | +``` |
108 | 84 |
|
109 | | -5. Testing the Deployment |
| 85 | +### Step 3: Modify the Workflow File |
110 | 86 |
|
111 | | -Push a change to your GitHub repository. |
| 87 | +- Replace **your_username** with your PythonAnywhere username. |
| 88 | +- Replace **yourrepository** with your GitHub repository name. |
| 89 | +- Replace **your_console_id** (Find it by listing consoles via API: `https://www.pythonanywhere.com/api/v0/user/your_username/consoles/`). |
| 90 | +- Ensure the correct branch (**main** or **master**) is specified in the workflow. |
112 | 91 |
|
113 | | -Go to GitHub → Actions and verify the workflow execution. |
| 92 | +## 5. Testing the Deployment |
114 | 93 |
|
115 | | -If successful, check your PythonAnywhere project for the updated code. |
| 94 | +1. Push a change to your GitHub repository. |
| 95 | +2. Go to **GitHub → Actions** and verify the workflow execution. |
| 96 | +3. If successful, check your PythonAnywhere project for the updated code. |
| 97 | +4. The web app will automatically reload after deployment. |
116 | 98 |
|
117 | | -The web app will automatically reload after deployment. |
118 | | - |
119 | | -6. Manual Deployment (If Needed) |
| 99 | +## 6. Manual Deployment (If Needed) |
120 | 100 |
|
121 | 101 | If GitHub Actions is not used, you can manually pull updates: |
122 | 102 |
|
| 103 | +```bash |
123 | 104 | cd yourrepository |
124 | 105 | git pull origin main |
| 106 | +``` |
125 | 107 |
|
126 | 108 | To reload the web app manually: |
127 | 109 |
|
| 110 | +```bash |
128 | 111 | touch /var/www/your_username_pythonanywhere_com_wsgi.py |
| 112 | +``` |
129 | 113 |
|
130 | | -7. Troubleshooting |
131 | | - |
132 | | -Issue |
133 | | - |
134 | | -Solution |
| 114 | +## 7. Troubleshooting |
135 | 115 |
|
136 | | -Authentication error |
| 116 | +| Issue | Solution | |
| 117 | +|--------|----------| |
| 118 | +| **Authentication error** | Ensure the API token is correctly added in GitHub Secrets. | |
| 119 | +| **Changes not reflecting** | Verify `git pull` works in the PythonAnywhere console. | |
| 120 | +| **Console ID issue** | Use API to list active consoles and find the correct `console_id`. | |
137 | 121 |
|
138 | | -Ensure the API token is correctly added in GitHub Secrets. |
| 122 | +## Conclusion |
139 | 123 |
|
140 | | -Changes not reflecting |
141 | | - |
142 | | -Verify git pull works in the PythonAnywhere console. |
| 124 | +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. 🚀 |
143 | 125 |
|
144 | | -Console ID issue |
145 | 126 |
|
146 | | -Use API to list active consoles and find the correct console_id. |
147 | 127 |
|
148 | | -Conclusion |
149 | 128 |
|
150 | | -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. 🚀 |
151 | 129 |
|
152 | 130 |
|
153 | 131 |
|
154 | | -This should start the server on http://127.0.0.1:8000/ or http://localhost:8000/. |
155 | 132 |
|
156 | | -Nice work |
0 commit comments