From 48ae45141611c6320c24eb63033c0166bead47f0 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Fri, 8 Jul 2022 23:05:36 +0200 Subject: [PATCH] setup docker and jenkins --- .gitignore | 2 -- Dockerfile | 10 ++++++ Jenkinsfile | 88 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 19 ++++++++++ package-lock.json | 16 +++++++++ package.json | 2 ++ src/index.js | 3 ++ src/server.js | 13 +++++++ 8 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100644 docker-compose.yml create mode 100644 src/server.js diff --git a/.gitignore b/.gitignore index 44b4837..03b420a 100644 --- a/.gitignore +++ b/.gitignore @@ -79,7 +79,6 @@ web_modules/ .env.development.local .env.test.local .env.production.local -.env.local # parcel-bundler cache (https://parceljs.org/) .cache @@ -104,7 +103,6 @@ dist # vuepress v2.x temp and cache directory .temp -.cache # Docusaurus cache and generated files .docusaurus diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..095c424 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:16.16.0-alpine3.16 + +COPY src/* . +COPY package.json package.json + +EXPOSE 8080 + +RUN npm install + +CMD ["node"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..2c4987a --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,88 @@ +pipeline { + agent any + + environment { + NEXUS = credentials('jenkins_nexus') + NCLAZZ = credentials('nclazz_api_token') + + DOCKER_REGISTRY = "docker.nclazz.de" + DOCKER_GROUP = 'nclazz-bots' + DOCKER_IMAGE = 'gitea-bot' + DOCKER_TAG = "${ BRANCH_NAME == "master" ? "latest" : "develop" }" + DOCKER_VERSION = "1.0.0" + + DEPLOY_ENV = "production" + } + + options { + timeout(time: 30, unit: "MINUTES") + } + + stages { + stage('Docker Build') { + agent { label 'docker' } + steps { + script { + docker.withRegistry("https://$DOCKER_REGISTRY", "jenkins_nexus") { + def apiImage = docker.build("$DOCKER_GROUP/$DOCKER_IMAGE") + apiImage.push("${env.DOCKER_VERSION}") + apiImage.push("${env.DOCKER_TAG}") + } + } + } + } + stage('Run Deployment') { + agent { label 'provision' } + when { + branch 'master' + } + steps { + deployToSwarm( + name: 'nclazz-gitea-bot', + file: "${env.WORKSPACE}/docker-compose.yml", + forceUpdate: true, + askApproval: false, + slackChannel: 'deployment', + env: [ + PORT: 7007, + VERSION: env.DOCKER_VERSION, + AUTH_TOKEN: env.NCLAZZ + ] + ) + exposeService( + name: 'nclazz-gitea-bot', + fqdn: 'gitea-bot.nclazz.de', + description: 'Gitea bot', + location: '/', + backendPort: 7007 + ) + } + } + } + post { + always { + cleanWs() + } + failure { + slackSend( + channel: "notifications", + color: "danger", + message: "There is a *build failure* in ${env.JOB_NAME}.\nBuild: ${env.RUN_DISPLAY_URL} " + ) + } + unstable { + slackSend( + channel: "notifications", + color: "warning", + message: "Some tests have failed in ${env.JOB_NAME}.\nBuild: ${env.RUN_DISPLAY_URL} " + ) + } + fixed { + slackSend( + channel: "notifications", + color: "good", + message: "The build ${env.JOB_NAME} completed successfully and is back to normal.\nBuild: ${env.RUN_DISPLAY_URL} " + ) + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3e83d6f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.8' +services: + service: + image: docker.nclazz.de/nclazz-bots/gitea-bot:$VERSION + environment: + SPRING_MAIL_USERNAME: "${MAIL_USERNAME}" + SPRING_MAIL_PASSWORD: "${MAIL_PASSWORD}" + SPRING_DATASOURCE_USERNAME: "${DB_USERNAME}" + SPRING_DATASOURCE_PASSWORD: "${DB_PASSWORD}" + SPRING_PROFILES_ACTIVE: swarm + NCLAZZ_AUTH_TOKEN: "${AUTH_TOKEN}" + deploy: + replicas: 1 + placement: + max_replicas_per_node: 1 + update_config: + order: start-first + ports: + - "${PORT}:8080" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ee7af94..3419943 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,8 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "body-parser": "^1.20.0", + "dotenv": "^16.0.1", "express": "^4.18.1" } }, @@ -141,6 +143,15 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "16.0.1", + "resolved": "https://nexus.nclazz.de/repository/npm_public/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://nexus.nclazz.de/repository/npm_public/ee-first/-/ee-first-1.1.1.tgz", @@ -735,6 +746,11 @@ "resolved": "https://nexus.nclazz.de/repository/npm_public/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" }, + "dotenv": { + "version": "16.0.1", + "resolved": "https://nexus.nclazz.de/repository/npm_public/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==" + }, "ee-first": { "version": "1.1.1", "resolved": "https://nexus.nclazz.de/repository/npm_public/ee-first/-/ee-first-1.1.1.tgz", diff --git a/package.json b/package.json index 8893f59..0c205bb 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "author": "Niclas Thobaben", "license": "MIT", "dependencies": { + "body-parser": "^1.20.0", + "dotenv": "^16.0.1", "express": "^4.18.1" } } diff --git a/src/index.js b/src/index.js index e69de29..0b01772 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,3 @@ +require('dotenv').config() + +require('./server') \ No newline at end of file diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..404d4b1 --- /dev/null +++ b/src/server.js @@ -0,0 +1,13 @@ +const express = require('express') +const bodyParser = require('body-parser') + +console.log('Initialize express server') + +const app = express() + +app.use(bodyParser.json()) + +const port = process.env.SERVER_PORT || 8080 + +app.listen(port) +console.log(`Started express server on port ${port}`)