diff --git a/.gitea/workflows/build-image.yaml b/.gitea/workflows/build-image.yaml new file mode 100644 index 00000000..ac5a6c12 --- /dev/null +++ b/.gitea/workflows/build-image.yaml @@ -0,0 +1,64 @@ +name: Build Docker Image + +# Trigger manually or when a PR to main is completed. +on: + workflow_dispatch: + pull_request: + branches: + - main + - develop + types: + - closed + +jobs: + build-and-push: + if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }} + runs-on: ubuntu-latest + steps: + # Fetch the repository content for the Docker build context. + - name: Checkout repository + uses: actions/checkout@v4 + + # Enable Buildx for BuildKit features (incl. SSH mount support). + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # Authenticate against the container registry before pushing images. + - name: Log in to container registry + uses: docker/login-action@v3 + with: + registry: git.envipath.com + username: ${{ secrets.CI_REGISTRY_USER }} + password: ${{ secrets.CI_REGISTRY_PASSWORD }} + + # Generate image tags/labels: + # - main branch gets stable "envipy" and "latest" tags + # - every build gets an increasing build number tag + # - branch-prefixed commit SHA tag remains for traceability + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: git.envipath.com/envipath/envipy + tags: | + type=raw,value=envipy,enable=${{ github.ref_name == 'main' }} + type=raw,value=latest,enable=${{ github.ref_name == 'main' }} + type=raw,value=build-${{ github.run_number }} + type=sha,prefix={{branch}}- + + # Load SSH key so Docker can pull private git+ssh dependencies during build. + - name: Setup SSH for private git dependencies + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.ENVIPY_CI_PRIVATE_KEY }} + + # Build and push the production image; forward SSH agent without registry cache reuse. + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile + push: true + ssh: default + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index f2cc2da1..b70039db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -58,6 +58,10 @@ COPY tests tests COPY utilities utilities COPY manage.py . +# Used to run migrations etc +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + # Install frontend deps COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./ @@ -100,4 +104,5 @@ USER django EXPOSE 8000 -CMD ["gunicorn", "envipath.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"] +ENTRYPOINT ["/entrypoint.sh"] +CMD ["gunicorn", "envipath.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "8"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..f9a2b5e1 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e + +if [ "${SKIP_DJANGO_SETUP:-false}" != "true" ]; then + python manage.py migrate --no-input + python manage.py collectstatic --no-input +fi + +exec "$@" \ No newline at end of file