Skip to content

Commit 1cbaba9

Browse files
Merge pull request #18 from Happyesss/main
fixes
2 parents 53e2cd3 + 2a24aee commit 1cbaba9

17 files changed

Lines changed: 992 additions & 13 deletions

File tree

.github/workflows/cd.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: CD
2+
3+
on:
4+
workflow_dispatch:
5+
workflow_run:
6+
workflows: ["CI"]
7+
types:
8+
- completed
9+
branches: [ main ]
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v3
21+
22+
- name: Login to Docker Hub
23+
uses: docker/login-action@v3
24+
with:
25+
username: ${{ secrets.DOCKERHUB_USERNAME }}
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
28+
- name: Build and push Docker image
29+
uses: docker/build-push-action@v5
30+
with:
31+
context: .
32+
push: true
33+
tags: |
34+
${{ secrets.DOCKERHUB_USERNAME }}/fairlx:latest
35+
${{ secrets.DOCKERHUB_USERNAME }}/fairlx:${{ github.sha }}
36+
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/fairlx:buildcache
37+
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/fairlx:buildcache,mode=max
38+
39+
# Deploy to Digital Ocean
40+
- name: Deploy to Digital Ocean
41+
uses: appleboy/ssh-action@v1.0.3
42+
with:
43+
host: ${{ secrets.DO_HOST }}
44+
username: ${{ secrets.DO_USERNAME }}
45+
password: ${{ secrets.DO_PASSWORD }}
46+
port: ${{ secrets.DO_PORT }}
47+
script: |
48+
# Stop and remove existing container if it exists
49+
docker stop fairlx-app || true
50+
docker rm fairlx-app || true
51+
52+
# Remove old image to save space
53+
docker rmi ${{ secrets.DOCKERHUB_USERNAME }}/fairlx:latest || true
54+
55+
# Pull the latest image
56+
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/fairlx:latest
57+
58+
# Run the new container
59+
docker run -d \
60+
--name fairlx-app \
61+
--restart unless-stopped \
62+
-p 3000:3000 \
63+
-e NODE_ENV=production \
64+
-e NEXT_TELEMETRY_DISABLED=1 \
65+
-e NEXT_PUBLIC_APP_URL="${{ secrets.NEXT_PUBLIC_APP_URL }}" \
66+
-e NEXT_PUBLIC_APPWRITE_ENDPOINT="${{ secrets.NEXT_PUBLIC_APPWRITE_ENDPOINT }}" \
67+
-e NEXT_PUBLIC_APPWRITE_PROJECT="${{ secrets.NEXT_PUBLIC_APPWRITE_PROJECT }}" \
68+
-e NEXT_PUBLIC_APPWRITE_DATABASE_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_DATABASE_ID }}" \
69+
-e NEXT_PUBLIC_APPWRITE_WORKSPACES_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_WORKSPACES_ID }}" \
70+
-e NEXT_PUBLIC_APPWRITE_MEMBERS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_MEMBERS_ID }}" \
71+
-e NEXT_PUBLIC_APPWRITE_PROJECTS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_PROJECTS_ID }}" \
72+
-e NEXT_PUBLIC_APPWRITE_TASKS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_TASKS_ID }}" \
73+
-e NEXT_PUBLIC_APPWRITE_TIME_LOGS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_TIME_LOGS_ID }}" \
74+
-e NEXT_PUBLIC_APPWRITE_CUSTOM_COLUMNS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_CUSTOM_COLUMNS_ID }}" \
75+
-e NEXT_PUBLIC_APPWRITE_DEFAULT_COLUMN_SETTINGS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_DEFAULT_COLUMN_SETTINGS_ID }}" \
76+
-e NEXT_PUBLIC_APPWRITE_IMAGES_BUCKET_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_IMAGES_BUCKET_ID }}" \
77+
-e NEXT_PUBLIC_APPWRITE_ATTACHMENTS_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_ATTACHMENTS_ID }}" \
78+
-e NEXT_PUBLIC_APPWRITE_ATTACHMENTS_BUCKET_ID="${{ secrets.NEXT_PUBLIC_APPWRITE_ATTACHMENTS_BUCKET_ID }}" \
79+
-e NEXT_APPWRITE_KEY="${{ secrets.NEXT_APPWRITE_KEY }}" \
80+
${{ secrets.DOCKERHUB_USERNAME }}/fairlx:latest
81+
82+
# Clean up unused Docker images
83+
docker image prune -f
84+
85+
# Wait a moment for the container to start
86+
sleep 10
87+
88+
# Check if the container is running
89+
if docker ps | grep -q fairlx-app; then
90+
echo "Deployment successful! Container is running."
91+
else
92+
echo "Deployment failed! Container is not running."
93+
docker logs fairlx-app
94+
exit 1
95+
fi
96+
97+
- name: Deployment Status
98+
run: |
99+
echo "Deployment to Digital Ocean completed successfully!"
100+
echo "Application URL: http://${{ secrets.DO_HOST }}:3000"
101+
echo "Docker image: ${{ secrets.DOCKERHUB_USERNAME }}/fairlx:latest"

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [22.15.0]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: 'npm'
25+
26+
- name: Install dependencies
27+
run: npm install
28+
29+
- name: Lint check
30+
run: npm run lint
31+
32+
- name: Build
33+
run: npm run build
34+
env:
35+
NEXT_TELEMETRY_DISABLED: 1
36+
37+
- name: Cache build outputs
38+
uses: actions/cache@v3
39+
with:
40+
path: |
41+
.next/cache
42+
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
43+
restore-keys: |
44+
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181

