From e047560790d93bb6398a4fea6701b06f0d603c39 Mon Sep 17 00:00:00 2001 From: Hu3rror <19755727+hu3rror@users.noreply.github.com> Date: Sun, 19 May 2024 20:44:09 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=A7=20(server.ts):=20change=20port?= =?UTF-8?q?=20variable=20case=20from=20lowercase=20to=20uppercase=20for=20?= =?UTF-8?q?consistency=20=20=20=F0=9F=94=A7=20(server.ts):=20add=20support?= =?UTF-8?q?=20for=20process.env.PORT=20environment=20variable=20to=20be=20?= =?UTF-8?q?able=20to=20run=20app=20on=20a=20configurable=20port=20=20=20?= =?UTF-8?q?=F0=9F=94=A7=20(.env.example):=20add=20new=20environment=20vari?= =?UTF-8?q?ables=20for=20Litestream=20and=20Memogram=20=20=20=F0=9F=9A=80?= =?UTF-8?q?=20(.github/workflows/get-build-stable-artifacts-with-memogram-?= =?UTF-8?q?and-push-image.yml):=20add=20new=20GitHub=20Actions=20workflow?= =?UTF-8?q?=20to=20build=20and=20push=20Docker=20images=20=20=20?= =?UTF-8?q?=F0=9F=9A=80=20(Dockerfile.memogram):=20add=20new=20Dockerfile?= =?UTF-8?q?=20for=20Memogram=20=20=20=F0=9F=94=A7=20(etc/Procfile):=20add?= =?UTF-8?q?=20new=20Procfile=20for=20Memogram=20=20=20=F0=9F=94=A7=20(etc/?= =?UTF-8?q?memogram.env):=20add=20new=20environment=20file=20for=20Memogra?= =?UTF-8?q?m=20=20=20=F0=9F=94=A7=20(scripts/run.sh):=20add=20script=20to?= =?UTF-8?q?=20replace=20MEMOGRAM=5FBOT=5FTOKEN=20and=20MEMOS=5FPORT=20in?= =?UTF-8?q?=20.env=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 +- ...artifacts-with-memogram-and-push-image.yml | 68 ++++++++++++++++ Dockerfile.memogram | 78 +++++++++++++++++++ etc/Procfile | 2 + etc/memogram.env | 2 + scripts/run.sh | 12 ++- 6 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/get-build-stable-artifacts-with-memogram-and-push-image.yml create mode 100644 Dockerfile.memogram create mode 100644 etc/Procfile create mode 100644 etc/memogram.env diff --git a/.env.example b/.env.example index 03d2e76..c792f5c 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,9 @@ +# For Litestream LITESTREAM_ACCESS_KEY_ID=000000001a2b3c40000000001 LITESTREAM_SECRET_ACCESS_KEY=K000ABCDEFGHiJkLmNoPqRsTuVwXyZ0 LITESTREAM_REPLICA_BUCKET=xxxxxxxxx LITESTREAM_REPLICA_ENDPOINT=s3.us-west-000.backblazeb2.com -LITESTREAM_REPLICA_PATH=memos_prod.db \ No newline at end of file +LITESTREAM_REPLICA_PATH=memos_prod.db +# For Memogram +MEMOGRAM_SERVER_ADDR=dns:localhost:5230 +MEMOGRAM_BOT_TOKEN=your_telegram_bot_token \ No newline at end of file diff --git a/.github/workflows/get-build-stable-artifacts-with-memogram-and-push-image.yml b/.github/workflows/get-build-stable-artifacts-with-memogram-and-push-image.yml new file mode 100644 index 0000000..f97b9f7 --- /dev/null +++ b/.github/workflows/get-build-stable-artifacts-with-memogram-and-push-image.yml @@ -0,0 +1,68 @@ +name: get-build-stable-artifacts-with-memogram-and-push-image + +on: + push: + paths: + - 'Dockerfile.memogram' + - 'scripts/run.sh' + workflow_dispatch: + +jobs: + build-and-push-release-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Extract build args + id: keydb + uses: pozetroninc/github-action-get-latest-release@v0.8.0 + with: + owner: usememos + repo: memos + excludes: prerelease, draft + token: ${{ github.token }} + - name: Login to Docker Hub + uses: docker/login-action@v3 + if: github.event_name != 'pull_request' + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + hu3rror/memos-litestream + ghcr.io/hu3rror/memos-litestream + tags: | + type=raw,value=stable-memogram + type=semver,pattern={{version}},value=${{ steps.keydb.outputs.release }}-memogram + + - name: Build and Push + id: docker_build + uses: docker/build-push-action@v5 + with: + context: ./ + file: ./Dockerfile.memogram + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile.memogram b/Dockerfile.memogram new file mode 100644 index 0000000..dd27f78 --- /dev/null +++ b/Dockerfile.memogram @@ -0,0 +1,78 @@ +# Set the Litestream image tag +ARG LITESTREAM_IMAGE_TAG=0.3.13 +# Set the Memos image tag +ARG MEMOS_IMAGE_TAG=0.22.0 + +# Build Litestream +FROM docker.io/litestream/litestream:${LITESTREAM_IMAGE_TAG} AS litestream_upstream +# Set the entry point to an empty array +ENTRYPOINT [] + +# Build Memos +FROM ghcr.io/usememos/memos:${MEMOS_IMAGE_TAG} AS memos_upstream +# Set the entry point to an empty array +ENTRYPOINT [] + +# Final image +FROM ubuntu:latest as final +# Set the working directory to /usr/local/memos +WORKDIR /usr/local/memos + +# Update apt and install necessary software +RUN apt-get update && apt-get install -y wget tzdata tmux curl bash +# Set the timezone to UTC +ENV TZ="UTC" + +# Copy Memos to /usr/local/memos +COPY --from=memos_upstream /usr/local/memos/ /usr/local/memos/ + +# Copy Litestream to /usr/local/bin +COPY --from=litestream_upstream /usr/local/bin/litestream /usr/local/bin/litestream + +# Set the target architecture +ARG TARGETARCH + +# Download Memogram +ENV MEMOGRAM_TAG=0.1.1 +RUN wget https://github.com/usememos/telegram-integration/releases/download/v${MEMOGRAM_TAG}/memogram_v${MEMOGRAM_TAG}_linux_${TARGETARCH}.tar.gz && \ + tar -xvf memogram_v${MEMOGRAM_TAG}_linux_${TARGETARCH}.tar.gz && \ + rm memogram_v${MEMOGRAM_TAG}_linux_${TARGETARCH}.tar.gz README.md && \ + chown root:root /usr/local/memos/memogram && \ + chmod +x /usr/local/memos/memogram + +# Download overmind +ENV OVERMIND_VERSION=2.5.1 +RUN wget https://github.com/DarthSim/overmind/releases/download/v${OVERMIND_VERSION}/overmind-v${OVERMIND_VERSION}-linux-${TARGETARCH}.gz && \ + gzip -d overmind-v${OVERMIND_VERSION}-linux-${TARGETARCH}.gz && \ + mv overmind-v${OVERMIND_VERSION}-linux-${TARGETARCH} overmind && \ + chmod +x overmind + +# Copy Memogram environment file +COPY etc/memogram.env /usr/local/memos/.env + +# Copy Litestream global configuration file +COPY etc/litestream.yml /etc/litestream.yml + +# Copy startup script and make it executable +COPY scripts/run.sh /usr/local/memos/run.sh +RUN chmod +x /usr/local/memos/run.sh + +# Copy overmind configuration file +COPY etc/Procfile /usr/local/memos/Procfile + +# Define environment variables +ENV DB_PATH="/var/opt/memos/memos_prod.db" + +# Expose port 5230 +EXPOSE 5230 + +# Create a directory to store data, which can be referenced as the mounting point +RUN mkdir -p /var/opt/memos +VOLUME /var/opt/memos + +# Set Memos mode to "prod" and port to 5230 +ENV MEMOS_MODE="prod" +ENV MEMOS_PORT="5230" + +# Run Memos with Litestream (Default WORKDIR is `/usr/local/memos/`) +CMD ["./overmind", "start", "-f", "/usr/local/memos/Procfile"] diff --git a/etc/Procfile b/etc/Procfile new file mode 100644 index 0000000..e2d202e --- /dev/null +++ b/etc/Procfile @@ -0,0 +1,2 @@ +memos: ./run.sh +memogram: sleep 5 && ./memogram \ No newline at end of file diff --git a/etc/memogram.env b/etc/memogram.env new file mode 100644 index 0000000..ec1f22f --- /dev/null +++ b/etc/memogram.env @@ -0,0 +1,2 @@ +SERVER_ADDR=dns:localhost:5230 +BOT_TOKEN= \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh index beed392..a86f24b 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -10,7 +10,17 @@ else echo "Finished restoring the database." fi -echo "Starting litestream & memos service." +# Replace MEMOGRAM_BOT_TOKEN in .env +if [ -f "./memogram" ] && [ -f "./.env" ] && [ -n "$MEMOGRAM_BOT_TOKEN" ]; then + # Replace MEMOGRAM_BOT_TOKEN in .env + sed -i 's//'"$MEMOGRAM_BOT_TOKEN"'/g' ./.env + + # Replace MEMOS_PORT in .env + if [ "$MEMOS_PORT" != "5230" ]; then + sed -i 's/5230/'"$MEMOS_PORT"'/g' ./.env + fi +fi # Run litestream with your app as the subprocess. +echo "Starting litestream & memos service." exec litestream replicate -exec "./memos"