From 59652e6936069880950eceb434b7c3d1717814fd Mon Sep 17 00:00:00 2001 From: Mantre Date: Sat, 28 Dec 2024 03:02:08 +0800 Subject: [PATCH] feat: auto deployment (#192) --- .github/workflows/deploy.yml | 29 ++++++ deployment/Dockerfile | 18 ++++ deployment/Makefile | 11 --- deployment/README.md | 26 +++++ deployment/cli/Dockerfile | 19 ---- deployment/deploy.sh | 26 +++++ deployment/discord/Dockerfile.Mainnet | 21 ---- deployment/discord/Dockerfile.Mod | 21 ---- deployment/discord/Dockerfile.Testnet | 21 ---- deployment/docker-compose.yml | 130 ++++++++++--------------- deployment/telegram/Dockerfile.Mainnet | 21 ---- 11 files changed, 150 insertions(+), 193 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 deployment/Dockerfile delete mode 100644 deployment/Makefile create mode 100644 deployment/README.md delete mode 100644 deployment/cli/Dockerfile create mode 100644 deployment/deploy.sh delete mode 100644 deployment/discord/Dockerfile.Mainnet delete mode 100644 deployment/discord/Dockerfile.Mod delete mode 100644 deployment/discord/Dockerfile.Testnet delete mode 100644 deployment/telegram/Dockerfile.Mainnet diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..91221677 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,29 @@ +name: Deploying + +on: + push: + branches: + - main + tags: + - 'v*' + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: SSH into server and deploy Pagu + uses: appleboy/ssh-action@v1.2.0 + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + port: ${{ secrets.SSH_PORT }} + key: ${{ secrets.SSH_KEY }} + script: | + cd source + git pull + export PAGU_DB_ROOT_PASSWORD=${{ secrets.PAGU_DB_ROOT_PASSWORD }} + export PAGU_DB_USER=${{ secrets.PAGU_DB_USER }} + export PAGU_DB_USER_PASSWORD=${{ secrets.PAGU_DB_USER_PASSWORD }} + export PHPMYADMIN_PORT=${{ secrets.PHPMYADMIN_PORT }} + bash ./deployment/deploy.sh diff --git a/deployment/Dockerfile b/deployment/Dockerfile new file mode 100644 index 00000000..c010ace4 --- /dev/null +++ b/deployment/Dockerfile @@ -0,0 +1,18 @@ +# Builder stage +FROM golang:1.23.3-alpine AS builder +RUN apk add --no-cache bash make +WORKDIR /pagu +COPY .. . +RUN make build-discord build-telegram + +# Discord bot +FROM alpine:3.14 AS discord +WORKDIR /bin +COPY --from=builder /pagu/build/pagu-discord . +CMD ["./pagu-discord", "-c", "/pagu/config.yml", "run"] + +# Telegram bot +FROM alpine:3.14 AS telegram +WORKDIR /bin +COPY --from=builder /pagu/build/pagu-telegram . +CMD ["./pagu-telegram", "-c", "/pagu/config.yml", "run"] diff --git a/deployment/Makefile b/deployment/Makefile deleted file mode 100644 index f3858f09..00000000 --- a/deployment/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -build-cli-container: - BUILDKIT_PROGRESS=plain docker compose build pagu-cli - -build-discord-container: - BUILDKIT_PROGRESS=plain docker compose build pagu-discord - -build-cli-telegram: - BUILDKIT_PROGRESS=plain docker compose build pagu-telegram - -force-run: - BUILDKIT_PROGRESS=plain docker compose up -d --force-recreate \ No newline at end of file diff --git a/deployment/README.md b/deployment/README.md new file mode 100644 index 00000000..ecd78873 --- /dev/null +++ b/deployment/README.md @@ -0,0 +1,26 @@ +## Auto-Deployment for Pagu Project + +This project includes an automated deployment process for +both the `stable` and `latest` versions of the Pagu Discord and Telegram bots. + +### Deployment Overview + +The deployment system uses the following mechanisms: + +- **Stable Version**: Activated when a Git tag is pushed to the repository. +- **Latest Version**: Activated when changes are pushed to the `main` branch. + +### How to Create a Tag: + +To create a tag and push it to the repository, follow these steps: + +1. Ensure that the origin is set to the current repository, not your fork. +2. Ensure that the Pagu's [version](../version.go) is updated. +3. Run the following commands: + +```bash +VERSION=0.?.? +git pull origin +git tag -s -a v${VERSION} -m "Version ${VERSION}" +git push origin v${VERSION} +``` diff --git a/deployment/cli/Dockerfile b/deployment/cli/Dockerfile deleted file mode 100644 index c0697229..00000000 --- a/deployment/cli/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM golang:1.22.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN make build-cli - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-cli -COPY --from=builder /pagu/build /pagu-cli - -RUN chmod +x /pagu-cli/pagu-cli - -CMD ["./pagu-cli/pagu-cli", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/deploy.sh b/deployment/deploy.sh new file mode 100644 index 00000000..62b9cf24 --- /dev/null +++ b/deployment/deploy.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e + +DOCKER_TAG="latest" + +TAG=$(git describe --tags --exact-match 2> /dev/null) || echo "" + +if [[ -n "$TAG" ]]; then + DOCKER_TAG="stable" +fi + +echo "Building ${DOCKER_TAG} version" + +docker build -t pagu-discord:${DOCKER_TAG} -f ./deployment/Dockerfile . --target discord +docker build -t pagu-telegram:${DOCKER_TAG} -f ./deployment/Dockerfile . --target telegram + +docker compose -f ./deployment/docker-compose.yml down +docker compose -f ./deployment/docker-compose.yml up -d + +## Some cleanup +echo "Cleanup" + +docker builder prune -f +docker image prune -f +docker container prune -f diff --git a/deployment/discord/Dockerfile.Mainnet b/deployment/discord/Dockerfile.Mainnet deleted file mode 100644 index ac357fe6..00000000 --- a/deployment/discord/Dockerfile.Mainnet +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.23.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN go mod tidy -RUN go mod vendor -RUN make build-discord - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-discord -COPY --from=builder /pagu/build /pagu-discord - -RUN chmod +x /pagu-discord/pagu-discord - -CMD ["./pagu-discord/pagu-discord", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/discord/Dockerfile.Mod b/deployment/discord/Dockerfile.Mod deleted file mode 100644 index 93cada28..00000000 --- a/deployment/discord/Dockerfile.Mod +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.23.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN go mod tidy -RUN go mod vendor -RUN make build-discord - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-discord-mod -COPY --from=builder /pagu/build /pagu-discord-mod - -RUN chmod +x /pagu-discord-mod/pagu-discord - -CMD ["./pagu-discord-mod/pagu-discord", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/discord/Dockerfile.Testnet b/deployment/discord/Dockerfile.Testnet deleted file mode 100644 index 93857a6c..00000000 --- a/deployment/discord/Dockerfile.Testnet +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.23.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN go mod tidy -RUN go mod vendor -RUN make build-discord - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-discord-testnet -COPY --from=builder /pagu/build /pagu-discord-testnet - -RUN chmod +x /pagu-discord-testnet/pagu-discord - -CMD ["./pagu-discord-testnet/pagu-discord", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/docker-compose.yml b/deployment/docker-compose.yml index 1433f496..e3b5850f 100644 --- a/deployment/docker-compose.yml +++ b/deployment/docker-compose.yml @@ -1,115 +1,87 @@ -version: '3' - -networks: - pagu-network: - external: true - services: pagu-db: - image: mysql:5.7.42-debian + image: mysql:8.0.40-debian hostname: pagu-db environment: - MYSQL_ROOT_PASSWORD: ${PAGU_DB_PASSWORD} MYSQL_DATABASE: pagu - expose: - - 4417 - ports: - - "4417:3306" + MYSQL_ROOT_PASSWORD: ${PAGU_DB_ROOT_PASSWORD} + MYSQL_USER: ${PAGU_DB_USER} + MYSQL_PASSWORD: ${PAGU_DB_USER_PASSWORD} container_name: pagu-db - networks: - pagu-network: volumes: - - pagu-db-data:/var/lib/mysql + - ${HOME}/mysql_data:/var/lib/mysql + networks: + - pagu-network + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", pagu-db] + timeout: 5s + retries: 10 + pagu-phpmyadmin: image: phpmyadmin/phpmyadmin environment: PMA_HOST: pagu-db container_name: pagu-phpmyadmin ports: - - "4418:80" - networks: - pagu-network: + - "${PHPMYADMIN_PORT}:80" depends_on: - - pagu-db - pagu-cli: - build: - context: ../. - dockerfile: deployment/cli/Dockerfile - hostname: pagu-cli - container_name: pagu-cli + pagu-db: + condition: service_healthy networks: - pagu-network: - depends_on: - - pagu-db - volumes: - - ../config/config.yml:/config/config.yml - - ../config/wallets/main_wallet:/config/wallets/main_wallet - - ../config/wallets/test_wallet:/config/wallets/test_wallet - pagu-discord: - build: - context: ../. - dockerfile: deployment/discord/Dockerfile.Mainnet - hostname: pagu-discord-mainnet - container_name: pagu-discord + - pagu-network + + pagu-discord-mainnet: + image: pagu-discord:stable + container_name: pagu-discord-mainnet networks: pagu-network: depends_on: - - pagu-db + - pagu-phpmyadmin volumes: - - ../config/config.mainnet.yml:/config/config.yml - - ../config/wallets/main_wallet:/config/wallets/main_wallet - pagu-discord-mod: - build: - context: ../. - dockerfile: deployment/discord/Dockerfile.Mod - hostname: pagu-discord-mod - container_name: pagu-discord-mod + - ${HOME}/pagu-discord-mainnet:/pagu + + pagu-discord-staging: + image: pagu-discord:latest + container_name: pagu-discord-staging networks: pagu-network: depends_on: - - pagu-db + - pagu-phpmyadmin volumes: - - ../config/config.mod.yml:/config/config.yml - - ../config/templates:/config/templates - - ../config/wallets/main_wallet:/config/wallets/main_wallet + - ${HOME}/pagu-discord-staging:/pagu + pagu-discord-testnet: - build: - context: ../. - dockerfile: deployment/discord/Dockerfile.Testnet - hostname: pagu-discord-testnet + image: pagu-discord:latest container_name: pagu-discord-testnet networks: pagu-network: depends_on: - - pagu-db + - pagu-phpmyadmin volumes: - - ../config/config.testnet.yml:/config/config.yml - - ../config/wallets/testnet_wallet:/config/wallets/testnet_wallet - pagu-telegram: - build: - context: ../. - dockerfile: deployment/telegram/Dockerfile.Mainnet - hostname: pagu-telegram-mainnet - container_name: pagu-telegram + - ${HOME}/pagu-discord-testnet:/pagu + + pagu-discord-moderator: + image: pagu-discord:stable + container_name: pagu-discord-moderator networks: pagu-network: depends_on: - - pagu-db + - pagu-phpmyadmin volumes: - - ../config/config.mainnet.yml:/config/config.yml - - ../config/wallets/main_wallet:/config/wallets/main_wallet - pagu-node: - hostname: pagu-node - domainname: pagu-node - container_name: pagu-node - image: pactus/pactus:latest - command: pactus-daemon start -w ./../../../pactus - restart: always - ports: - - "50051:50051" - volumes: - - ./../../../pactus:/pactus + - ${HOME}/pagu-discord-moderator:/pagu + + pagu-telegram-mainnet: + image: pagu-telegram:stable + container_name: pagu-telegram-mainnet networks: pagu-network: + depends_on: + - pagu-phpmyadmin + volumes: + - ${HOME}/pagu-telegram-mainnet:/pagu + volumes: - pagu-db-data: { } + pagu-volume: + +networks: + pagu-network: diff --git a/deployment/telegram/Dockerfile.Mainnet b/deployment/telegram/Dockerfile.Mainnet deleted file mode 100644 index 4f046342..00000000 --- a/deployment/telegram/Dockerfile.Mainnet +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.23.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN go mod tidy -RUN go mod vendor -RUN make build-telegram - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-telegram -COPY --from=builder /pagu/build /pagu-telegram - -RUN chmod +x /pagu-telegram/pagu-telegram - -CMD ["./pagu-telegram/pagu-telegram", "-c", "./config/config.yml", "run"] \ No newline at end of file