From 044943f19ae905e72a3a36cef62ab6978b3372f6 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 18 Aug 2022 10:58:47 +0200 Subject: [PATCH] Added Jenkinsfile and Dockerfile --- Dockerfile | 18 +++++++++++++++ Jenkinsfile | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2cbabfa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM node:16-alpine + +WORKDIR /app +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +COPY ./.next /app/.next +COPY ./public /app/public +COPY ./res /app/res +COPY ./node_modules /app/node_modules +COPY ./next.config.js /app/next.config.js +COPY ./package.json /app/package.json +COPY ./yarn.lock /app/yarn.lock + +RUN npm install + +ENTRYPOINT npm start +EXPOSE 3000 diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..fe525de --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,64 @@ +#!groovy + +node { + stage('Checkout') { + checkout scm + } + + stage('Prepare') { + sh 'git rev-parse --verify HEAD > GIT_COMMIT' + env.GIT_COMMIT = readFile('GIT_COMMIT').trim().substring(0, 10) + sh 'rm GIT_COMMIT' + def jobName = env.JOB_NAME.tokenize('/') + print env.jobName + + // set env + env.DOCKER_NAME="${jobName[0]}/${jobName[1]}:${env.BRANCH_NAME}-${env.GIT_COMMIT}" + env.DOCKER_REGISTRY="docker.sebse.de" + + env.NODEJS_HOME = "${tool 'node16'}" + env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}" + env.NEXT_TELEMETRY_DISABLED=1 + + def customImage + } + + /*stage('Test') { + env.NODE_ENV = "test" + print "Environment will be : ${env.NODE_ENV}" + sh 'node -v' + sh 'yarn install' + sh 'yarn test' + }*/ + + stage('Build Prod') { + env.NODE_ENV = "production" + print "Environment will be : ${env.NODE_ENV}" + + sh 'node -v' + sh 'yarn install --production=false' + sh 'yarn run build' + sh 'rm node_modules -rf' + sh 'yarn install --production=true' + } + + stage('Build Docker') { + customImage = docker.build(env.DOCKER_NAME) + } + + stage('Deploy') { + echo 'Push to Repo' + docker.withRegistry("https://${DOCKER_REGISTRY}", 'docker-registry-robot-web-development') { + /* Push the container to the custom Registry */ + customImage.push() + } + } + + stage('Cleanup') { + echo 'prune and cleanup' + sh 'rm node_modules -rf' + sh "docker rmi ${env.DOCKER_NAME} ${DOCKER_REGISTRY}/${env.DOCKER_NAME}" + sh 'docker image prune -f' + sh 'docker rmi $(docker images -a | grep "" | awk "{print \$3}") &2>/del/null || exit 0' + } +}