diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 01781f8..bf7fc06 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -20,6 +20,11 @@ jobs: - name: Checkout uses: actions/checkout@v3 + - + name: Set environment variables + run: | + echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + echo "NOW=$(date -R)" >> $GITHUB_ENV # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S' - name: Set up QEMU uses: docker/setup-qemu-action@v2 @@ -29,6 +34,7 @@ jobs: - name: Login to Docker Hub uses: docker/login-action@v2 + # if: ${{ secrets.DOCKERHUB_USERNAME && secrets.DOCKERHUB_TOKEN }} with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} @@ -44,8 +50,14 @@ jobs: uses: docker/build-push-action@v4 with: context: . - platforms: linux/amd64,linux/arm64 # ,linux/arm/v7 push: true + build-args: | + COMMIT=${{ github.sha }} + BRANCH=${{ env.BRANCH }} + NOW=${{ env.NOW }} + platforms: linux/amd64,linux/arm64 # ,linux/arm/v7 tags: | voglerr/free-games-claimer:latest ghcr.io/vogler/free-games-claimer:latest + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index df20609..67a5e89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -60,12 +60,19 @@ COPY . . RUN dos2unix *.sh && chmod +x *.sh COPY docker-entrypoint.sh /usr/local/bin/ +ARG COMMIT="" +ARG BRANCH="" +ARG NOW="" +ENV COMMIT=${COMMIT} +ENV BRANCH=${BRANCH} +ENV NOW=${NOW} + LABEL org.opencontainers.image.title="free-games-claimer" \ org.opencontainers.image.name="free-games-claimer" \ org.opencontainers.image.description="Automatically claims free games on the Epic Games Store, Amazon Prime Gaming and GOG" \ org.opencontainers.image.url="https://github.com/vogler/free-games-claimer" \ org.opencontainers.image.source="https://github.com/vogler/free-games-claimer" \ - org.opencontainers.image.revision=${COMMIT_SHA} \ + org.opencontainers.image.revision=${COMMIT} \ org.opencontainers.image.ref.name=${BRANCH} \ org.opencontainers.image.base.name="ubuntu:jammy" \ org.opencontainers.image.version="latest" diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 7796364..2b5ddee 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -2,6 +2,10 @@ set -eo pipefail # exit on error, error on any fail in pipe (not just last cmd); add -x to print each cmd; see gist bash_strict_mode.md +echo "Version: https://github.com/vogler/free-games-claimer/tree/${COMMIT}" +[ ! -z $BRANCH ] && [ $BRANCH != "main" ] && echo "Branch: ${BRANCH}" +echo "Build: $NOW" + # Remove chromium profile lock. # When running in docker and then killing it, on the next run chromium displayed a dialog to unlock the profile which made the script time out. # Maybe due to changed hostname of container or due to how the docker container kills playwright - didn't check. diff --git a/util.js b/util.js index 8cc91e9..0b2dcbe 100644 --- a/util.js +++ b/util.js @@ -108,11 +108,11 @@ export const notify = (html) => new Promise((resolve, reject) => { const title = cfg.notify_title ? `-t ${cfg.notify_title}` : ''; exec(`apprise ${cfg.notify} -i html '${title}' -b '${html}'`, (error, stdout, stderr) => { if (error) { - console.log(`error: ${error.message}`); - if (error.message.includes('command not found')) { - console.info('Run `pip install apprise`. See https://github.com/vogler/free-games-claimer#notifications'); - } - return resolve(); + console.log(`error: ${error.message}`); + if (error.message.includes('command not found')) { + console.info('Run `pip install apprise`. See https://github.com/vogler/free-games-claimer#notifications'); + } + return resolve(); } if (stderr) console.error(`stderr: ${stderr}`); if (stdout) console.log(`stdout: ${stdout}`); diff --git a/version.js b/version.js new file mode 100644 index 0000000..7b9ebef --- /dev/null +++ b/version.js @@ -0,0 +1,50 @@ +// check if running the latest version + +import {log} from 'console'; +import { readFileSync } from 'fs'; +import { exec } from 'child_process'; + +const execp = (cmd) => new Promise((resolve, reject) => { + exec(cmd, (error, stdout, stderr) => { + if (stderr) console.error(`stderr: ${stderr}`); + // if (stdout) console.log(`stdout: ${stdout}`); + if (error) { + console.log(`error: ${error.message}`); + if (error.message.includes('command not found')) { + console.info('Install git to check for updates!'); + } + return reject(); + } + resolve(stdout.trim()); + }); +}); + +const git_main = () => readFileSync('.git/refs/heads/main').toString().trim(); + +let sha, date; +// if (existsSync('/.dockerenv')) { // did not work +if (process.env.NOVNC_PORT) { + log('Running inside Docker.'); + ['COMMIT', 'BRANCH', 'NOW'].forEach(v => log(` ${v}:`, process.env[v])); + sha = process.env.COMMIT; + date = process.env.NOW; +} else { + log('Not running inside Docker.'); + sha = await execp('git rev-parse HEAD'); + date = await execp('git show -s --format=%cD'); // same as format as `date -R` (RFC2822) + // date = await execp('git show -s --format=%ch'); // %ch is same as --date=human (short/relative) +} + +const gh = await (await fetch('https://api.github.com/repos/vogler/free-games-claimer/commits/main', { + // headers: { accept: 'application/vnd.github.VERSION.sha' } + })).json(); +// log(gh); + +log('Local commit:', sha, new Date(date)); +log('Online commit:', gh.sha, new Date(gh.commit.committer.date)); + +if (sha == gh.sha) { + log('Running the latest version!') +} else { + log('Not running the latest version!') +}