From c857985a6d3736287976caf5b173f19046306465 Mon Sep 17 00:00:00 2001 From: VictorV Date: Fri, 9 Oct 2020 14:42:29 -0700 Subject: [PATCH] feat(docker): add docker and publish images to ghcr (#411) Co-authored-by: Jef LeCompte --- .dockerignore | 6 +++ .env-example | 102 ++++++++++++++++++-------------------- .github/workflows/cd.yaml | 22 ++++++++ .github/workflows/ci.yaml | 14 +++++- Dockerfile | 36 ++++++++++++++ README.md | 13 +++++ src/config.ts | 3 ++ src/index.ts | 5 ++ 8 files changed, 147 insertions(+), 54 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..1970493661 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.idea/ +.git/ +.vs/ +.vscode/ +build/ +node_modules/ diff --git a/.env-example b/.env-example index 3081143d4a..1825d525c8 100644 --- a/.env-example +++ b/.env-example @@ -2,56 +2,52 @@ # Read https://github.com/jef/nvidia-snatcher#customization for help on customizing this file ############################################################################################# -BROWSER_TRUSTED="" -COUNTRY="" -DISCORD_NOTIFY_GROUP="" -DISCORD_WEB_HOOK="" -EMAIL_USERNAME="" -EMAIL_TO="" -EMAIL_PASSWORD="" -HEADLESS="" -IN_STOCK_WAIT_TIME="" -LOG_LEVEL="" -LOW_BANDWIDTH="" -MAX_PRICE_3070="" -MAX_PRICE_3080="" -MAX_PRICE_3090="" -MICROCENTER_LOCATION="" -NVIDIA_ADD_TO_CART_ATTEMPTS="" -NVIDIA_SESSION_TTL="" -OPEN_BROWSER="" -PAGE_BACKOFF_MIN="" -PAGE_BACKOFF_MAX="" -PAGE_SLEEP_MIN="" -PAGE_SLEEP_MAX="" -PAGE_TIMEOUT="" -PHONE_NUMBER="" -PHONE_CARRIER="" -PLAY_SOUND="" -PROXY_ADDRESS="" -PROXY_PORT="" -PUSHBULLET="" -PUSHOVER_TOKEN="" -PUSHOVER_USER="" -PUSHOVER_PRIORITY="" -SCREENSHOT="false" -SHOW_ONLY_BRANDS="" -SHOW_ONLY_MODELS="" -SHOW_ONLY_SERIES="" -SLACK_CHANNEL="" -SLACK_TOKEN="" -SMTP_ADDRESS="" -SMTP_PORT="" -STORES="" -TELEGRAM_ACCESS_TOKEN="" -TELEGRAM_CHAT_ID="" -TWILIO_ACCOUNT_SID="" -TWILIO_AUTH_TOKEN="" -TWILIO_FROM_NUMBER="" -TWILIO_TO_NUMBER="" -TWITTER_CONSUMER_KEY="" -TWITTER_CONSUMER_SECRET="" -TWITTER_ACCESS_TOKEN_KEY="" -TWITTER_ACCESS_TOKEN_SECRET="" -TWITTER_TWEET_TAGS="" -USER_AGENT="" +BROWSER_TRUSTED= +COUNTRY= +DISCORD_NOTIFY_GROUP= +DISCORD_WEB_HOOK= +EMAIL_USERNAME= +EMAIL_TO= +EMAIL_PASSWORD= +HEADLESS= +IN_STOCK_WAIT_TIME= +LOG_LEVEL= +LOW_BANDWIDTH= +MAX_PRICE= +MICROCENTER_LOCATION= +NVIDIA_ADD_TO_CART_ATTEMPTS= +NVIDIA_SESSION_TTL= +OPEN_BROWSER= +PAGE_BACKOFF_MIN= +PAGE_BACKOFF_MAX= +PAGE_SLEEP_MIN= +PAGE_SLEEP_MAX= +PAGE_TIMEOUT= +PHONE_NUMBER= +PHONE_CARRIER= +PLAY_SOUND= +PROXY_ADDRESS= +PROXY_PORT= +PUSHBULLET= +PUSHOVER_TOKEN= +PUSHOVER_USER= +PUSHOVER_PRIORITY= +SCREENSHOT= +SHOW_ONLY_BRANDS= +SHOW_ONLY_MODELS= +SHOW_ONLY_SERIES= +SLACK_CHANNEL= +SLACK_TOKEN= +STORES= +TELEGRAM_ACCESS_TOKEN= +TELEGRAM_CHAT_ID= +TWILIO_ACCOUNT_SID= +TWILIO_AUTH_TOKEN= +TWILIO_FROM_NUMBER= +TWILIO_TO_NUMBER= +TWITTER_CONSUMER_KEY= +TWITTER_CONSUMER_SECRET= +TWITTER_ACCESS_TOKEN_KEY= +TWITTER_ACCESS_TOKEN_SECRET= +TWITTER_TWEET_TAGS= +USER_AGENT= diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 0f63dd2afc..048cddb5a0 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -16,3 +16,25 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} release-type: node package-name: nvidia-snatcher + - name: login into github package registry + run: echo ${{ secrets.CR_PAT }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin + - name: build nightly docker image + if: ${{ ! steps.release.outputs.release_created }} + run: | + docker build \ + -t "ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA:0:7}" \ + -t "ghcr.io/${GITHUB_REPOSITORY}:nightly" . + - name: publish nightly + if: ${{ ! steps.release.outputs.release_created }} + run: docker push "ghcr.io/${GITHUB_REPOSITORY}" + - name: build latest docker image + if: ${{ steps.release.outputs.release_created }} + run: | + docker build \ + -t "ghcr.io/${GITHUB_REPOSITORY}:${TAG_NAME}" \ + -t "ghcr.io/${GITHUB_REPOSITORY}:latest" . + env: + TAG_NAME: ${{ steps.release.outputs.tag_name }} + - name: publish latest + if: ${{ steps.release.outputs.release_created }} + run: docker push "ghcr.io/${GITHUB_REPOSITORY}" diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8e1203487a..0d8586695e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ jobs: build-lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v2 - uses: actions/setup-node@v2.1.2 with: node-version: 14 @@ -25,3 +25,15 @@ jobs: npm ci npm run build npm run lint + build-docker: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: dorny/paths-filter@v2 + id: changes + with: + filters: | + src: + - 'Dockerfile' + - name: Build image + run: docker build . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..1aa8340e48 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# Build the source code +FROM node:14.11.0-alpine3.12 AS builder + +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true + +WORKDIR /build + +COPY package.json package.json +COPY package-lock.json package-lock.json +COPY tsconfig.json tsconfig.json +RUN npm ci + +COPY src/ src/ +RUN npm run build +RUN npm prune --production + +FROM node:14.11.0-alpine3.12 + +RUN apk add --no-cache chromium + +ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \ + DOCKER=true + +RUN addgroup -S appuser && adduser -S -g appuser appuser \ + && mkdir -p /home/appuser/Downloads /app \ + && chown -R appuser:appuser /home/appuser \ + && chown -R appuser:appuser /app + +USER appuser + +WORKDIR /app + +COPY --from=builder /build/node_modules/ node_modules/ +COPY --from=builder /build/build/ build/ + +CMD [ "node", "./build/index.js" ] diff --git a/README.md b/README.md index 32bad8c251..915ca39ccb 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ You do not need any computer skills, smarts, or anything of that nature. You are ### Quick overview +#### Native installation + - [Node.js 14](https://nodejs.org/en/) - [git](https://git-scm.com/) - Clone this project `git clone https://github.com/jef/nvidia-snatcher.git` @@ -54,6 +56,17 @@ At any point you want the program to stop, use Ctrl + C. > :point_right: Please visit the [wiki](https://github.com/jef/nvidia-snatcher/wiki) if you need more help with installation. +#### Docker image (To run inside containers) + +Available via GitHub Container Registry. + +| Tag | Note | +|:---:|---| +| `latest` | Latest stable build | +| `nightly` | Latest HEAD build, could be unstable | + +Use `docker run --cap-add=SYS_ADMIN -it --rm --env-file ./.env ghcr.io/jef/nvidia-snatcher:nightly` to run. + ### Developer notes The command `npm run start:dev` can be used instead of `npm run start` to automatically restart the project when filesystem changes are detected in the `src/` folder or `.env` file. diff --git a/src/config.ts b/src/config.ts index 44f2c23c09..6155995e85 100644 --- a/src/config.ts +++ b/src/config.ts @@ -118,6 +118,8 @@ const browser = { open: envOrBoolean(process.env.OPEN_BROWSER) }; +const docker = envOrBoolean(process.env.DOCKER); + const logLevel = envOrString(process.env.LOG_LEVEL, 'info'); const notifications = { @@ -225,6 +227,7 @@ const store = { export const config = { browser, + docker, logLevel, notifications, nvidia, diff --git a/src/index.ts b/src/index.ts index 554b71c617..591e4c5a31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,11 @@ async function main() { args.push('--disable-setuid-sandbox'); } + // https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#tips + if (config.docker) { + args.push('--disable-dev-shm-usage'); + } + // Add the address of the proxy server if defined if (config.proxy.address) { args.push(`--proxy-server=http://${config.proxy.address}:${config.proxy.port}`);