8282
## 🚀 Getting Started
8383

84+
### CI/CD Setup
85+
86+
This project uses GitHub Actions for CI/CD. Before you begin:
87+
88+
1. Set up required secrets in GitHub repository settings:
89+
- `DOCKERHUB_USERNAME`: Your Docker Hub username
90+
- `DOCKERHUB_TOKEN`: Docker Hub access token
91+
92+
For detailed CI/CD setup instructions, see:
93+
- [CI/CD Setup Guide](./docs/CICD_SETUP.md)
94+
- [Secrets Setup Guide](./docs/SECRETS_SETUP.md)
95+
8496
### Prerequisites
8597

8698
- **Node.js** 18+ and **npm** (or **Bun** for faster package management)

dockerfile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# syntax=docker/dockerfile:1
2+
3+
# Base image for building
4+
FROM node:20-alpine AS base
5+
6+
# Install dependencies only when needed
7+
FROM base AS deps
8+
RUN apk add --no-cache libc6-compat
9+
WORKDIR /app
10+
11+
# Copy package files
12+
COPY package.json bun.lock ./
13+
RUN npm install
14+
15+
# Rebuild the source code only when needed
16+
FROM base AS builder
17+
WORKDIR /app
18+
COPY --from=deps /app/node_modules ./node_modules
19+
COPY . .
20+
21+
# Disable telemetry during the build
22+
ENV NEXT_TELEMETRY_DISABLED 1
23+
24+
# Build the application
25+
RUN npm run build
26+
27+
# Production image, copy all the files and run next
28+
FROM base AS runner
29+
WORKDIR /app
30+
31+
# Don't run production as root
32+
RUN addgroup --system --gid 1001 nodejs
33+
RUN adduser --system --uid 1001 nextjs
34+
35+
# Disable telemetry during runtime
36+
ENV NEXT_TELEMETRY_DISABLED 1
37+
ENV NODE_ENV production
38+
39+
# Copy necessary files
40+
COPY --from=builder /app/public ./public
41+
42+
# Set the correct permission for prerender cache
43+
RUN mkdir .next
44+
RUN chown nextjs:nodejs .next
45+
46+
# Automatically leverage output traces to reduce image size
47+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
48+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
49+
50+
# Switch to non-root user
51+
USER nextjs
52+
53+
# Expose the port the app runs on
54+
EXPOSE 3000
55+
56+
ENV PORT 3000
57+
ENV HOSTNAME "0.0.0.0"
58+
59+
CMD ["node", "server.js"]

docs/CICD_SETUP.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# CI/CD Setup
2+
3+
This document explains how to set up the continuous integration and continuous deployment (CI/CD) pipeline for this project.
4+
5+
## GitHub Actions
6+
7+
The project uses GitHub Actions for automated testing, building, and deployment. Two main workflows are configured:
8+
9+
- `ci.yml`: Runs tests and checks on every pull request
10+
- `cd.yml`: Handles deployment when code is merged to main
11+
12+
## Required Secrets
13+
14+
Before the CI/CD pipelines can work, you need to set up the following secrets in your GitHub repository:
15+
16+
### Docker Hub Secrets
17+
| Secret Name | Description | How to Get |
18+
|-------------|-------------|------------|
19+
| `DOCKERHUB_USERNAME` | Your Docker Hub username | Your Docker Hub account username |
20+
| `DOCKERHUB_TOKEN` | Access token for Docker Hub | Generate from Docker Hub security settings |
21+
22+
For detailed instructions on setting up secrets, see [SECRETS_SETUP.md](./SECRETS_SETUP.md).
23+
24+
## Workflows
25+
26+
### Continuous Integration (ci.yml)
27+
- Runs on push to main and pull requests
28+
- Performs:
29+
- Dependency installation
30+
- Linting
31+
- Building
32+
- Testing
33+
- Cache management
34+
35+
### Continuous Deployment (cd.yml)
36+
- Runs on push to main
37+
- Performs:
38+
- Docker image building
39+
- Docker image pushing
40+
- Deployment (if configured)
41+
42+
## Setting Up CI/CD
43+
44+
1. Fork/clone the repository
45+
2. Set up required secrets (see above)
46+
3. Enable GitHub Actions in your repository
47+
4. Push to trigger the workflows
48+
49+
## Monitoring
50+
51+
- Check the "Actions" tab in GitHub to monitor workflow runs
52+
- Review workflow logs for any issues
53+
- Set up notifications for workflow failures
54+
55+
## Customizing
56+
57+
To customize the CI/CD pipeline:
58+
59+
1. Edit workflows in `.github/workflows/`
60+
2. Test changes in a branch
61+
3. Update documentation
62+
4. Create a pull request
63+
64+
## Troubleshooting
65+
66+
Common issues and solutions:
67+
68+
1. **Workflow Failures**
69+
- Check secrets are properly set
70+
- Verify Docker Hub credentials
71+
- Review action logs for errors
72+
73+
2. **Docker Issues**
74+
- Confirm Docker Hub access
75+
- Check image build steps
76+
- Verify registry permissions
77+
78+
3. **Deployment Problems**
79+
- Review deployment credentials
80+
- Check environment variables
81+
- Verify hosting platform status

0 commit comments

Comments
 (0)