diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 11bf4bd..4c78974 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -36,63 +36,106 @@ jobs: needs: check_changelog env: - TEST_TAG: ghcr.io/${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:test + BASE_TAG: retroarcher/discord-bot steps: - name: Checkout uses: actions/checkout@v3 + - name: Prepare + id: prepare + env: + NEXT_VERSION: ${{ needs.check_changelog.outputs.next_version }} + run: | + # determine to push image to dockerhub and ghcr or not + if [[ $GITHUB_EVENT_NAME == "push" ]]; then + PUSH=true + else + PUSH=false + fi + + # setup the tags + TAGS="${BASE_TAG}:${GITHUB_SHA},ghcr.io/${BASE_TAG}:${GITHUB_SHA}" + + if [[ $GITHUB_REF == refs/heads/master ]]; then + TAGS="${TAGS},${BASE_TAG}:latest,ghcr.io/${BASE_TAG}:latest" + TAGS="${TAGS},${BASE_TAG}:master,ghcr.io/${BASE_TAG}:master" + elif [[ $GITHUB_REF == refs/heads/nightly ]]; then + TAGS="${TAGS},${BASE_TAG}:nightly,ghcr.io/${BASE_TAG}:nightly" + else + TAGS="${TAGS},${BASE_TAG}:test,ghcr.io/${BASE_TAG}:test" + fi + + if [[ ${NEXT_VERSION} != "" ]]; then + TAGS="${TAGS},${BASE_TAG}:${NEXT_VERSION},ghcr.io/${BASE_TAG}:${NEXT_VERSION}" + fi + + echo ::set-output name=branch::${GITHUB_REF#refs/heads/} + echo ::set-output name=build_date::$(date -u +'%Y-%m-%dT%H:%M:%SZ') + echo ::set-output name=commit::${GITHUB_SHA} + echo ::set-output name=platforms::linux/386,linux/amd64,linux/arm64/v8 + echo ::set-output name=push::${PUSH} + echo ::set-output name=tags::${TAGS} + + - name: Set Up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 + id: buildx + + - name: Cache Docker Layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- - name: Log in to Docker Hub + if: ${{ steps.prepare.outputs.push == 'true' }} # PRs do not have access to secrets uses: docker/login-action@v1 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - name: Log in to the Container registry + if: ${{ steps.prepare.outputs.push == 'true' }} # PRs do not have access to secrets uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and export to Docker + - name: Build and push uses: docker/build-push-action@v2 with: context: ./ file: ./Dockerfile - load: true - tags: ${{ env.TEST_TAG }} + push: ${{ steps.prepare.outputs.push }} + platforms: ${{ steps.prepare.outputs.platforms }} + build-args: | + BRANCH=${{ steps.prepare.outputs.branch }} + COMMIT=${{ steps.prepare.outputs.commit }} + BUILD_DATE=${{ steps.prepare.outputs.build_date }} + tags: ${{ steps.prepare.outputs.tags }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache - - name: Test Image - run: | - docker run -d -e 'bot_token=${{ secrets.DISCORD_TEST_BOT_TOKEN }}' ${{ env.TEST_TAG }} - - - name: Build and push (non master) - if: ${{ github.event_name == 'push' && github.ref != 'refs/heads/master' }} - uses: docker/build-push-action@v2 - with: - context: ./ - file: ./Dockerfile - push: true - tags: | - ${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:${{ github.ref_name }} - ghcr.io/${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:${{ github.ref_name }} - - - name: Build and push (master) + - name: Update Docker Hub Description if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} - uses: docker/build-push-action@v2 + uses: peter-evans/dockerhub-description@v3 with: - context: ./ - file: ./Dockerfile - push: true - tags: | - ${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:latest - ghcr.io/${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:latest - ${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:${{ needs.check_changelog.outputs.next_version }} - ghcr.io/${{ secrets.DOCKER_HUB_USERNAME }}/discord-bot:${{ needs.check_changelog.outputs.next_version }} + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_PASSWORD }} # token is not currently supported + repository: ${{ env.BASE_TAG }} + short-description: ${{ github.event.repository.description }} + readme-filepath: ./DOCKER_README.md + + create_release: + runs-on: ubuntu-latest + needs: [check_changelog, docker] + steps: - name: Create/Update GitHub Release if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} diff --git a/CHANGELOG.md b/CHANGELOG.md index aed2838..37d002b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [0.0.4] - 2022-04-24 +### Fixed +- Corrected environment variable for daily release task + ## [0.0.3] - 2022-04-06 ### Fixed - Environment variable names switched to uppercase diff --git a/DOCKER_README.md b/DOCKER_README.md new file mode 100644 index 0000000..d7eabf5 --- /dev/null +++ b/DOCKER_README.md @@ -0,0 +1,82 @@ +# Docker + +## Using docker run +Create and run the container (substitute your ``): + +```bash +docker run -d \ + --name=retroarcher-discord-bot \ + --restart=unless-stopped \ + -e BOT_TOKEN= \ + -e DAILY_CHANNEL_ID= \ + -e DAILY_RELEASES= \ + -e DAILY_TASKS= \ + -e DAILY_TASKS_UTC_HOUR= \ + -e GRAVATAR_EMAIL= \ + -e IGDB_CLIENT_ID= \ + -e IGDB_CLIENT_SECRET= \ + retroarcher/discord-bot +``` + +To update the container it must be removed and recreated: + +```bash +# Stop the container +docker stop retroarcher-discord-bot +# Remove the container +docker rm retroarcher-discord-bot +# Pull the latest update +docker pull retroarcher/discord-bot +# Run the container with the same parameters as before +docker run -d ... +``` + +## Using docker-compose + +Create a `docker-compose.yml` file with the following contents (substitute your ``): + +```yaml +version: '3' +services: + retroarcher-discord-bot: + image: retroarcher/discord-bot + container_name: retroarcher-discord-bot + restart: unless-stopped + environment: + - BOT_TOKEN= + - DAILY_CHANNEL_ID= + - DAILY_RELEASES= + - DAILY_TASKS= + - DAILY_TASKS_UTC_HOUR= + - GRAVATAR_EMAIL= + - IGDB_CLIENT_ID= + - IGDB_CLIENT_SECRET= +``` + +Create and start the container (run the command from the same folder as your `docker-compose.yml` file): + +```bash +docker-compose up -d +``` + +To update the container: +```bash +# Pull the latest update +docker-compose pull +# Update and restart the container +docker-compose up -d +``` + +## Parameters +You must substitute the `` with your own settings. + +| Parameter | Required | Default | Description | +| -------------------- | -------- | ------- | ------------------------------------------------------------- | +| BOT_TOKEN | True | None | Token from Bot page on discord developer portal. | +| DAILY_TASKS | False | true | Daily tasks on or off. | +| DAILY_RELEASES | False | true | Send a message for each game released on this day in history. | +| DAILY_CHANNEL_ID | False | None | Required if daily_tasks is enabled. | +| DAILY_TASKS_UTC_HOUR | False | 12 | The hour to run daily tasks. | +| GRAVATAR_EMAIL | False | None | Gravatar email address for bot avatar. | +| IGDB_CLIENT_ID | False | None | Required if daily_releases is enabled. | +| IGDB_CLIENT_SECRET | False | None | Required if daily_releases is enabled. | diff --git a/discord_bot.py b/discord_bot.py index 16feb8e..8c2b073 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -382,7 +382,7 @@ async def daily_task(): except KeyError: daily_releases = True else: - if os.environ['DAILY_TASKS'].lower() == 'true': + if os.environ['DAILY_RELEASES'].lower() == 'true': daily_releases = True else: print("'DAILY_RELEASES' environment variable is disabled") diff --git a/requirements.txt b/requirements.txt index a215d5a..a01165f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -Flask==2.0.2 +Flask==2.1.1 igdb-api-v4==0.0.5 libgravatar==1.0.0 py-cord==2.0.0b3 -python-dotenv==0.19.2 +python-dotenv==0.20.0 requests==2.27.1