From 7656f048523fef69c73e6f8615bb63731444773b Mon Sep 17 00:00:00 2001 From: Grigory Date: Wed, 11 Dec 2024 08:30:37 +0500 Subject: [PATCH] ci(image-search-server): use docker --- .dockerignore | 25 ++++++++++++ .github/workflows/deploy_server.yml | 63 +++++++++++------------------ .gitignore | 1 + .vscode/extensions.json | 3 +- Dockerfile | 18 +++++++++ docker-compose.yml | 11 +++++ 6 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f4dd915 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +.cache +.github +.husky +.vscode +node_modules +commitlintrc.json +.dockerignore +.editorconfig +.gitattributes +.npmrc +Dockerfile* +docker-compose* +LICENSE + +**/eslint.config.* +**/.prettier* +**/.gitignore +**/*.md +**/.env* + +# mtcute +client.session* + +packages/image-search-bot +!packages/image-search-bot/package.json diff --git a/.github/workflows/deploy_server.yml b/.github/workflows/deploy_server.yml index b4a9519..c2184f8 100644 --- a/.github/workflows/deploy_server.yml +++ b/.github/workflows/deploy_server.yml @@ -10,13 +10,13 @@ on: jobs: check-credentials: - name: Check credentials + name: Check for SSH credentials runs-on: ubuntu-latest environment: production outputs: is_set: ${{ steps.check.outputs.is_set }} steps: - - name: Check SSH credentials + - name: Perform check id: check run: | if [[ '${{ secrets.SSH_KEY }}' && '${{ secrets.SSH_HOST }}' && '${{ secrets.SSH_USERNAME }}' ]]; then @@ -35,18 +35,22 @@ jobs: with: cwd: packages/image-search-server - # blocker for commented: https://github.com/oven-sh/bun/issues/6567 deploy: name: Deploy server runs-on: ubuntu-latest needs: check-package environment: production - defaults: - run: - working-directory: packages/image-search-server steps: - # - name: Checkout repo - # uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + - name: Checkout repo + uses: actions/checkout@cbb722410c2e876e24abbe8de2cc27693e501dcb + + - name: Build image + run: | + docker build --platform linux/arm64 --pull --rm -f 'Dockerfile' -t image-search-server:latest '.' + docker save -o image-search-server.tar image-search-server:latest + + - name: Package image + run: gzip image-search-server.tar - name: Setup SSH run: | @@ -54,29 +58,11 @@ jobs: echo '${{ secrets.SSH_KEY }}' > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa echo 'StrictHostKeyChecking no' >> ~/.ssh/config - working-directory: . - - # - name: Setup Bun - # uses: oven-sh/setup-bun@f4d14e03ff726c06358e5557344e1da148b56cf7 - - # - name: Install dependencies - # run: bun install --frozen-lockfile --production - - # - name: Build - # run: bun run build - # - name: Deploy - # run: | - # scp dist/image-search-server '${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:~/image-search-server/image-search-server_new' - # ssh '${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}' /bin/bash << EOF - # pkill -f image-search-server || true - # cd ~/image-search-server - # mv image-search-server_new image-search-server - # nohup ./image-search-server > /dev/null 2>&1 & - # EOF + - name: Deploy image + run: scp image-search-server.tar.gz docker-compose.yml ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:/tmp/ - # temporary solution - - name: Deploy + - name: Run container uses: appleboy/ssh-action@66aa4d343bf909ac4fa0ac52f4e62a9adc354c95 with: host: ${{ secrets.SSH_HOST }} @@ -84,14 +70,13 @@ jobs: key: ${{ secrets.SSH_KEY }} script: | cd - repository_name="$(basename '${{ github.repository }}')" - if [[ ! -d "$repository_name" ]]; then - git clone '${{ github.server_url }}/${{ github.repository }}' + if [[ ! -d "image-search-server" ]]; then + echo "Create the image-search-server folder with the .env file in it first." >&2 + exit 1 fi - cd "$repository_name" - pkill -f 'bun run start$' - git fetch --all - git reset --hard origin/master - cd packages/image-search-server - bun install --frozen-lockfile --production - nohup bun run start > /dev/null 2>&1 & + cd image-search-server + docker compose down + docker rmi image-search-server:latest + mv -f /tmp/image-search-server.tar.gz /tmp/docker-compose.yml ./ + docker load -i image-search-server.tar.gz + docker compose up -d diff --git a/.gitignore b/.gitignore index 804a39a..becd9d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .env* .cache/ +image-search-server.tar* diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 9e3b828..b192955 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -4,6 +4,7 @@ "dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "oven.bun-vscode", - "github.vscode-github-actions" + "github.vscode-github-actions", + "ms-azuretools.vscode-docker" ] } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1dc98dc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# syntax=docker/dockerfile:1.7-labs + +FROM oven/bun:1 AS base + +FROM base AS install +WORKDIR /temp +COPY --exclude=**/src . . +RUN cd packages/image-search-server && bun install --frozen-lockfile --production + +FROM base AS release +WORKDIR /server +COPY --from=install /temp/node_modules node_modules +COPY . . +RUN cd /server/packages/image-search-server && bun install --frozen-lockfile --production + +USER bun +EXPOSE 3000/tcp +ENTRYPOINT [ "bun", "run", "--cwd", "packages/image-search-server", "start" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..959d573 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + server: + image: image-search-server:latest + container_name: image-search-server + restart: unless-stopped + tty: true + volumes: + - ./keys:/packages/image-search-server/keys + env_file: .env + ports: + - 3000:3000