From 619ff99f0509cbd04f6d57363dde745b542dd7e7 Mon Sep 17 00:00:00 2001 From: Cnily03 Date: Fri, 15 Mar 2024 06:43:34 +0800 Subject: [PATCH] doc: add docker compose file --- .github/workflows/publish.yml | 16 +++++++++++----- Dockerfile | 3 ++- README.md | 7 ++++++- docker-compose.yml | 15 +++++++++++++++ package.json | 3 ++- src/components/output.ts | 11 ++++++++++- test/docker.js | 10 ++++++++++ 7 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3a7b9cb..c21f48c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -30,13 +30,19 @@ jobs: - uses: actions/checkout@v4 - name: Build and Publish Docker Image run: | - tag=$(echo ${{ github.ref }} | sed 's/refs\/tags\///') - if [[ $tag == v[0-9]* ]]; then - tag=$(echo $tag | sed 's/v//') + tag="$(echo ${{ github.ref }} | sed 's/refs\/tags\///')" + if [[ "$tag" =~ ^v[0-9]* ]]; then + version="$(echo $tag | sed 's/v//')" + else + version="$tag" fi - docker build -t ${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:$tag . + docker build -t "${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:$version" . docker login -u ${{ vars.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_TOKEN }} - docker push ${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:$tag + docker push "${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:$version" + if [[ "$tag" == "$(git describe --tags --abbrev=0)" ]]; then + docker tag "${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:$version" "${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:latest" + docker push "${{ vars.DOCKER_HUB_USERNAME }}/ssl-autoupdater:latest" + fi npm: runs-on: 'ubuntu-latest' diff --git a/Dockerfile b/Dockerfile index 123db54..e9203c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,10 @@ FROM node:20.11.0 COPY . /app RUN mkdir -p /app/data COPY ./config.js /app/data/config.js +RUN mkdir -p /app/data/logs USER root WORKDIR /app RUN npm install && npm run build -ENTRYPOINT [ "npm", "run", "docker" ] \ No newline at end of file +ENTRYPOINT [ "npm", "run", "docker-log" ] \ No newline at end of file diff --git a/README.md b/README.md index bbba982..80e9b42 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,16 @@ Pull `cnily03/ssl-autoupdater` from Docker Hub docker run -itd \ -v /path/to/your/config.js:/app/data/config.js \ -v ~/.acme.sh:/root/.acme.sh \ - --restart=always \ + --restart=unless-stopped \ --name ssl-autoupdater \ cnily03/ssl-autoupdater ``` +> [!TIP] +> Docker compose file is also provided in the repository. +> Edit it according to annotations in the file. +> Then run `docker compose up -d` to start the service. + Replace `/path/to/your/config.js` with your own configuration file. Or you can mount the whole directory to `/app/data/` to make it easier to manage. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cae2c55 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3' + +services: + ssl-autoupdater: + image: cnily03/ssl-autoupdater + container_name: ssl-autoupdater + tty: true + environment: + - TZ=Asia/Shanghai + volumes: + # Make directory `data` in the same directory as your docker-compose.yml file + # Create your configuration file in data directory + - ./data:/app/data + - ~/.acme.sh:/root/.acme.sh + restart: unless-stopped diff --git a/package.json b/package.json index 39f77ee..ed0df22 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "compile-template": "node compile-template.js", "build": "rm -rf dist/* dist/.[!.]* dist/..?* && tsc && tsc-alias && npm run compile-template", "test": "npm run build && node test/index.js", - "docker": "node test/docker.js" + "docker": "node test/docker.js", + "docker-log": "mkdir -p /app/data/logs && node test/docker.js | tee /app/data/logs/$(date +\"%Y%m%d_%H%M%S\").log" }, "keywords": ["auto", "SSL", "update"], "author": "Cnily03", diff --git a/src/components/output.ts b/src/components/output.ts index 6072416..7fbe824 100644 --- a/src/components/output.ts +++ b/src/components/output.ts @@ -1,7 +1,16 @@ import { Session } from "@/components/session" import "colors" -const date_string = () => new Date().toLocaleString().replace(/\/(\d)([^\d])/, "/0$1$2").replace(/\/(\d)([^\d])/, "/0$1$2").replace(/\//g, "-") +const date_string = () => { + let date = new Date() + let year = date.getFullYear() + let month = (date.getMonth() + 1).toString().padStart(2, "0") + let day = date.getDate().toString().padStart(2, "0") + let hours = date.getHours().toString().padStart(2, "0") + let minutes = date.getMinutes().toString().padStart(2, "0") + let seconds = date.getSeconds().toString().padStart(2, "0") + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` +} export type OutputConstructor = new (session: Session, identifier?: string) => Output diff --git a/test/docker.js b/test/docker.js index 21fd443..5afedbc 100644 --- a/test/docker.js +++ b/test/docker.js @@ -7,6 +7,9 @@ if (!fs.existsSync(CONF_PATH)) { console.error("Config file not found at " + CONF_PATH) process.exit(1) } + +let is_watch = false + // const CONFIG = require("../config.js") const CONFIG = require(CONF_PATH) @@ -55,6 +58,7 @@ if (CONFIG.qcloud.enable) { if (CONFIG.mailserver.enable) opts.mailer = new autossl.MailSender(mailOpts()) const QCloudUpdater = new autossl.updater.QCloud(CONFIG.qcloud.secretId, CONFIG.qcloud.secretKey, opts) QCloudUpdater.watch() + is_watch = true } if (CONFIG.qiniu.enable) { @@ -62,4 +66,10 @@ if (CONFIG.qiniu.enable) { if (CONFIG.mailserver.enable) opts.mailer = new autossl.MailSender(mailOpts()) const QiniuUpdater = new autossl.updater.Qiniu(CONFIG.qiniu.accessKey, CONFIG.qiniu.secretKey, opts) QiniuUpdater.watch() + is_watch = true +} + +if (!is_watch) { + console.log("No updater enabled. Please stop the container.") + process.stdin.resume(); } \ No newline at end of file