diff --git a/CI-CD/yibo.Jenkinsfile b/CI-CD/yibo.Jenkinsfile new file mode 100644 index 00000000..9be34909 --- /dev/null +++ b/CI-CD/yibo.Jenkinsfile @@ -0,0 +1,111 @@ +pipeline { + agent any + + options { + // Keep artifacts and logs of last 10 builds + buildDiscarder(logRotator(numToKeepStr: '10')) + // Disallows concurrent executions of the Pipeline. + disableConcurrentBuilds() + // Sets a timeout period for the Pipeline run, after which Jenkins should abort the Pipeline. + // Set timeout period as 1 hour to prevent infinite blocks in our Jenkins. + timeout(time: 1, unit: 'HOURS') + // Prepends all console output generated by the Pipeline run with the time at which the line was emitted. + timestamps() + } + + tools { + dockerTool 'docker-latest' + } + + environment { + IMAGE_VERSION = "${env.BUILD_ID}" + AWS_REGION = 'ap-southeast-2' + CYPRESS_RECORD_KEY = credentials('cypress-record-key') + N_G_ENDPOINT = credentials('N_G_ENDPOINT') + ECR_URL = credentials('ECR_REGISTRY_URL') + AWS_ECR_URL = credentials('AWS_ECR_URL') + } + + + + + stages { + stage('Cleanup Worker space') { + steps { + cleanWs() + checkout scm + } + } + + stage('Git check out') { + steps{ + echo 'Git check out...' + // Get source code from a GitHub repository + git branch: env.BRANCH_NAME, url:'https://github.com/A-Comosus/comosus-client.git' + } + } + stage('Install dependencies') { + steps { + echo 'Install dependencies...' + sh 'yarn' + sh 'yarn codegen' + } + } + + stage('Test') { + steps { + echo 'Testing..' + sh 'yarn test:coverage' + sh 'yarn e2e:start-server:record' + } + } + + stage('Checking for linter error') { + steps { + echo 'Checking for linter error....' + sh 'yarn lint' + } + } + + stage('Build') { + steps { + echo 'Building....' + sh 'yarn build:with-codegen' + } + } + + stage('Build and Upload Image to ECR') { + + when { + anyOf { + branch 'develop' + branch 'CI-CD' + } + } + + steps { + script{ + docker.withRegistry("https://${AWS_ECR_URL}", 'ecr:ap-southeast-2:AWS_CREDENTIAL') { + app = docker.build("${ECR_URL}:${env.BUILD_NUMBER}", "--build-arg N_G_ENDPOINT=${N_G_ENDPOINT} -f Dockerfile.yibo .") + app.push() + } + } + } + } + + post { + always { + withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { + deleteDir() + } + } + success { + echo 'Pipeline Successed :)' + } + failure { + mail to: 'Norris.wu.au@outlook.com', + subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", + body: "Project: ${env.JOB_NAME}, Build Number: ${env.BUILD_NUMBER}, Something is wrong with ${env.BUILD_URL}" + } + } +} \ No newline at end of file diff --git a/Dockerfile.yibo b/Dockerfile.yibo new file mode 100644 index 00000000..aee91f14 --- /dev/null +++ b/Dockerfile.yibo @@ -0,0 +1,48 @@ +# Install dependencies only when needed +FROM node:16-alpine AS build_env +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile + +# Rebuild the source code only when needed +FROM node:16-alpine AS builder +WORKDIR /app + +COPY --from=build_env /app/node_modules ./node_modules +COPY . . + +ARG N_G_ENDPOINT + +ENV NEXT_PUBLIC_GRAPHQL_ENDPOINT ${N_G_ENDPOINT} + +RUN yarn codegen && yarn build + +FROM node:16-alpine AS runner +WORKDIR /app + +ENV NODE_ENV production + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +# You only need to copy next.config.js if you are NOT using the default configuration +COPY --from=builder /app/next.config.js ./ +COPY --from=builder /app/public ./public +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 + +USER nextjs + +EXPOSE 3000 + +ENV PORT 3000 + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry. +# ENV NEXT_TELEMETRY_DISABLED 1 + +CMD ["node_modules/.bin/next", "start"] \ No newline at end of file