From aa7759878bbb8902cae8ca0843189f9f6be01ad7 Mon Sep 17 00:00:00 2001 From: yibowdevops Date: Fri, 8 Jul 2022 12:42:14 +1000 Subject: [PATCH 1/4] yibo.jenkinsfile created --- CI-CD/yibo.Jenkinsfile | 161 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 CI-CD/yibo.Jenkinsfile diff --git a/CI-CD/yibo.Jenkinsfile b/CI-CD/yibo.Jenkinsfile new file mode 100644 index 00000000..9a58d2f6 --- /dev/null +++ b/CI-CD/yibo.Jenkinsfile @@ -0,0 +1,161 @@ +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') + } + + + + + stages { + stage('Cleanup docker space') { + steps { + echo 'Cleanup docker space' + sh 'docker system prune -f' + } + } + + 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 install' + } + } + + stage('Test') { + steps { + echo 'Testing..' + sh 'yarn test:coverage' + } + } + + 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 ('Integrated Test') { + steps { + echo 'Integrated Test....' + sh 'yarn e2e:start-server:record' + } + } + + + // stage ('Container Integrated Test') { + // steps { + // echo 'Container Integrated Test....' + // sh + // } + // } + + stage('Build and Upload Image to ECR') { + + when { + anyOf { + branch 'develop' + branch 'CI-CD' + } + } + + steps { + echo 'build image with ecr tage...' + withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { + sh "docker build --build-arg N_G_ENDPOINT=${N_G_ENDPOINT} --build-arg G_ENDPOINT=${G_ENDPOINT} -t ${AWS_ECR_URL}:${IMAGE_VERSION} ." + } + + echo 'upload to ECR' + withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { + withAWS(region: "${AWS_ECR_REGION}", credentials: 'AWS_Credentials') { + script { + def login = ecrLogin() + sh('#!/bin/sh -e\n' + "${login}") // hide logging + docker.image("${AWS_ECR_URL}:${IMAGE_VERSION}").push() + } + } + } + } + } + + stage('Deploy to EKS') { + + when { + anyOf { + branch 'develop' + branch 'CI-CD' + } + } + + steps { + echo 'Git check out terrafom repo...' + // Get source code from a GitHub repository + git branch: 'master',credentialsId:'Terraform-repo-access' ,url:'git@github.com:A-Comosus/a-comosus-terraform.git' + + + echo 'Update EKS through terraform....' + + echo 'inital terraform' + withAWS(region: "${AWS_ECR_REGION}", credentials:'eks-jenkins-update-account') { + dir("kubernetes-config") { + sh 'terraform init' + } + } + + echo 'terraform apply' + withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { + withAWS(region: "${AWS_ECR_REGION}", credentials:'eks-jenkins-update-account') { + dir("kubernetes-config") { + script { + sh("terraform apply -var=\'frontend-image=${AWS_ECR_URL}:${IMAGE_VERSION}\' --auto-approve") + } + } + } + } + + } + } + } + + post { + always { + withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { + deleteDir() + } + } + } +} \ No newline at end of file From 152b40717f18487b90f8203d245a7b252e1bc4a7 Mon Sep 17 00:00:00 2001 From: yibowdevops Date: Fri, 8 Jul 2022 12:52:54 +1000 Subject: [PATCH 2/4] adding dockerfile.yibo --- Dockerfile.yibo | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Dockerfile.yibo 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 From ab9afeccd40eefcc3bf62f9a4b039da877b6050f Mon Sep 17 00:00:00 2001 From: yibowdevops Date: Fri, 8 Jul 2022 17:33:09 +1000 Subject: [PATCH 3/4] update Jenkinsfile withCredentials --- CI-CD/yibo.Jenkinsfile | 81 ++++++++---------------------------------- 1 file changed, 15 insertions(+), 66 deletions(-) diff --git a/CI-CD/yibo.Jenkinsfile b/CI-CD/yibo.Jenkinsfile index 9a58d2f6..7e25f978 100644 --- a/CI-CD/yibo.Jenkinsfile +++ b/CI-CD/yibo.Jenkinsfile @@ -21,16 +21,19 @@ pipeline { 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 docker space') { + stage('Cleanup Worker space') { steps { - echo 'Cleanup docker space' - sh 'docker system prune -f' + cleanWs() + checkout scm } } @@ -44,7 +47,8 @@ pipeline { stage('Install dependencies') { steps { echo 'Install dependencies...' - sh 'yarn install' + sh 'yarn' + sh 'yarn codegen' } } @@ -52,6 +56,7 @@ pipeline { steps { echo 'Testing..' sh 'yarn test:coverage' + sh 'yarn e2e:start-server:record' } } @@ -75,15 +80,7 @@ pipeline { sh 'yarn e2e:start-server:record' } } - - - // stage ('Container Integrated Test') { - // steps { - // echo 'Container Integrated Test....' - // sh - // } - // } - + stage('Build and Upload Image to ECR') { when { @@ -94,62 +91,14 @@ pipeline { } steps { - echo 'build image with ecr tage...' - withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { - sh "docker build --build-arg N_G_ENDPOINT=${N_G_ENDPOINT} --build-arg G_ENDPOINT=${G_ENDPOINT} -t ${AWS_ECR_URL}:${IMAGE_VERSION} ." - } - - echo 'upload to ECR' - withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { - withAWS(region: "${AWS_ECR_REGION}", credentials: 'AWS_Credentials') { - script { - def login = ecrLogin() - sh('#!/bin/sh -e\n' + "${login}") // hide logging - docker.image("${AWS_ECR_URL}:${IMAGE_VERSION}").push() - } + 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() } } - } + } } - - stage('Deploy to EKS') { - - when { - anyOf { - branch 'develop' - branch 'CI-CD' - } - } - - steps { - echo 'Git check out terrafom repo...' - // Get source code from a GitHub repository - git branch: 'master',credentialsId:'Terraform-repo-access' ,url:'git@github.com:A-Comosus/a-comosus-terraform.git' - - - echo 'Update EKS through terraform....' - - echo 'inital terraform' - withAWS(region: "${AWS_ECR_REGION}", credentials:'eks-jenkins-update-account') { - dir("kubernetes-config") { - sh 'terraform init' - } - } - - echo 'terraform apply' - withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) { - withAWS(region: "${AWS_ECR_REGION}", credentials:'eks-jenkins-update-account') { - dir("kubernetes-config") { - script { - sh("terraform apply -var=\'frontend-image=${AWS_ECR_URL}:${IMAGE_VERSION}\' --auto-approve") - } - } - } - } - - } - } - } post { always { From 4cf37923b70316901e432bdd629cb904184da4c3 Mon Sep 17 00:00:00 2001 From: yibowdevops Date: Thu, 14 Jul 2022 08:52:24 +1000 Subject: [PATCH 4/4] update Jenkinsfile as per pr comments --- CI-CD/yibo.Jenkinsfile | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/CI-CD/yibo.Jenkinsfile b/CI-CD/yibo.Jenkinsfile index 7e25f978..9be34909 100644 --- a/CI-CD/yibo.Jenkinsfile +++ b/CI-CD/yibo.Jenkinsfile @@ -73,13 +73,6 @@ pipeline { sh 'yarn build:with-codegen' } } - - stage ('Integrated Test') { - steps { - echo 'Integrated Test....' - sh 'yarn e2e:start-server:record' - } - } stage('Build and Upload Image to ECR') { @@ -106,5 +99,13 @@ pipeline { 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