This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Next.js to Harbor | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
name: Build and Push Docker Image | |
runs-on: ubuntu-latest | |
env: # Env variables needed during build | |
NEXT_PUBLIC_DOMAIN_BACKEND: ${{secrets.NEXT_PUBLIC_DOMAIN_BACKEND}} | |
NEXT_PUBLIC_PROJECT_ID: ${{secrets.NEXT_PUBLIC_PROJECT_ID}} | |
API_KEY: ${{secrets.API_KEY}} | |
steps: | |
# Step 1: Checkout the code | |
- name: Checkout Code | |
uses: actions/checkout@v4 | |
# Step 2: Set up Node.js and install dependencies with pnpm | |
- uses: pnpm/action-setup@v4 | |
name: Install pnpm | |
with: | |
version: 10 | |
run_install: false | |
- name: Install Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
cache: 'pnpm' | |
- name: Install Dependencies | |
run: pnpm install | |
# Step 3: Build the Next.js project | |
- name: Build Project | |
run: pnpm build | |
# Step 4: Prepare the Docker image | |
- name: Log in to Harbor Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: harbor.fayevr.dev | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build Docker Image | |
run: | | |
echo "FROM node:18-alpine AS base" > Dockerfile | |
echo "FROM base AS deps" >> Dockerfile | |
echo "RUN apk add --no-cache libc6-compat" >> Dockerfile | |
echo "WORKDIR /app" >> Dockerfile | |
echo "ENV NODE_ENV=production" >> Dockerfile | |
echo "RUN addgroup --system --gid 1001 nodejs" >> Dockerfile | |
echo "RUN adduser --system --uid 1001 nextjs" >> Dockerfile | |
echo "COPY --from=builder /app/public ./public" >> Dockerfile | |
echo "COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./" >> Dockerfile | |
echo "COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static" >> Dockerfile | |
echo "USER nextjs" >> Dockerfile | |
echo "EXPOSE 3000" >> Dockerfile | |
echo "ENV PORT=3000" >> Dockerfile | |
echo "CMD [\"node\", \"server.js\"]" >> Dockerfile | |
docker build -t harbor.fayevr.dev/fleet/fayevr.dev:latest . | |
# Step 5: Push the Docker image to Harbor | |
- name: Push Docker Image | |
run: docker push harbor.fayevr.dev/fleet/fayevr.dev:latest |