Skip to content

Commit

Permalink
增加 github action
Browse files Browse the repository at this point in the history
  • Loading branch information
pakizheng committed Jan 11, 2024
1 parent 1021104 commit ccad5a2
Show file tree
Hide file tree
Showing 44 changed files with 160 additions and 25 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/docker_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Build and Deploy Docker Image

on:
push:
branches:
- master # 根据需要调整分支名称

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/im-web:latest
build-args: |
SECRET_ID=${{ secrets.SECRET_ID }}
SECRET_KEY=${{ secrets.SECRET_KEY }}
DB_NAME=${{ secrets.DB_NAME }}
DB_USER_NAME=${{ secrets.DB_USER_NAME }}
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
COS_BUCKET=${{ secrets.COS_BUCKET }}
COS_REGION=${{ secrets.COS_REGION }}
- name: Deploy to remote server
env:
SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }}
run: |
sudo apt-get install -y sshpass
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no root@${{ secrets.SERVER_ID }} "
docker stop im-web
docker rm im-web
echo '${{ secrets.DOCKERHUB_TOKEN }}' | docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
docker pull ${{ secrets.DOCKER_USER_NAME }}/im-web:latest
docker run -d -p 3000:3000 --name im-web ${{ secrets.DOCKER_USER_NAME }}/im-web:latest
"
72 changes: 72 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# # syntax=docker/dockerfile:1

# # Comments are provided throughout this file to help you get started.
# # If you need more help, visit the Dockerfile reference guide at
# # https://docs.docker.com/go/dockerfile-reference/

# ARG NODE_VERSION=18.15.0

# FROM node:${NODE_VERSION}-alpine

# # Use production node environment by default.
# ENV NODE_ENV production


# WORKDIR .

# CMD npm install

# # Download dependencies as a separate step to take advantage of Docker's caching.
# # Leverage a cache mount to /root/.npm to speed up subsequent builds.
# # Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
# # into this layer.
# # RUN --mount=type=bind,source=package.json,target=package.json \
# # --mount=type=bind,source=package-lock.json,target=package-lock.json \
# # --mount=type=cache,target=/root/.npm \
# # npm ci --omit=dev

# # Run the application as a non-root user.
# USER node

# # Copy the rest of the source files into the image.
# # COPY . .

# # Expose the port that the application listens on.
# EXPOSE 8000

# # Run the application.
# CMD npm run dev

FROM node:18.15.0

EXPOSE 8001

# 先安装npm依赖,因为源代码变更很频繁
COPY package.json /
# COPY package-lock.json /
RUN npm install
RUN npm run build

# 安装 PM2
RUN npm install pm2 -g

COPY . /

# 设置环境变量
ARG SECRET_ID
ARG SECRET_KEY
ARG DB_NAME
ARG DB_USER_NAME
ARG DB_PASSWORD
ARG COS_BUCKET
ARG COS_REGION
ENV SECRET_ID=$SECRET_ID
ENV SECRET_KEY=$SECRET_KEY
ENV DB_NAME=$DB_NAME
ENV DB_USER_NAME=$DB_USER_NAME
ENV DB_PASSWORD=$DB_PASSWORD
ENV COS_BUCKET=$COS_BUCKET
ENV COS_REGION=$COS_REGION

#CMD ["node", "main.js"]
CMD ["pm2-runtime", "./ecosystem.config.js", "--env production"]
18 changes: 18 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
apps: [
{
name: 'im',
script: './dist/main.js',
env_production: {
NODE_ENV: 'production',
SECRET_ID: process.env.SECRET_ID,
SECRET_KEY: process.env.SECRET_KEY,
DB_NAME: process.env.DB_NAME,
DB_USER_NAME: process.env.DB_USER_NAME,
DB_PASSWORD: process.env.DB_PASSWORD,
COS_BUCKET: process.env.COS_BUCKET,
COS_REGION: process.env.COS_REGION,
},
},
],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "SecretId=AKID88FDHNg7i3Smz2vt17A7un0MuWzZ2Pci SecretKey=9Ro6Id4QK0ayQ2YyFJAsNedp46uTzWJE nest start --watch",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
Expand Down
3 changes: 1 addition & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Controller, Get, Post, UseInterceptors } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Public } from './decorators/public';
// import { CacheInterceptor } from '@nestjs/cache-manager';

