Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working on dockerization automatic deployment #44

Merged
merged 17 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**/.husky
**/.idea
**/.vscode
**/node_modules
**/.env
.dockerignore
**/.gitignore
**/*.env
**/.git
**/.gitmodules
**/.github
**/Dockerfile
**/docker-compose.yml
48 changes: 48 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#Location: .github/workflows/custom_config.yml

name: Build and push containers to Github Container Registry and apply rolling update
on:
push:
branches:
- develop

jobs:
build:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Getting image tag
uses: actions/github-script@v6
id: get_image_tag
with:
result-encoding: string
script: |
return context.payload.ref.replace(/.*\//, '');

- name: Create and push docker image
run: |
# login to ghcr.io
docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}

# build with full metadata
docker build \
--label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \
--label "org.opencontainers.image.description=Deposit payment example dockerized app" \
--label "org.opencontainers.image.licenses=MIT" \
-t ghcr.io/golemfactory/deposit_example:${{ steps.get_image_tag.outputs.result }} \
--build-arg BACKEND_URL=https://deposit.dev.golem.network \
.

# push one image with two tags into repository
docker push --all-tags ghcr.io/golemfactory/deposit_example

- name: Notify Develop Branch
continue-on-error: true
if: ${{ steps.get_image_tag.outputs.result == 'develop' }}
run: curl http://deposit.dev.golem.network:5000/release/pull
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build docker files

on:
push:

permissions:
packages: write
contents: write

jobs:

build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Create and push docker image
run: |
# login to ghcr.io
docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}

# build with full metadata
docker build \
--label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \
--label "org.opencontainers.image.description=Deposit payment example dockerized app" \
--label "org.opencontainers.image.licenses=MIT" \
-t ghcr.io/golemfactory/deposit_example:latest \
--build-arg BACKEND_URL=http://deposit.dev.golem.network:5174 \
.

# push one image with two tags into repository
docker push --all-tags ghcr.io/golemfactory/deposit_example

- name: Notify Develop Server
continue-on-error: true
if: ${{ steps.get_image_tag.outputs.result == 'develop' }}
run: curl http://deposit.dev.golem.network.:5000/release/pull
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
backend/.env.local
temp
.env
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[submodule "golem-js"]
path = golem-js
url = git@github.com:golemfactory/golem-js.git
url = https://github.com/golemfactory/golem-js.git
branch = pociej/deposit
[submodule "ya-ts-client"]
path = ya-ts-client
url = git@github.com:golemfactory/ya-ts-client.git
url = https://github.com/golemfactory/ya-ts-client.git
branch = pociej/deposit
[submodule "golem-sdk-task-executor"]
path = golem-sdk-task-executor
url = git@github.com:golemfactory/golem-sdk-task-executor.git
url = https://github.com/golemfactory/golem-sdk-task-executor.git
branch = pociej/deposit

28 changes: 0 additions & 28 deletions .husky/commit-msg

This file was deleted.

1 change: 0 additions & 1 deletion .husky/pre-commit

This file was deleted.

18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG BACKEND_URL=http://127.0.0.1:5174

FROM node:20-bullseye

RUN apt-get update && apt-get install -y vim

RUN npm install -g pnpm serve pm2
# Set the working directory
WORKDIR /app
COPY . /app

ARG BACKEND_URL
ENV VITE_BACKEND_URL=$BACKEND_URL
RUN echo "The backend URL is $BACKEND_URL"
RUN mkdir /app/temp

