From 42337d935ba345ed04c2482bc6209460ec9d5d66 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:30:26 +0500 Subject: [PATCH 1/8] feat: added dockerfile and docker image push workflow for enterprise-subsidy --- .../push-enterprise-subsidy-image.yaml | 62 ++++++++++ dockerfiles/enterprise-subsidy.Dockerfile | 107 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 .github/workflows/push-enterprise-subsidy-image.yaml create mode 100644 dockerfiles/enterprise-subsidy.Dockerfile diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml new file mode 100644 index 0000000..f1c3119 --- /dev/null +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -0,0 +1,62 @@ +name: Build and Push Enterprise Subsidy 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 + + pull_request: + branches: + - '**' + +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'; + console.log('Will use tag: ' + tagName); + 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/enterprise-subsidy.Dockerfile + push: true + target: dev + tags: edxops/enterprise-subsidy-dev:${{ steps.get-tag-name.outputs.result }} + + - 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 Enterprise Subsidy + to: team-cosmonauts@edx.org + from: github-actions + body: Push Image to docker.io/edxops for Enterprise Subsidy failed! For details see "github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \ No newline at end of file diff --git a/dockerfiles/enterprise-subsidy.Dockerfile b/dockerfiles/enterprise-subsidy.Dockerfile new file mode 100644 index 0000000..573c3f2 --- /dev/null +++ b/dockerfiles/enterprise-subsidy.Dockerfile @@ -0,0 +1,107 @@ +FROM ubuntu:focal as app +MAINTAINER sre@edx.org + +# Packages installed: + +# language-pack-en locales; ubuntu locale support so that system utilities have a consistent +# language and time zone. + +# python; ubuntu doesnt ship with python, so this is the python we will use to run the application + +# python3-pip; install pip to install application requirements.txt files + +# pkg-config +# mysqlclient>=2.2.0 requires this (https://github.com/PyMySQL/mysqlclient/issues/620) + +# libmysqlclient-dev; to install header files needed to use native C implementation for +# MySQL-python for performance gains. + +# libssl-dev; # mysqlclient wont install without this. + +# python3-dev; to install header files for python extensions; much wheel-building depends on this + +# gcc; for compiling python extensions distributed with python packages like mysql-client + +# git; necessary to install local python packages in editable mode via pip. It's got electrolytes. + +# make; we use makefiles for all sorts of stuff + +# ENV variables for Python 3.12 support +ARG PYTHON_VERSION=3.12 +ENV TZ=UTC +ENV TERM=xterm-256color +ENV DEBIAN_FRONTEND=noninteractive + +# software-properties-common is needed to setup Python 3.12 env +RUN apt-get update && \ + apt-get install -y software-properties-common && \ + apt-add-repository -y ppa:deadsnakes/ppa + +# If you add a package here please include a comment above describing what it is used for +RUN apt-get update && apt-get -qy install --no-install-recommends \ + build-essential \ + language-pack-en \ + locales \ + pkg-config \ + libmysqlclient-dev \ + libssl-dev \ + gcc \ + git \ + make \ + curl \ + libffi-dev \ + libsqlite3-dev \ + python3-pip \ + python${PYTHON_VERSION} \ + python${PYTHON_VERSION}-dev \ + python${PYTHON_VERSION}-distutils + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +# delete apt package lists because we do not need them inflating our image +RUN rm -rf /var/lib/apt/lists/* + +# need to use virtualenv pypi package with Python 3.12 +RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} +RUN pip install virtualenv + +# cloning git repo +RUN curl -L https://github.com/openedx/enterprise-subsidy/archive/refs/heads/main.tar.gz | tar -xz --strip-components=1 + +# Create a virtualenv for sanity +ENV VIRTUAL_ENV=/edx/venvs/enterprise-subsidy +RUN virtualenv -p python${PYTHON_VERSION} $VIRTUAL_ENV +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +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 DJANGO_SETTINGS_MODULE enterprise_subsidy.settings.production + +EXPOSE 18280 +RUN useradd -m --shell /bin/false app + +# Dependencies are installed as root so they cannot be modified by the application user. +RUN pip install -r requirements/pip.txt +RUN pip install -r requirements/production.txt + +RUN mkdir -p /edx/var/log + +# Code is owned by root so it cannot be modified by the application user. +# So we copy it before changing users. +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 enterprise-subsidy -c /edx/app/enterprise-subsidy/enterprise_subsidy/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_subsidy.wsgi:application + + +FROM app as newrelic +RUN pip install newrelic +CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsidy/enterprise_subsidy/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_subsidy.wsgi:application + +FROM app as devstack +USER root +RUN pip install -r requirements/dev.txt +USER app +CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsidy/enterprise_subsidy/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_subsidy.wsgi:application From ca5cc2b43071736682b9def372ba60e62b748d55 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:41:06 +0500 Subject: [PATCH 2/8] refactor: updated push image workflow target --- .github/workflows/push-enterprise-subsidy-image.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index f1c3119..311e5a3 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -45,7 +45,7 @@ jobs: with: file: ./dockerfiles/enterprise-subsidy.Dockerfile push: true - target: dev + target: devstack tags: edxops/enterprise-subsidy-dev:${{ steps.get-tag-name.outputs.result }} - name: Send failure notification From 45b5a09ffbfaafb2bf2d43a3af2dddafc3aa4957 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:45:31 +0500 Subject: [PATCH 3/8] chore: Remove pull_request trigger from workflow --- .github/workflows/push-enterprise-subsidy-image.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index 311e5a3..f8fd25b 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -9,10 +9,6 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time - pull_request: - branches: - - '**' - jobs: build-and-push-image: runs-on: ubuntu-latest From a375c3966507eb2942c4a565bb386607f666faf0 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:09:30 +0500 Subject: [PATCH 4/8] chore: updated mail team for failure notification --- .github/workflows/push-enterprise-subsidy-image.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index f8fd25b..cb59967 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -53,6 +53,6 @@ jobs: username: ${{secrets.edx_smtp_username}} password: ${{secrets.edx_smtp_password}} subject: Push Image to docker.io/edxops failed in Enterprise Subsidy - to: team-cosmonauts@edx.org + to: team-titans@edx.org from: github-actions - body: Push Image to docker.io/edxops for Enterprise Subsidy failed! For details see "github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \ No newline at end of file + body: Push Image to docker.io/edxops for Enterprise Subsidy failed! For details see "github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" From 04264cbbd8eebaa5548d50cd62d0e4a6f71d70bd Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:28:14 +0500 Subject: [PATCH 5/8] perf: updated Dockerfile to optimize requirements installation and dependency caching --- .../workflows/push-enterprise-subsidy-image.yaml | 5 +++++ dockerfiles/enterprise-subsidy.Dockerfile | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index cb59967..52541d2 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -9,6 +9,11 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time +# Added for testing purposes. Will remove once the PR is finalised + pull_request: + branches: + - '**' + jobs: build-and-push-image: runs-on: ubuntu-latest diff --git a/dockerfiles/enterprise-subsidy.Dockerfile b/dockerfiles/enterprise-subsidy.Dockerfile index 573c3f2..922e1d8 100644 --- a/dockerfiles/enterprise-subsidy.Dockerfile +++ b/dockerfiles/enterprise-subsidy.Dockerfile @@ -54,7 +54,7 @@ RUN apt-get update && apt-get -qy install --no-install-recommends \ python3-pip \ python${PYTHON_VERSION} \ python${PYTHON_VERSION}-dev \ - python${PYTHON_VERSION}-distutils + python${PYTHON_VERSION}-distutils RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone @@ -65,8 +65,7 @@ RUN rm -rf /var/lib/apt/lists/* RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} RUN pip install virtualenv -# cloning git repo -RUN curl -L https://github.com/openedx/enterprise-subsidy/archive/refs/heads/main.tar.gz | tar -xz --strip-components=1 +RUN mkdir -p requirements # Create a virtualenv for sanity ENV VIRTUAL_ENV=/edx/venvs/enterprise-subsidy @@ -83,11 +82,18 @@ EXPOSE 18280 RUN useradd -m --shell /bin/false app # Dependencies are installed as root so they cannot be modified by the application user. + +RUN curl -L -o requirements/pip.txt https://raw.githubusercontent.com/openedx/enterprise-subsidy/main/requirements/pip.txt +RUN curl -L -o requirements/production.txt https://raw.githubusercontent.com/openedx/enterprise-subsidy/main/requirements/production.txt + RUN pip install -r requirements/pip.txt RUN pip install -r requirements/production.txt RUN mkdir -p /edx/var/log +# cloning git repo +RUN curl -L https://github.com/openedx/enterprise-subsidy/archive/refs/heads/main.tar.gz | tar -xz --strip-components=1 + # Code is owned by root so it cannot be modified by the application user. # So we copy it before changing users. USER app @@ -102,6 +108,7 @@ CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsid FROM app as devstack USER root +RUN curl -L -o requirements/dev.txt https://raw.githubusercontent.com/openedx/enterprise-subsidy/main/requirements/dev.txt RUN pip install -r requirements/dev.txt USER app CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsidy/enterprise_subsidy/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_subsidy.wsgi:application From b78d63fa16d58e60003cbe182fbe415981605241 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:32:04 +0500 Subject: [PATCH 6/8] chore: Remove pull_request trigger from workflow --- .github/workflows/push-enterprise-subsidy-image.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index 52541d2..cb59967 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -9,11 +9,6 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time -# Added for testing purposes. Will remove once the PR is finalised - pull_request: - branches: - - '**' - jobs: build-and-push-image: runs-on: ubuntu-latest From 85a88fd5e862067e2af12af75007f39927e47bfe Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:55:35 +0500 Subject: [PATCH 7/8] refactor: remove redundant curl command --- .github/workflows/push-enterprise-subsidy-image.yaml | 5 +++++ dockerfiles/enterprise-subsidy.Dockerfile | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index cb59967..52541d2 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -9,6 +9,11 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time +# Added for testing purposes. Will remove once the PR is finalised + pull_request: + branches: + - '**' + jobs: build-and-push-image: runs-on: ubuntu-latest diff --git a/dockerfiles/enterprise-subsidy.Dockerfile b/dockerfiles/enterprise-subsidy.Dockerfile index 922e1d8..0249a99 100644 --- a/dockerfiles/enterprise-subsidy.Dockerfile +++ b/dockerfiles/enterprise-subsidy.Dockerfile @@ -108,7 +108,6 @@ CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsid FROM app as devstack USER root -RUN curl -L -o requirements/dev.txt https://raw.githubusercontent.com/openedx/enterprise-subsidy/main/requirements/dev.txt RUN pip install -r requirements/dev.txt USER app CMD gunicorn --workers=2 --name enterprise-subsidy -c /edx/app/enterprise-subsidy/enterprise_subsidy/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_subsidy.wsgi:application From 75861988064f4e8777e448676f640536b38c580f Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:59:37 +0500 Subject: [PATCH 8/8] chore: Remove pull_request trigger from workflow --- .github/workflows/push-enterprise-subsidy-image.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/push-enterprise-subsidy-image.yaml b/.github/workflows/push-enterprise-subsidy-image.yaml index 52541d2..cb59967 100644 --- a/.github/workflows/push-enterprise-subsidy-image.yaml +++ b/.github/workflows/push-enterprise-subsidy-image.yaml @@ -9,11 +9,6 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time -# Added for testing purposes. Will remove once the PR is finalised - pull_request: - branches: - - '**' - jobs: build-and-push-image: runs-on: ubuntu-latest