From a290eb0800626b24044e3e633cbf953d03d1ebe6 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:32:17 +0500 Subject: [PATCH] feat: added dockerfile and docker image push workflow for edx-api-notes (#27) * feat: added dockerfile and docker image push workflow for edx-api-notes * refactor: updated push image workflow to add platforms * chore: updated mail team for failure notification * perf: updated Dockerfile to optimize requirements installation and dependency caching * chore: Remove pull_request trigger from workflow * chore: updated push workflow --- .../workflows/push-edx-notes-api-image.yaml | 58 ++++++++++++ dockerfiles/edx-notes-api.Dockerfile | 94 +++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 .github/workflows/push-edx-notes-api-image.yaml create mode 100644 dockerfiles/edx-notes-api.Dockerfile diff --git a/.github/workflows/push-edx-notes-api-image.yaml b/.github/workflows/push-edx-notes-api-image.yaml new file mode 100644 index 0000000..8ad2558 --- /dev/null +++ b/.github/workflows/push-edx-notes-api-image.yaml @@ -0,0 +1,58 @@ +name: Build and Push Edx Notes Api Image + +on: + workflow_dispatch: + inputs: + branch: + description: "Target branch from which the source dockerfile from image will be sourced" + + schedule: + - cron: "0 4 * * 1-5" # UTC Time + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + + steps: + - name: Get tag name + id: get-tag-name + uses: actions/github-script@v5 + with: + script: | + const tagName = "${{ github.event.inputs.branch }}" || 'latest'; + return tagName; + result-encoding: string + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Build and push Dev Docker image + uses: docker/build-push-action@v6 + with: + file: ./dockerfiles/edx-notes-api.Dockerfile + push: true + target: dev + tags: edxops/edx-notes-api-dev:${{ steps.get-tag-name.outputs.result }} + platforms: linux/amd64,linux/arm64 + + - name: Send failure notification + if: failure() + uses: dawidd6/action-send-mail@v3 + with: + server_address: email-smtp.us-east-1.amazonaws.com + server_port: 465 + username: ${{secrets.edx_smtp_username}} + password: ${{secrets.edx_smtp_password}} + subject: Push Image to docker.io/edxops failed in Edx Notes Api + to: team-aurora@edx.org + from: github-actions + body: Push Image to docker.io/edxops for Edx Notes Api failed! For details see "github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" diff --git a/dockerfiles/edx-notes-api.Dockerfile b/dockerfiles/edx-notes-api.Dockerfile new file mode 100644 index 0000000..9097c34 --- /dev/null +++ b/dockerfiles/edx-notes-api.Dockerfile @@ -0,0 +1,94 @@ +FROM ubuntu:focal as app + +# Packages installed: +# git; Used to pull in particular requirements from github rather than pypi, +# and to check the sha of the code checkout. + +# language-pack-en locales; ubuntu locale support so that system utilities have a consistent +# language and time zone. + +# python3.8-dev; to install python 3.8 +# python3-venv; installs venv module required to create virtual environments + +# libssl-dev; # mysqlclient wont install without this. + +# libmysqlclient-dev; to install header files needed to use native C implementation for +# MySQL-python for performance gains. + +# If you add a package here please include a comment above describing what it is used for + +RUN apt-get update && \ + apt-get install -y software-properties-common && \ + apt-add-repository -y ppa:deadsnakes/ppa && \ + apt-get update && apt-get upgrade -qy && \ + apt-get install \ + language-pack-en \ + locales \ + git \ + libmysqlclient-dev \ + pkg-config \ + libssl-dev \ + build-essential \ + python3.8-dev \ + python3.8-distutils \ + python3-virtualenv -qy && \ + rm -rf /var/lib/apt/lists/* + + +RUN locale-gen en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + + +# ENV variables lifetime is bound to the container whereas ARGS variables lifetime is bound to the image building process only +# Also ARGS provide us an option of compatibility of Path structure for Tutor and other OpenedX installations +ARG COMMON_CFG_DIR "/edx/etc" +ARG COMMON_APP_DIR="/edx/app" +ARG NOTES_VENV_DIR="${COMMON_APP_DIR}/venvs/notes" + +ENV PATH="$NOTES_VENV_DIR/bin:$PATH" + +RUN useradd -m --shell /bin/false app + +# Install curl +RUN apt-get update && apt-get install -y curl + +RUN mkdir -p requirements + +RUN virtualenv -p python3.8 --always-copy ${NOTES_VENV_DIR} + +RUN pip install --upgrade pip setuptools + +RUN curl -L -o requirements/base.txt https://raw.githubusercontent.com/openedx/edx-notes-api/master/requirements/base.txt +RUN curl -L -o requirements/pip.txt https://raw.githubusercontent.com/openedx/edx-notes-api/master/requirements/pip.txt + +RUN pip install --no-cache-dir -r requirements/base.txt +RUN pip install --no-cache-dir -r requirements/pip.txt + +RUN mkdir -p /edx/var/log + +EXPOSE 8120 + +FROM app as dev + +ENV DJANGO_SETTINGS_MODULE "notesserver.settings.devstack" + +# Backwards compatibility with devstack +RUN touch "${COMMON_APP_DIR}/edx_notes_api_env" + +RUN curl -L https://github.com/openedx/edx-notes-api/archive/refs/heads/master.tar.gz | tar -xz --strip-components=1 + +CMD while true; do python ./manage.py runserver 0.0.0.0:8120; sleep 2; done + +FROM app as production + +ENV EDXNOTES_CONFIG_ROOT /edx/etc +ENV DJANGO_SETTINGS_MODULE "notesserver.settings.yaml_config" + +RUN curl -L https://github.com/openedx/edx-notes-api/archive/refs/heads/master.tar.gz | tar -xz --strip-components=1 + +USER app + +# Gunicorn 19 does not log to stdout or stderr by default. Once we are past gunicorn 19, the logging to STDOUT need not be specified. +CMD gunicorn --workers=2 --name notes -c /edx/app/notes/notesserver/docker_gunicorn_configuration.py --log-file - --max-requests=1000 notesserver.wsgi:application