diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..243e9e3cc --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,57 @@ +pipeline { + agent { label 'server6-solihin' } + + stages { + stage('Pull SCM') { + steps { + git branch: 'main', url: 'https://github.com/15ahmadsolihin/simple-apps.git' + } + } + + stage('Build') { + steps { + sh''' + cd app + npm install + ''' + } + } + + stage('Testing') { + steps { + sh''' + cd app + APP_PORT=50001 npm test + APP_PORT=50001 npm run test:coverage + ''' + } + } + + // stage('Code Review') { + // steps { + // sh''' + // cd app + // sonar-scanner \ + // -Dsonar.projectKey=Simple-Apps \ + // -Dsonar.sources=. \ + // -Dsonar.host.url=http://172.23.x.x:9000 \ + // -Dsonar.login=token-sonar + // ''' + // } + // } + + stage('Deploy') { + steps { + sh''' + docker compose up --build -d + ''' + } + } + + stage('Backup') { + steps { + sh 'docker compose push' + } + } + } +} \ No newline at end of file diff --git a/app/.env b/app/.env new file mode 100644 index 000000000..b65bc8e83 --- /dev/null +++ b/app/.env @@ -0,0 +1,5 @@ +DB_NAME=training +DB_HOST=172.23.7.116 +DB_USER=peserta +DB_PASS=password +APP_PORT=3000 diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 000000000..6baccdd18 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,7 @@ +FROM node:20-slim +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +EXPOSE 3000 +CMD ["npm", "start"] \ No newline at end of file diff --git a/app/README.md b/app/README.md new file mode 100644 index 000000000..92fd0a466 --- /dev/null +++ b/app/README.md @@ -0,0 +1,13 @@ +# Create env variable + +``` +vim .env +``` + +``` +DB_NAME=training +DB_HOST=ipaddress +DB_USER=peserta +DB_PASS=password +APP_PORT=3000 +``` diff --git a/app.js b/app/app.js similarity index 84% rename from app.js rename to app/app.js index 0b45c4027..bbf49510e 100644 --- a/app.js +++ b/app/app.js @@ -1,8 +1,8 @@ const express = require('express') const mysql = require('mysql'); const app = express() -const PORT = process.env.PORT || 3000; const path = require('path') +require('dotenv').config(); // Import Middleware const logger = require('./middleware/logger') @@ -31,8 +31,8 @@ app.get('/users', (req, res, next) => { }) }); -app.listen(PORT, () => { - console.log(`Example app listening on port ${PORT}`) +app.listen(process.env.APP_PORT, () => { + console.log(`Example app listening on port ${process.env.APP_PORT}`) }) module.exports = app \ No newline at end of file diff --git a/database/training.sql b/app/database/training.sql similarity index 100% rename from database/training.sql rename to app/database/training.sql diff --git a/app/middleware/db_connect.js b/app/middleware/db_connect.js new file mode 100644 index 000000000..0e66d5ba1 --- /dev/null +++ b/app/middleware/db_connect.js @@ -0,0 +1,12 @@ +const mysql = require('mysql') +require('dotenv').config(); + +// Connect database +const connection = mysql.createConnection({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME +}) + +module.exports = connection \ No newline at end of file diff --git a/middleware/logger.js b/app/middleware/logger.js similarity index 100% rename from middleware/logger.js rename to app/middleware/logger.js diff --git a/package.json b/app/package.json similarity index 83% rename from package.json rename to app/package.json index 0fc600d78..6de9fa897 100644 --- a/package.json +++ b/app/package.json @@ -6,13 +6,12 @@ "scripts": { "dev": "nodemon app", "start": "node app", - "test:unit": "jest testing/app.test", - "test:integration": "jest testing/db.test" + "test": "jest --forceExit testing/", + "test:coverage": "jest --coverage --forceExit testing/" }, "author": "Moch Rafi Riadi", "license": "ISC", "dependencies": { - "chai": "^4.3.7", "dotenv": "^16.0.3", "express": "^4.18.2", "moment": "^2.29.4", @@ -20,6 +19,7 @@ "nodemon": "^2.0.22" }, "devDependencies": { + "chai": "^4.3.7", "babel-jest": "^29.5.0", "jest": "^29.5.0", "supertest": "^6.3.3" diff --git a/public/images/idn-logo.png b/app/public/images/idn-logo.png similarity index 100% rename from public/images/idn-logo.png rename to app/public/images/idn-logo.png diff --git a/app/public/index.html b/app/public/index.html new file mode 100644 index 000000000..c8b2344d8 --- /dev/null +++ b/app/public/index.html @@ -0,0 +1,159 @@ + + + + + + TechConsult | Solusi Transformasi Digital + + + + + + + + +
+
+
+

+ Akselerasi Bisnis Anda dengan Teknologi Tepat. +

+

+ Kami membantu perusahaan melakukan transformasi digital melalui strategi IT yang inovatif, aman, dan terukur. +

+ +
+
+ Cyber Security +
+
+
+ +
+
+

Layanan Kami

+

Solusi IT Komprehensif

+ +
+
+
+ +
+

Cloud Infrastructure

+

Migrasi dan pengelolaan infrastruktur cloud yang efisien dan skalabel untuk bisnis Anda.

+
+ +
+
+ +
+

Cyber Security

+

Lindungi data aset berharga Anda dari ancaman siber dengan sistem keamanan berlapis.

+
+ +
+
+ +
+

Software Development

+

Pengembangan aplikasi kustom yang dirancang khusus untuk memenuhi kebutuhan operasional.

+
+
+
+
+ +
+
+
+
+
+ Team + Meeting +
+
+
+

Mengapa Memilih TechConsult?

+

+ Dengan pengalaman lebih dari 10 tahun, kami memahami bahwa setiap bisnis memiliki tantangan unik. Kami tidak hanya memberikan teknologi, tapi memberikan solusi strategis yang membantu Anda tumbuh. +

+
    +
  • + + Tim Ahli Tersertifikasi Internasional +
  • +
  • + + Dukungan Teknis 24/7 +
  • +
  • + + Pendekatan Berbasis Data & Hasil +
  • +
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/app/public/index2.html b/app/public/index2.html new file mode 100644 index 000000000..8fd99627e --- /dev/null +++ b/app/public/index2.html @@ -0,0 +1 @@ +test branch \ No newline at end of file diff --git a/public/stylesheet/style.css b/app/public/stylesheet/style.css similarity index 100% rename from public/stylesheet/style.css rename to app/public/stylesheet/style.css diff --git a/app/testing/app.test.js b/app/testing/app.test.js new file mode 100644 index 000000000..2f74fbdf1 --- /dev/null +++ b/app/testing/app.test.js @@ -0,0 +1,44 @@ +const request = require('supertest'); +const mysql = require('mysql'); +require('dotenv').config(); + +const connection = require('../middleware/db_connect'); +const app = require('../app'); // Replace with the path to your application file + +describe('Unit Test /', () => { + it('should respond with index.html', async () => { + const response = await request(app).get('/'); + expect(response.status).toBe(200); + }); +}); + +describe('Unit Test /app1', () => { + it('should respond with "Hello App1!"', async () => { + const response = await request(app).get('/app1'); + expect(response.status).toBe(200); + }); + }); + + describe('Unit Test /app2', () => { + it('should respond with "Hello App2!"', async () => { + const response = await request(app).get('/app2'); + expect(response.status).toBe(200); + }); + }); + +describe('Integration Test Connect Database', () => { + beforeEach(() => { + // Set the connection for the application to use + app.set('connection', connection.connect); + }); + + afterEach(() => { + // Close the connection pool after the tests are done + connection.end(); + }); + + it('should respond with users data', async () => { + const response = await request(app).get('/users'); + expect(response.status).toBe(200); + }); +}); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..407fb538b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +name: app + +services: + app: + image: ahmadsolihin/simple-app:1.0 + build: ./app + ports: + - "5000:3000" + volumes: + - vol-upload:/app/public/images/ + +volumes: + vol-upload: \ No newline at end of file diff --git a/middleware/db_connect.js b/middleware/db_connect.js deleted file mode 100644 index 16ed286a2..000000000 --- a/middleware/db_connect.js +++ /dev/null @@ -1,11 +0,0 @@ -const mysql = require('mysql') - -// Connect database -const connection = mysql.createConnection({ - host: '172.17.0.2', - user: 'peserta', - password: 'password', - database: 'training' -}) - -module.exports = connection \ No newline at end of file diff --git a/public/index.html b/public/index.html deleted file mode 100644 index d6785c439..000000000 --- a/public/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - Training DevOps - - - - -

Belajar Devops

- - \ No newline at end of file diff --git a/testing/app.test.js b/testing/app.test.js deleted file mode 100644 index 178167f52..000000000 --- a/testing/app.test.js +++ /dev/null @@ -1,26 +0,0 @@ -const request = require('supertest') -const express = require('express') - -// Import the app file -const app = require('../app') - -describe('Test /', () => { - it('should respond with index.html', async () => { - const response = await request(app).get('/'); - expect(response.status).toBe(200); - }); -}); - -describe('Test /app1', () => { - it('should respond with "Hello App1!"', async () => { - const response = await request(app).get('/'); - expect(response.status).toBe(200); - }); - }); - - describe('Test /app2', () => { - it('should respond with "Hello App2!"', async () => { - const response = await request(app).get('/'); - expect(response.status).toBe(200); - }); - }); \ No newline at end of file diff --git a/testing/db.test.js b/testing/db.test.js deleted file mode 100644 index 8f9936cc2..000000000 --- a/testing/db.test.js +++ /dev/null @@ -1,41 +0,0 @@ -const request = require('supertest'); -const mysql = require('mysql'); -const { expect } = require('chai'); - -const connection = require('../middleware/db_connect'); -const app = require('../app'); // Replace with the path to your application file - -describe('Integration Test', () => { - let connection; - - beforeEach(() => { - // Create a connection pool instead of a single connection - connection = mysql.createPool({ - connectionLimit: 10, // Adjust the limit according to your requirements - host: '172.17.0.2', - user: 'peserta', - password: 'password', - database: 'training' - }); - - // Set the connection for the application to use - app.set('connection', connection); - }); -\ - afterEach(() => { - // Close the connection pool after the tests are done - connection.end(); - }); - - it('should connect to the MySQL database', (done) => { - request(app) - .get('/users') // Replace with the endpoint you want to test - .expect(200) - .end((err, res) => { - expect(err).to.be.null; - expect(res.status).to.equal(200); - // Additional assertions based on your application's response - done(); - }); - }); -}); \ No newline at end of file