|
1 | | -name: Deploy Next.js Docker App |
| 1 | +name: '🚀 Deploy Next.js Docker App (Single Job)' |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: |
6 | | - - main |
| 5 | + branches: [main] |
7 | 6 |
|
8 | 7 | jobs: |
9 | 8 | build-and-deploy: |
10 | 9 | runs-on: self-hosted |
11 | | - |
| 10 | + name: '🐳 Build & Deploy' |
12 | 11 | steps: |
13 | | - - name: Check out repository |
| 12 | + - name: '🔍 Checkout Code' |
14 | 13 | uses: actions/checkout@v3 |
15 | 14 |
|
16 | | - # Debug: Verify secret exists |
17 | | - - name: Check secret presence |
| 15 | + # ======================== |
| 16 | + # 🔐 Secrets & Config Setup |
| 17 | + # ======================== |
| 18 | + - name: '🔒 Verify Secrets Exist' |
18 | 19 | run: | |
19 | 20 | if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then |
20 | | - echo "❌ Critical error: GOOGLE_SERVICES_JSON_BASE64 secret is empty!" |
| 21 | + echo "❌ Critical error: GOOGLE_SERVICES_JSON_BASE64 secret missing!" |
21 | 22 | exit 1 |
22 | | - else |
23 | | - echo "✅ Secret exists (length: ${#GOOGLE_SERVICES_JSON_BASE64} chars)" |
24 | 23 | fi |
25 | | - env: |
26 | | - GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }} |
| 24 | + echo "✅ All secrets present" |
27 | 25 |
|
28 | | - # Decode with error handling |
29 | | - - name: Create google-services.json |
| 26 | + - name: '📁 Create google-services.json' |
30 | 27 | run: | |
31 | | - set -e # Exit immediately on error |
32 | | - echo "Decoding base64 secret..." |
33 | 28 | echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json |
34 | | -
|
35 | | - echo "Verifying file:" |
36 | | - ls -lh google-services.json |
37 | | - file google-services.json |
38 | | - head -n 2 google-services.json |
39 | | -
|
40 | | - if [ ! -s google-services.json ]; then |
41 | | - echo "❌ File is empty!" |
42 | | - exit 1 |
43 | | - fi |
| 29 | + echo "🔄 Validating JSON..." |
| 30 | + jq empty google-services.json # Requires jq installed |
44 | 31 | env: |
45 | 32 | GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }} |
46 | 33 |
|
47 | | - - name: Create .env file |
| 34 | + - name: '⚙️ Create .env File' |
48 | 35 | run: | |
49 | 36 | echo "${{ secrets.ENV_FILE_CONTENT }}" > .env |
50 | | - echo "" >> .env # Ensure file ends with a newline |
| 37 | + echo "" >> .env # Ensure trailing newline |
51 | 38 |
|
52 | | - - name: Debug .env file |
53 | | - run: | |
54 | | - echo "=== .env file content ===" |
55 | | - cat .env |
56 | | - echo "=== End of .env file ===" |
57 | | -
|
58 | | - # Debug: Check file size of google-services.json |
59 | | - - name: Check google-services.json file size |
60 | | - run: wc -c google-services.json |
61 | | - |
62 | | - # Validate JSON syntax |
63 | | - - name: Validate google-services.json |
64 | | - run: node -e "JSON.parse(require('fs').readFileSync('google-services.json','utf8'))" |
65 | | - |
66 | | - - name: Build Docker image |
| 39 | + # ======================== |
| 40 | + # 🐳 Docker Operations |
| 41 | + # ======================== |
| 42 | + - name: '🛠 Build Docker Image' |
67 | 43 | run: docker build -t codebuilder-webapp:latest . |
68 | 44 |
|
69 | | - - name: Stop and remove old container (if running) |
| 45 | + - name: '🗑 Cleanup Old Containers' |
70 | 46 | run: | |
71 | | - OLD_CONTAINER_ID=$(docker ps -aq --filter name=codebuilder-webapp) |
72 | | - if [ -n "$OLD_CONTAINER_ID" ]; then |
73 | | - echo "Stopping and removing container $OLD_CONTAINER_ID..." |
74 | | - docker stop $OLD_CONTAINER_ID |
75 | | - docker rm $OLD_CONTAINER_ID |
76 | | - else |
77 | | - echo "No old container found." |
78 | | - fi |
| 47 | + docker ps -aq --filter name=codebuilder-webapp | xargs -r docker rm -f |
79 | 48 |
|
80 | | - - name: Start new container |
81 | | - run: docker run -d --network host -p 3000:3000 --env-file .env --name codebuilder-webapp codebuilder-webapp:latest |
| 49 | + - name: '🚀 Launch New Container' |
| 50 | + run: | |
| 51 | + docker run -d \ |
| 52 | + --network host \ |
| 53 | + -p 3000:3000 \ |
| 54 | + --env-file .env \ |
| 55 | + --name codebuilder-webapp \ |
| 56 | + codebuilder-webapp:latest |
0 commit comments