RUN pnpm install
RUN pnpm build:all
5 changes: 3 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "backend",
"name": "deposit:backend",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"dev": "concurrently -k \"tsc -w\" \"nodemon --env-file=.env.local ./dist/index.js\"",
"build": "tsc"
"build": "tsc",
"start": "pm2 start ./dist/index.js"
},
"type": "module",
"keywords": [],
Expand Down
4 changes: 3 additions & 1 deletion backend/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ container.register({
container.register({
db: awilix
.asFunction((connectionString) => {
return mongoose.connect(connectionString);
return mongoose.connect(connectionString, {
dbName: process.env.DB_NAME,
});
})
.singleton(),
});
Expand Down
7 changes: 7 additions & 0 deletions backend/src/services/user/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IUserService, IUser } from "./types.js";
import { userModel } from "./model.js";
import { v4 as uuidv4 } from "uuid";
import mongoose from "mongoose";

const randomNonce = () => {
return Math.floor(Math.random() * 10000000);
Expand All @@ -9,6 +10,11 @@ const randomNonce = () => {
export const userService: IUserService = {
registerUser: async (walletAddress: string) => {
const user = await userModel.findOne({ walletAddress });
console.log("#########################");
console.log("registering user");
console.log("model", mongoose.connection);
console.log("user", user);
console.log("#########################");
if (user !== null) {
return user;
}
Expand All @@ -17,6 +23,7 @@ export const userService: IUserService = {
walletAddress,
nonce: randomNonce(),
id: uuidv4(),
deposits: [],
},
{
_id: true,
Expand Down
84 changes: 84 additions & 0 deletions deploy/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
services:
# application service
backend:
image: ghcr.io/golemfactory/deposit_example:${DEPLOY_BRANCH:-latest}
command: pnpm run start
working_dir: /app/backend
environment:
- YAGNA_APPKEY=66667777888
- YAGNA_API_URL=http://yagna:7465
- JWT_SECRET=dup]a
- JWT_TOKEN_EXPIRATION=1d
- JWT_REFRESH_TOKEN_EXPIRATION=7d
- JWT_ISSUER=golem.network
- MONGO_URI=mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017/
- HOST=0.0.0.0
- PORT=5174
- DB_NAME=depositDB
- DEPOSIT_CONTRACT_ADDRESS=0xb9919c8D8D384d93C195503064A3b303Ea8Fdbaa
ports:
- "5174:5174"
depends_on:
- yagna
- mongo

frontend:
image: ghcr.io/golemfactory/deposit_example:${DEPLOY_BRANCH:-latest}
command: pnpm run start
working_dir: /app/frontend
environment:
- YAGNA_APPKEY=66667777888
- YAGNA_API_URL=http://yagna:7465
- JWT_SECRET=dup]a
- JWT_TOKEN_EXPIRATION=1d
- JWT_REFRESH_TOKEN_EXPIRATION=7d
- JWT_ISSUER=golem.network
- MONGO_URI=mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017/
- HOST=0.0.0.0
- PORT=5174
- DB_NAME=depositDB
- DEPOSIT_CONTRACT_ADDRESS=0xb9919c8D8D384d93C195503064A3b303Ea8Fdbaa
ports:
- "3000:3000"
depends_on:
- backend
# yagna service
yagna:
build:
context: ../yagna
dockerfile: Dockerfile
args:
- YAGNA_VERSION=pre-rel-v0.15.0-deposits-beta1
command: yagna service run
environment:
- YAGNA_AUTOCONF_APPKEY=66667777888
- YAGNA_AUTOCONF_ID_SECRET=0000000000000000000000000000000000000000000000000000000222222222
- YAGNA_API_URL=http://0.0.0.0:7465
- SUBNET=change_me
- YA_NET_BIND_URL=udp://0.0.0.0:0
# mongo service
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=project
ports:
- "27017:27017"

# mongo express service
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
depends_on:
- mongo
ports:
- "8888:8081"
18 changes: 18 additions & 0 deletions deploy/npm_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require('express');
const { spawn } = require('child_process');

const app = express();

app.get('/release/pull', (req, res) => {
const deployProcess = spawn('/bin/bash', ['-c', './script.sh'],
{
cwd: process.cwd(),
stdio: "inherit"
}
);
res.send('Hello World');
});

const server = app.listen(5000, '0.0.0.0', () => {
console.log('Server running');
});
10 changes: 10 additions & 0 deletions deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "deposit-payments-deploy-updater",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"start": "node npm_server.js"
}
}
8 changes: 8 additions & 0 deletions deploy/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
for i in {1..10}
do
git pull
docker-compose pull
# docker-compose config
docker-compose up -d
sleep 10
done
Loading
Loading