diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000000..62647acaa8 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,58 @@ +name: Build Docker image + +on: + pull_request: + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: pokeapi/updater + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + with: + detached: true + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + - name: Inspect builder + run: | + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + - name: Build + id: docker_build + uses: docker/build-push-action@v5 + with: + context: ./updater + load: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + - name: Run and test + run: (cd updater && bash cmd.bash) + env: + BRANCH_NAME: updater-bot + COMMIT_AND_PUSH: false + COMMIT_EMAIL: "" + COMMIT_MESSAGE: "[Updater Bot] Regenerate data" + COMMIT_NAME: Updater Bot + REPO_DATA: https://github.com/PokeAPI/api-data.git + REPO_POKEAPI: https://github.com/PokeAPI/pokeapi.git + - name: Image digest and tag + run: | + echo ${{ steps.docker_build.outputs.digest }} + echo ${{ steps.meta.outputs.tags }} diff --git a/updater/Dockerfile b/updater/Dockerfile index 9d987d8cfd..67847f7e1a 100644 --- a/updater/Dockerfile +++ b/updater/Dockerfile @@ -1,16 +1,11 @@ -FROM docker:18.09.3-dind +FROM docker:24.0.7-dind-alpine3.18 RUN apk update -RUN apk add curl python3 git bash dos2unix openssh build-base python3-dev - -RUN ln -sf $(ls /usr/bin/easy_install*) /usr/bin/easy_install -RUN easy_install pip -COPY --from=docker/compose:1.25.0-alpine /usr/local/bin/docker-compose /usr/local/bin/ +RUN apk add curl python3 git bash openssh build-base python3-dev py3-pip RUN mkdir /updater WORKDIR /updater COPY . /updater/ -RUN dos2unix cmd.bash ENV COMMIT_NAME 'Updater Bot' ENV COMMIT_EMAIL '' diff --git a/updater/cmd.bash b/updater/cmd.bash index 0015d6b818..fa9ba0f73a 100644 --- a/updater/cmd.bash +++ b/updater/cmd.bash @@ -1,19 +1,17 @@ #!/usr/bin/env bash -[ -z "${COMMIT_NAME}" ] && { echo "Need to set COMMIT_NAME"; exit 1; } -[ -z "${COMMIT_EMAIL}" ] && { echo "Need to set COMMIT_EMAIL"; exit 1; } -[ -z "${COMMIT_MESSAGE}" ] && { echo "Need to set COMMIT_MESSAGE"; exit 1; } [ -z "${REPO_POKEAPI}" ] && { echo "Need to set REPO_POKEAPI"; exit 1; } [ -z "${REPO_DATA}" ] && { echo "Need to set REPO_DATA"; exit 1; } [ -z "${BRANCH_NAME}" ] && { echo "Need to set BRANCH_NAME"; exit 1; } [ -z "${REPO_POKEAPI_CHECKOUT_OBJECT:=master}" ] && { echo "REPO_POKEAPI_CHECKOUT_OBJECT not set, defaulting to \`master\`"; } +[ -z "${REPO_APIDATA_CHECKOUT_OBJECT:=master}" ] && { echo "REPO_APIDATA_CHECKOUT_OBJECT not set, defaulting to \`master\`"; } +[ -z "${TEST:=true}" ] +declare -r COMMIT_AND_PUSH="${COMMIT_AND_PUSH:-false}" set -e set -o pipefail set -x -export COMPOSE_INTERACTIVE_NO_CLI=1 - dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 &> /dev/null & git clone "$REPO_POKEAPI" pokeapi @@ -25,15 +23,13 @@ git checkout "$REPO_POKEAPI_CHECKOUT_OBJECT" git submodule init git submodule update --remote -docker volume create --name=redis_data -docker volume create --name=pg_data -docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d - -docker-compose exec -T app python manage.py migrate --settings=config.docker-compose -docker-compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose' +docker compose -f docker-compose.yml -f docker-compose-dev.yml up -d +docker compose exec -T app python manage.py migrate --settings=config.docker-compose +docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose' # set up the data side cd ../api-data +git checkout "$REPO_APIDATA_CHECKOUT_OBJECT" git branch -D "$BRANCH_NAME" || true git branch "$BRANCH_NAME" git checkout "$BRANCH_NAME" @@ -46,11 +42,16 @@ ditto clone --src-url http://localhost/ --dest-dir ./data --select pokemon/129 ditto analyze --data-dir ./data # commit and push -git add data -git config user.name "$COMMIT_NAME" -git config user.email "$COMMIT_EMAIL" -if ! git commit -m "$COMMIT_MESSAGE"; then - echo "The generated data doesn't bring any updates" - exit 2 +if [ "$COMMIT_AND_PUSH" = 'true' ]; then + [ -z "${COMMIT_NAME}" ] && { echo "Need to set COMMIT_NAME"; exit 1; } + [ -z "${COMMIT_EMAIL}" ] && { echo "Need to set COMMIT_EMAIL"; exit 1; } + [ -z "${COMMIT_MESSAGE}" ] && { echo "Need to set COMMIT_MESSAGE"; exit 1; } + git add data + git config user.name "$COMMIT_NAME" + git config user.email "$COMMIT_EMAIL" + if ! git commit -m "$COMMIT_MESSAGE"; then + echo "The generated data doesn't bring any updates" + exit 2 + fi + git push -fu origin "$BRANCH_NAME" fi -git push -fu origin "$BRANCH_NAME"