diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a54398f..c268311 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,45 +1,61 @@ -# This is a basic workflow to help you get started with Actions - -name: Development - -# Controls when the workflow will run +name: Development deployment from Github to AWS on: - # Triggers the workflow on push or pull request events but only for the master branch push: - branches: [development] - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: + branches: + - development -# A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: - build: + deploy-docker-image: runs-on: ubuntu-latest + steps: + - name: Checkout Latest Repo + uses: actions/checkout@master - strategy: - matrix: - node-version: [20.10.0] + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - steps: - - name: Checkout source code - uses: actions/checkout@v4 + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Install dependencies - run: npm install + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile + push: true + tags: siddharth9903/seistart:latest - - name: Build Next.js app - run: npm run build + deploy-docker-image-to-aws-eb: + needs: deploy-docker-image + runs-on: ubuntu-latest - - name: Generate deployment package - run: zip -r deploy.zip '.next' 'package.json' 'public' + steps: + - name: Get timestamp + uses: gerred/current-time@v1.0.0 + id: current-time + + - name: Run string replace + uses: frabert/replace-string-action@v2 + id: format-time + with: + pattern: '[:\.]+' + string: "${{ steps.current-time.outputs.time }}" + replace-with: "-" + flags: "g" - - name: Deploy to EB - uses: einaregilsson/beanstalk-deploy@v22 + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 with: - aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - application_name: Gateway - environment_name: next-blog-dev - region: ap-south-1 - version_label: ver-${{ github.sha }} - deployment_package: deploy.zip - use_existing_version_if_available: true + role-skip-session-tagging: true + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ap-south-1 + + - name: Deploy to Elastic Beanstalk + run: | + version_label="$GITHUB_SHA-${{ steps.format-time.outputs.outputs.replaced }}" + aws elasticbeanstalk create-application-version --application-name Gateway --version-label "$version_label" --source-bundle S3Bucket=docker-login-bucket-seistart,S3Key=Dockerrun.aws.json + aws elasticbeanstalk update-environment --application-name Gateway --environment-name seistart-docker-dev --version-label "$version_label" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3bb5fcd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM node:21 as base +# RUN apk add --no-cache g++ make py3-pip libc6-compat +WORKDIR /app +COPY package*.json ./ +RUN npm install +EXPOSE 3000 + +FROM base as builder +WORKDIR /app +COPY . . +RUN npm run build + + +FROM base as production +WORKDIR /app + +ENV NODE_ENV=production +RUN npm ci + +COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/package.json ./package.json +COPY --from=builder /app/public ./public + +CMD npm start + +FROM base as dev +ENV NODE_ENV=development +RUN npm install +COPY . . +CMD npm run dev \ No newline at end of file diff --git a/Dockerrun.aws.json b/Dockerrun.aws.json new file mode 100644 index 0000000..2ecb616 --- /dev/null +++ b/Dockerrun.aws.json @@ -0,0 +1,18 @@ +{ + "AWSEBDockerrunVersion": "1", + "Authentication": { + "bucket": "docker-login-bucket-seistart", + "key": "dockercfg" + }, + "Image": { + "Name": "siddharth9903/seistart:latest", + "Update": "true" + }, + "Ports": [ + { + "ContainerPort": 3000, + "HostPort": 80 + } + ], + "Logging": "/var/log/nginx" +} diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..7af2066 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,14 @@ +version: "1" +services: + app: + image: seistart + build: + context: ./ + target: dev + dockerfile: Dockerfile + volumes: + - .:/app + - /app/node_modules + - /app/.next + ports: + - "3000:3000" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ed8ccfb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3" +services: + app: + image: seistart + build: + context: ./ + target: production + dockerfile: Dockerfile + ports: + - "80:3000"