-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
name: Deploy to Staging | ||
|
||
on: | ||
push: | ||
branches: [ development ] | ||
|
||
jobs: | ||
deploy-staging: | ||
runs-on: ubuntu-latest | ||
if: github.ref == 'refs/heads/development' | ||
steps: | ||
- uses: satackey/[email protected] | ||
# Ignore the failure of a step and avoid terminating the job. | ||
continue-on-error: true | ||
|
||
- name: Save my code in site directory | ||
uses: actions/checkout@v3 | ||
with: | ||
token: ${{ secrets.GIT_PAT }} #Your GitHub access token | ||
ref: development | ||
|
||
- name: Set my ssh key and ssh-agent | ||
uses: webfactory/[email protected] | ||
env: | ||
ACTIONS_ALLOW_UNSECURE_COMMANDS: true | ||
with: | ||
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 18 | ||
|
||
- name: Install dependencies | ||
run: yarn install | ||
working-directory: src | ||
|
||
# Add support for more platforms with QEMU (optional) | ||
# https://github.com/docker/setup-qemu-action | ||
- | ||
name: Set up QEMU | ||
uses: docker/setup-qemu-action@v2 | ||
|
||
# install Docker and enable docker service | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Run test | ||
run: docker-compose run addressbook npm test | ||
working-directory: src | ||
|
||
- name: Deploy Staging using ansible | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.STAGING_SERVER }} | ||
username: ${{ secrets.STAGING_USERNAME }} | ||
key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
script: | | ||
cd /var/www/addressbook | ||
git pull | ||
docker-compose down | ||
docker-compose up -d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Dockerfile | ||
|
||
FROM registry.semaphoreci.com/node:18 | ||
RUN mkdir -p /opt/app | ||
WORKDIR /opt/app | ||
COPY src/package.json src/package-lock.json . | ||
RUN npm install | ||
COPY src/ . | ||
EXPOSE 3000 | ||
CMD [ "npm", "start"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# docker-compose.yml | ||
|
||
version: "3.9" | ||
services: | ||
postgres: | ||
image: postgres | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
ports: | ||
- '5432:5432' | ||
|
||
addressbook: | ||
build: | ||
context: . | ||
dockerfile: ./Dockerfile | ||
command: "npm start" | ||
volumes: | ||
- /opt/app/node_modules | ||
- ./src:/opt/app | ||
environment: | ||
DB_SCHEMA: postgres | ||
DB_USER: postgres | ||
DB_PASSWORD: postgres | ||
DB_HOST: postgres | ||
depends_on: | ||
- postgres | ||
ports: | ||
- '3000:3000' | ||
|
||
volumes: | ||
addressbook-db: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var logger = require('morgan'); | ||
|
||
var usersRouter = require('./routes/users'); | ||
var indexRouter = require('./routes/index'); | ||
// add the following line near the indexRouter | ||
var personsRouter = require('./routes/persons'); | ||
|
||
var app = express(); | ||
|
||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
app.use('/', indexRouter); | ||
app.use('/users', usersRouter); | ||
app.use('/persons', personsRouter); | ||
|
||
module.exports = app; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// bin/migrate.js | ||
|
||
var db = require('../database.js'); | ||
db.sequelize.sync(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const app = require('../app'); // Update the path based on your project structure | ||
const debug = require('debug')('your-app-name:server'); | ||
const http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
const port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
const server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
const port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
const addr = server.address(); | ||
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// database.js | ||
|
||
const Sequelize = require('sequelize'); | ||
const sequelize = new Sequelize(process.env.DB_SCHEMA || 'postgres', | ||
process.env.DB_USER || 'postgres', | ||
process.env.DB_PASSWORD || 'postgres', | ||
{ | ||
host: process.env.DB_HOST || 'localhost', | ||
port: process.env.DB_PORT || 5432, | ||
dialect: 'postgres', | ||
dialectOptions: { | ||
ssl: process.env.DB_SSL == "true" | ||
} | ||
}); | ||
const Person = sequelize.define('Person', { | ||
firstName: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING, | ||
allowNull: true | ||
}, | ||
}); | ||
module.exports = { | ||
sequelize: sequelize, | ||
Person: Person | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const db = require('./database'); | ||
|
||
beforeAll(async () => { | ||
await db.sequelize.sync({ force: true }); | ||
}); | ||
|
||
test('create person', async () => { | ||
expect.assertions(1); | ||
const person = await db.Person.create({ | ||
id: 1, | ||
firstName: 'Bobbie', | ||
lastName: 'Draper' | ||
}); | ||
expect(person.id).toEqual(1); | ||
}); | ||
|
||
test('get person', async () => { | ||
expect.assertions(2); | ||
const person = await db.Person.findByPk(1); | ||
expect(person.firstName).toEqual('Bobbie'); | ||
expect(person.lastName).toEqual('Draper'); | ||
}); | ||
|
||
test('delete person', async () => { | ||
expect.assertions(1); | ||
await db.Person.destroy({ | ||
where: { | ||
id: 1 | ||
} | ||
}); | ||
const person = await db.Person.findByPk(1); | ||
expect(person).toBeNull(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.sequelize.close(); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.