@Controller()
// @UseInterceptors(CacheInterceptor)
export class AppController {
constructor(private readonly appService: AppService) {}

Expand Down
11 changes: 3 additions & 8 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { Inject, Module } from '@nestjs/common';
// import {
// CACHE_MANAGER,
// CacheInterceptor,
// CacheModule,
// } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { Module } from '@nestjs/common';
import { SequelizeModule } from '@nestjs/sequelize';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
Expand Down Expand Up @@ -75,4 +69,5 @@ const databaseModule = SequelizeModule.forRoot(databaseConfig);
},
],
})
export class AppModule {}
export class AppModule {
}
19 changes: 9 additions & 10 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { SequelizeModuleOptions } from '@nestjs/sequelize';
// 数据库配置
export const database: SequelizeModuleOptions = {
dialect: 'mysql',
host: '127.0.0.1',
host: process.env.DB_IP || '127.0.0.1',
port: 3306,
username: 'root',
password: '123456789',
database: 'db_im_web',
username: process.env.DB_USER_NAME || 'root',
password: process.env.DB_PASSWORD || '123456789',
database: process.env.DB_NAME || 'db_im_web',
synchronize: true,
define: {
timestamps: true,
Expand All @@ -17,14 +17,13 @@ export const database: SequelizeModuleOptions = {

// cos 配置
export const cosConfig = {
SecretId: process.env.SecretId || '',
SecretKey: process.env.SecretKey || '',
SecretId: process.env.SECRET_ID || '',
SecretKey: process.env.SECRET_KEY || '',
};

// cos 桶配置
export const cosBucketConfig = {
Bucket: 'im-web-1323590293',
Region: 'ap-guangzhou',
Bucket: process.env.COS_BUCKET || '',
Region: process.env.COS_REGION || '',
};

export const saltRounds = process.env.SaltRounds || 8;
export const saltRounds = 8;
5 changes: 5 additions & 0 deletions src/gateways/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export class ChatGateway
return result;
}

@SubscribeMessage('connect')
handleConnection(client: Socket) {
console.log('paki connect -----------------------------------------');
const userId = client.handshake.auth.user_id;
if (userId) {
client.join(userId);
Expand Down Expand Up @@ -290,6 +292,9 @@ export class ChatGateway

@SubscribeMessage(SOCKET_EVENT_SEND_GROUP_MESSAGE)
async sendGroupMessage(client: Socket, payload: GroupSendMessagePayload) {
console.log(
'paki SOCKET_EVENT_SEND_GROUP_MESSAGE ----------------------------------------',
);
const {
from,
to,
Expand Down
Binary file removed src/images/08DLwMWwG.webp
Binary file not shown.
Binary file removed src/images/08DLwvooK.webp
Binary file not shown.
Binary file removed src/images/105723aaca1058688279a821e76ec3726a.png
Binary file not shown.
Binary file removed src/images/36VX8wL8A.webp
Binary file not shown.
Binary file removed src/images/36Vwzp5JA.webp
Binary file not shown.
Binary file removed src/images/46GL1172V.webp
Binary file not shown.
Binary file removed src/images/69de99b599b026ad2753c67f01c447e5.png
Binary file not shown.
Binary file removed src/images/6WK9nXvOl.webp
Binary file not shown.
Binary file removed src/images/79VKP4RnO.webp
Binary file not shown.
Binary file removed src/images/84d5b1003997246dc715e8ae9ce4526a5.jpeg
Binary file not shown.
Binary file removed src/images/99YgkJq84.webp
Binary file not shown.
Binary file removed src/images/Br93nLkoo.webp
Binary file not shown.
Binary file removed src/images/Br9p6KEGx.webp
Binary file not shown.
Binary file removed src/images/LZWZnqEjWundefined.webp
Binary file not shown.
Binary file removed src/images/PjWjyJnB6.webp
Binary file not shown.
Binary file removed src/images/RljlwpPwK.webp
Binary file not shown.
Binary file removed src/images/WPoPm51Vn.webp
Binary file not shown.
Binary file removed src/images/avatar.jpg
Binary file not shown.
Binary file removed src/images/cc7a6c8f366fa1007ff165e1032f5fbe5.jpeg
Binary file not shown.
Binary file removed src/images/d8d2bc10fa84a2a94108ff7c0f1838899.png
Binary file not shown.
Binary file removed src/images/e0be80cef9101846d140875aa98035f4f.jpeg
Binary file not shown.
Binary file removed src/images/e5cf1098237e838a693b6105b3110ae3452.jpeg
Binary file not shown.
Binary file removed src/images/f8c5f8ca9ca77e9f5a57c4b458a72afb.png
Binary file not shown.
Binary file removed src/images/k2X7YgnmY.webp
Binary file not shown.
Binary file removed src/images/lRERDqzZrundefined.webp
Binary file not shown.
Binary file removed src/images/mqN9EQr7G.webp
Binary file not shown.
Binary file removed src/images/nR6OLOWNR.webp
Binary file not shown.
Binary file removed src/images/nR6kyz7KP.webp
Binary file not shown.
Binary file removed src/images/oZXKyNYqL.webp
Binary file not shown.
Binary file removed src/images/qQLZOlDOG.webp
Binary file not shown.
Binary file removed src/images/qQLz2Y69G.webp
Binary file not shown.
Binary file removed src/images/r2V2VgZP4.webp
Binary file not shown.
Binary file removed src/images/r2V9jDnvK.webp
Binary file not shown.
Binary file removed src/images/r2V9jQG12.webp
Binary file not shown.
Binary file removed src/images/w074JJrZ8.webp
Binary file not shown.
4 changes: 0 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NestFactory } from '@nestjs/core';
import cookieParser from 'cookie-parser';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';
import { RedisIoAdapter } from './gateways/redis-io-adapter';

Expand All @@ -16,9 +15,6 @@ async function bootstrap() {

app.useWebSocketAdapter(redisIoAdapter);
app.use(cookieParser());
app.useStaticAssets(join(__dirname, 'images'), {
prefix: '/images',
});
await app.listen(3000);
}
bootstrap();

0 comments on commit ccad5a2

Please sign in to comment.