diff --git a/.github/workflows/create-and-publish-docker-images.yml b/.github/workflows/create-and-publish-docker-images.yml new file mode 100644 index 00000000..658a2459 --- /dev/null +++ b/.github/workflows/create-and-publish-docker-images.yml @@ -0,0 +1,69 @@ +# source: https://docs.github.com/en/actions/tutorials/publish-packages/publish-docker-images#publishing-images-to-docker-hub-and-github-packages +name: Create and publish a Docker image + +# manually trigger while testing +on: + workflow_dispatch + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `PACKAGE_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v5 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.PACKAGE_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Set lowercase image name + run: | + IMAGE_NAME=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]') + echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Format repo slug + uses: actions/github-script@v8 + id: repo_slug + with: + result-encoding: string + script: | + return `ghcr.io/${process.env.GITHUB_REPOSITORY.toLowerCase()}` + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see [Usage](https://github.com/docker/build-push-action#usage) in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@v6 + with: + context: ./TEKDB + file: ./TEKDB/prod.Dockerfile + push: true + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/web:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/web:${{ github.sha }} + labels: ${{ steps.meta.outputs.labels }} + + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see [Using artifact attestations to establish provenance for builds](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds). + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v3 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true + diff --git a/.gitignore b/.gitignore index c5ba5117..c77ac2b2 100644 --- a/.gitignore +++ b/.gitignore @@ -86,6 +86,7 @@ celerybeat-schedule # dotenv .env +.env.prod # virtualenv venv/ diff --git a/TEKDB/prod.Dockerfile b/TEKDB/prod.Dockerfile new file mode 100644 index 00000000..ca2d2e24 --- /dev/null +++ b/TEKDB/prod.Dockerfile @@ -0,0 +1,46 @@ +FROM python:3.11-slim + +# Prevent Python from writing .pyc files and enable unbuffered stdout/stderr +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +ENV PIP_NO_CACHE_DIR=1 + +# Install system dependencies +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + postgresql-client \ + gcc \ + gdal-bin \ + libgdal-dev \ + libgeos-dev \ + && rm -rf /var/lib/apt/lists/* + +# Set working directory +WORKDIR /usr/src/app + +# Copy requirements first (cache pip install step when dependencies don't change) +COPY requirements.txt requirements_linux.txt /usr/src/app/ + +# Upgrade pip and install Python dependencies +# Note: editable packages (-e) will be installed at runtime via entrypoint.sh +RUN pip install --upgrade pip \ + && pip install -r requirements.txt -r requirements_linux.txt + +# Copy the application code +COPY . /usr/src/app + +# Copy and make entrypoint executable. The repository contains `docker/entrypoint.sh` +# which runs collectstatic, migrations and launches uWSGI. +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Expose the port the app runs on (entrypoint starts django development server or uWSGI on 8000) +EXPOSE 8000 + +# Default settings module (can be overridden at runtime) +ENV DJANGO_SETTINGS_MODULE=TEKDB.settings + +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] + +# use prod server in prod Dockerfile +CMD ["prod"] \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/common.yaml similarity index 94% rename from docker/docker-compose.yml rename to docker/common.yaml index f865e83a..3ee08bef 100644 --- a/docker/docker-compose.yml +++ b/docker/common.yaml @@ -16,7 +16,7 @@ services: interval: 10s timeout: 5s retries: 5 - + web: build: context: ../TEKDB/ @@ -39,7 +39,4 @@ services: ports: - "8000:8000" volumes: - - ../TEKDB:/usr/src/app - -volumes: - tekdb_db_data: + - ../TEKDB:/usr/src/app \ No newline at end of file diff --git a/docker/docker-compose.prod.yaml b/docker/docker-compose.prod.yaml index 872e7c53..c063379b 100644 --- a/docker/docker-compose.prod.yaml +++ b/docker/docker-compose.prod.yaml @@ -1,46 +1,16 @@ services: db: - image: postgis/postgis:15-3.4 - restart: always - platform: linux/amd64 - environment: - POSTGRES_DB: ${SQL_DATABASE} - POSTGRES_USER: ${SQL_USER} - POSTGRES_PASSWORD: ${SQL_PASSWORD} - volumes: - - tekdb_db_data:/var/lib/postgresql/data - ports: - - "5432:5432" - healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${SQL_USER} -d ${SQL_DATABASE} -h localhost -p ${SQL_PORT}"] - interval: 10s - timeout: 5s - retries: 5 + extends: + file: common.yaml + service: db web: - build: - context: ../TEKDB/ - dockerfile: ../TEKDB/Dockerfile + extends: + file: common.yaml + service: web command: ["prod"] - restart: unless-stopped - depends_on: - - db env_file: - - .env.dev - environment: - ALLOWED_HOSTS: ${ALLOWED_HOSTS} - DEBUG: ${DEBUG} - SQL_ENGINE: ${SQL_ENGINE} - SQL_HOST: ${SQL_HOST} - SQL_PORT: ${SQL_PORT} - SQL_DATABASE: ${SQL_DATABASE} - SQL_USER: ${SQL_USER} - SQL_PASSWORD: ${SQL_PASSWORD} - SECRET_KEY: ${SECRET_KEY} - ports: - - "8000:8000" - volumes: - - ../TEKDB:/usr/src/app + - .env.prod volumes: tekdb_db_data: diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml new file mode 100644 index 00000000..5a99ff86 --- /dev/null +++ b/docker/docker-compose.yaml @@ -0,0 +1,13 @@ +services: + db: + extends: + file: common.yaml + service: db + + web: + extends: + file: common.yaml + service: web + +volumes: + tekdb_db_data: