diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..17f8c98 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +*.md +.git +*.tar +tmp +.vscode +*.ipynb +__pycache__ +docker_save diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ce72fff..6a45c53 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,44 +1,70 @@ name: CI on: - release: - types: [published] push: - tags: - - "v*.*.*" + branches: + - master + pull_request: + branches: + - master + release: + # action by user workflow_dispatch: + jobs: - docker: + build: runs-on: ubuntu-latest + strategy: + matrix: + include: + - ARCH: armv7l + registry: ghcr.io + username: NAME_OWNER + password: GITHUB_TOKEN + - ARCH: x86_64 + registry: docker.io + username: DOCKERHUB_USERNAME + password: DOCKERHUB_TOKEN + name: build ${{ matrix.ARCH }} + steps: - - name: Checkout + - name: Prepare before build + run: | + sudo apt-get update -y + + - name: Checkout branch uses: actions/checkout@v2 - - - name: Login to DockerHub - uses: docker/login-action@v1 with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v1 - - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 + fetch-depth: 0 + + - name: Login to ${{ matrix.registry }} + uses: docker/login-action@v1 with: - context: . - platforms: linux/arm/v7 - # linux/amd64 - file: ./Dockerfile - push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/ramoloss:latest - - #- name: Run test - # run: | - # ci/test.sh + registry: ${{ matrix.registry }} + username: ${{ secrets[matrix.username] }} + password: ${{ secrets[matrix.password] }} + + - name: Build and test on ${{ matrix.ARCH }} plateform + env: + DISCORD_TOKEN: ${{ secrets.DISCORD_DEV_TOKEN }} + run: | + bash ci/build.sh + + - name: push on ${{ matrix.registry }} if success on main branch + if: ( success() && github.ref == 'refs/heads/main' ) + env: + ARCH: ${{ matrix.ARCH }} + REGISTRY: ${{ matrix.registry }} + REGISTRY_USERNAME: ${{ secrets[matrix.username] }} + run: | + bash ci/push.sh + + - name: push latest version on release + if: ( success() && github.event_name == 'release' ) + env: + ARCH: ${{ matrix.ARCH }} + REGISTRY: ${{ matrix.registry }} + REGISTRY_USERNAME: ${{ secrets[matrix.username] }} + run: | + make VERSION=latest push diff --git a/.gitignore b/.gitignore index 799c012..0929d02 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,9 @@ __pycache__ # folders _img_dices + # Pycharm .idea + +# env +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 1f3d09c..0000000 --- a/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM arm32v7/python:3.7-buster -WORKDIR /ramoloss -COPY . . -RUN apt-get update && apt-get install libatlas-base-dev -y && pip install --no-cache-dir -r requirements.txt -i https://www.piwheels.org/simple -CMD [ "python", "main.py"] diff --git a/Dockerfile.armv7l b/Dockerfile.armv7l new file mode 100644 index 0000000..75d9787 --- /dev/null +++ b/Dockerfile.armv7l @@ -0,0 +1,9 @@ +FROM arm32v7/python:3.7-buster as arch_armv7l +ENV DISCORD_TOKEN=${DISCORD_TOKEN} +WORKDIR /ramoloss +COPY . . +RUN apt-get update && apt-get install libatlas-base-dev -y \ + && pip install --no-cache-dir -r requirements.txt \ + -i https://www.piwheels.org/simple +CMD [ "python", "main.py"] + diff --git a/Dockerfile.x86_64 b/Dockerfile.x86_64 new file mode 100644 index 0000000..1a88fe7 --- /dev/null +++ b/Dockerfile.x86_64 @@ -0,0 +1,8 @@ +FROM python:3.7-buster as x86_64 +ENV DISCORD_TOKEN=${DISCORD_TOKEN} +WORKDIR /ramoloss +COPY . . +RUN apt-get update && pip install --no-cache-dir \ + -r requirements.txt +CMD [ "python", "main.py"] + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..46a10ec --- /dev/null +++ b/Makefile @@ -0,0 +1,88 @@ +# repository +SHELL = /bin/bash +NAME ?= ramoloss +ARCH ?= $(shell uname -m) +VERSION := $(shell git describe --abbrev=0) + +# docker-compose +DC := $(shell type -p docker-compose) +DC_BUILD_ARGS := --pull --force-rm +DC_RUN_ARGS := -d --no-build +DC_FILE := docker-compose.yml + +# docker +#DOCKER_REGISTRY := docker.io +#GITHUB_REGISTRY := ghcr.io +REPOSITORY ?= ramoloss + +# image +IMAGE_bot=${NAME}-bot:${VERSION} +IMAGE_REGISTRY_bot=${REGISTRY}/${REGISTRY_USERNAME}/${IMAGE_bot} + + +export + + +all: + @echo "Usage: NAME=ramoloss make deploy | build | \ + up | down | test | check | push | pull " + + +# check var or config +check-var-%: + @: $(if $(value $*),,$(error $* is undefined)) + @echo ${$*} + +check-config: + ${DC} -f ${DC_FILE} config + +check-config-quiet: + ${DC} -f ${DC_FILE} config -q + +# build all or one service +build: check-config-quiet + echo ${VERSION} + ${DC} -f ${DC_FILE} build ${DC_BUILD_ARGS} + +build-%: + @echo "# start $*" + ${DC} -f ${DC_FILE} build ${DC_BUILD_ARGS} $* + +# up all or one service +up: check-config-quiet + @if [ -z "${DISCORD_TOKEN}" ] ; \ + then echo "ERROR: DISCORD_TOKEN \ + not defined" ; exit 1 ; fi + ${DC} -f ${DC_FILE} up ${DC_RUN_ARGS} + +up-%: check-config-quiet + ${DC} -f ${DC_FILE} up ${DC_RUN_ARGS} $* + +# down all or one service +down: + ${DC} -f ${DC_FILE} down + +down-%: + ${DC} -f ${DC_FILE} down $* + +# test +test: test-container +test-%: + @echo "# test $*" + bash tests/test-$*.sh + +# push + +push: push-bot + +push-%: + @if [ -z "${REGISTRY}" -a -z "${REGISTRY_USERNAME}" ] ; \ + then echo "ERROR: REGISTRY and REGISTRY_USERNAME \ + not defined" ; exit 1 ; fi + docker tag ${IMAGE_$*} ${IMAGE_REGISTRY_$*} + docker push ${IMAGE_REGISTRY_$*} + +pull: pull-bot + +pull-%: + docker pull diff --git a/ci/build.sh b/ci/build.sh new file mode 100644 index 0000000..ef845e9 --- /dev/null +++ b/ci/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +make build +make up +make test +make down \ No newline at end of file diff --git a/ci/push.sh b/ci/push.sh new file mode 100644 index 0000000..9bd6c87 --- /dev/null +++ b/ci/push.sh @@ -0,0 +1 @@ +make push \ No newline at end of file diff --git a/config.json b/config.json index f5613ee..48778ee 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { + "description": "", "command_prefix": "!", - "description": "Très lent et endormi, il lui faut 5 secondes pour ressentir la douleur d'une attaque.", "extensions": [ "cogs.dice", "cogs.john", diff --git a/docker-compose.yml b/docker-compose.yml index bdbac0d..ce67a74 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,10 @@ -discord_bot: - container_name: ramoloss - build: . +version: '3.9' +services: + bot: environment: - - DISCORD_TOKEN=${DISCORD_TOKEN} \ No newline at end of file + - DISCORD_TOKEN=${DISCORD_TOKEN} + container_name: ${NAME}-bot + image: ${NAME}-bot:${VERSION} + build: + context : . + dockerfile: Dockerfile.${ARCH} diff --git a/requirements.txt b/requirements.txt index fe4676b..9621f08 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -discord.py -requests -beautifulsoup4 -numpy -opencv-python -matplotlib -pillow -lxml +beautifulsoup4==4.10.0 +discord.py==1.7.3 +lxml==4.6.3 +matplotlib==3.4.3 +numpy==1.21.2 +opencv-python==4.5.3.56 +Pillow==8.3.2 +requests==2.26.0 diff --git a/tests/test-container.sh b/tests/test-container.sh new file mode 100644 index 0000000..c76ba06 --- /dev/null +++ b/tests/test-container.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +active_container=$(docker ps --filter "name=$APP_NAME" -q) + +if [ -z "${active_container}" ]; then + echo "no container is running" + exit 1 +else + echo "container $APP_NAME with id $active_container is running" +fi \ No newline at end of file