mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-10-10 06:25:41 +00:00
Add auto-deploys, optimise Dockerfile & add running SHA to RIGS
This commit is contained in:
118
.github/workflows/ci.yml
vendored
118
.github/workflows/ci.yml
vendored
@@ -77,18 +77,25 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
docker:
|
# Each architecture builds on a native runner: emulating arm64 under QEMU (compiling pycairo, npm, uv) was the slow part
|
||||||
name: Docker image
|
docker-build:
|
||||||
runs-on: ubuntu-latest
|
name: Docker build (${{ matrix.platform }})
|
||||||
|
runs-on: ${{ matrix.runner }}
|
||||||
needs: [lint, test]
|
needs: [lint, test]
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- platform: linux/amd64
|
||||||
|
runner: ubuntu-latest
|
||||||
|
- platform: linux/arm64
|
||||||
|
runner: ubuntu-24.04-arm
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
- uses: docker/setup-buildx-action@v3
|
- uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
# Only authenticate when we are actually going to push (never for PRs, which may come from forks)
|
# Only authenticate when we are actually going to push (never for PRs, which may come from forks)
|
||||||
@@ -100,6 +107,60 @@ jobs:
|
|||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- id: build
|
||||||
|
name: Build (and push by digest on master)
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: ${{ matrix.platform }}
|
||||||
|
build-args: GIT_SHA=${{ github.sha }}
|
||||||
|
outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=${{ github.event_name == 'push' }}
|
||||||
|
cache-from: type=gha,scope=${{ matrix.platform }}
|
||||||
|
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
|
||||||
|
|
||||||
|
- name: Export digest
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
env:
|
||||||
|
DIGEST: ${{ steps.build.outputs.digest }}
|
||||||
|
run: |
|
||||||
|
mkdir -p "$RUNNER_TEMP/digests"
|
||||||
|
touch "$RUNNER_TEMP/digests/${DIGEST#sha256:}"
|
||||||
|
|
||||||
|
- name: Upload digest
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: digests-${{ strategy.job-index }}
|
||||||
|
path: ${{ runner.temp }}/digests/*
|
||||||
|
if-no-files-found: error
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
docker:
|
||||||
|
name: Docker image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [docker-build]
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
outputs:
|
||||||
|
tag: ${{ steps.sha-tag.outputs.tag }}
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
path: ${{ runner.temp }}/digests
|
||||||
|
pattern: digests-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log in to GHCR
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- id: meta
|
- id: meta
|
||||||
uses: docker/metadata-action@v5
|
uses: docker/metadata-action@v5
|
||||||
with:
|
with:
|
||||||
@@ -108,13 +169,40 @@ jobs:
|
|||||||
type=raw,value=latest
|
type=raw,value=latest
|
||||||
type=sha
|
type=sha
|
||||||
|
|
||||||
- name: Build (and push on master)
|
- id: sha-tag
|
||||||
uses: docker/build-push-action@v6
|
name: Pick the commit tag
|
||||||
with:
|
env:
|
||||||
context: .
|
TAGS: ${{ steps.meta.outputs.tags }}
|
||||||
platforms: linux/amd64,linux/arm64
|
run: |
|
||||||
push: ${{ github.event_name == 'push' }}
|
ref="$(grep -m1 ':sha-' <<< "$TAGS")"
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
test -n "$ref"
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
echo "tag=${ref##*:}" >> "$GITHUB_OUTPUT"
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
- name: Create multi-arch manifest
|
||||||
|
working-directory: ${{ runner.temp }}/digests
|
||||||
|
env:
|
||||||
|
IMAGE: ghcr.io/${{ github.repository }}
|
||||||
|
run: |
|
||||||
|
docker buildx imagetools create \
|
||||||
|
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
|
||||||
|
$(printf "$IMAGE@sha256:%s " *)
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
name: Deploy
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [docker]
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||||
|
environment: production
|
||||||
|
concurrency: deploy-production
|
||||||
|
steps:
|
||||||
|
- name: Deploy the image built for this commit
|
||||||
|
env:
|
||||||
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||||
|
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||||
|
TAG: ${{ needs.docker.outputs.tag }}
|
||||||
|
run: |
|
||||||
|
install -m 700 -d ~/.ssh
|
||||||
|
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
||||||
|
ssh deploy@rigs.nottinghamtec.co.uk "$TAG"
|
||||||
|
|||||||
@@ -97,6 +97,10 @@ COPY --from=assets --chown=app:app /app/pipeline/built_assets /app/pipeline/buil
|
|||||||
# DEBUG must never be on in a built image; enable it explicitly via the environment if needed
|
# DEBUG must never be on in a built image; enable it explicitly via the environment if needed
|
||||||
ENV DEBUG=false
|
ENV DEBUG=false
|
||||||
|
|
||||||
|
# Declared last so a new commit SHA does not invalidate any of the layers above
|
||||||
|
ARG GIT_SHA=unknown
|
||||||
|
ENV GIT_SHA=$GIT_SHA
|
||||||
|
|
||||||
USER app
|
USER app
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "PyRIGS.wsgi"]
|
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "PyRIGS.wsgi"]
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ SECRET_KEY = env("SECRET_KEY", default="gxhy(a#5mhp289_=6xx$7jh=eh$ymxg^ymc+di*0
|
|||||||
DEBUG = env("DEBUG", cast=bool, default=True)
|
DEBUG = env("DEBUG", cast=bool, default=True)
|
||||||
STAGING = env("STAGING", cast=bool, default=False)
|
STAGING = env("STAGING", cast=bool, default=False)
|
||||||
CI = env("CI", cast=bool, default=False)
|
CI = env("CI", cast=bool, default=False)
|
||||||
|
# Commit the running image was built from (baked in at build time)
|
||||||
|
GIT_SHA = env("GIT_SHA", default="unknown")
|
||||||
|
|
||||||
ALLOWED_HOSTS = [
|
ALLOWED_HOSTS = [
|
||||||
host.strip() for host in env("DJANGO_ALLOWED_HOSTS", default="rigs.nottinghamtec.co.uk").split(",") if host.strip()
|
host.strip() for host in env("DJANGO_ALLOWED_HOSTS", default="rigs.nottinghamtec.co.uk").split(",") if host.strip()
|
||||||
@@ -253,6 +255,7 @@ TEMPLATES = [
|
|||||||
"django.template.context_processors.request",
|
"django.template.context_processors.request",
|
||||||
"django.contrib.messages.context_processors.messages",
|
"django.contrib.messages.context_processors.messages",
|
||||||
"PyRIGS.views.ajax_context",
|
"PyRIGS.views.ajax_context",
|
||||||
|
"PyRIGS.views.version_context",
|
||||||
],
|
],
|
||||||
"debug": DEBUG,
|
"debug": DEBUG,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ def ajax_context(request):
|
|||||||
return {"is_ajax": is_ajax(request)}
|
return {"is_ajax": is_ajax(request)}
|
||||||
|
|
||||||
|
|
||||||
|
def version_context(request):
|
||||||
|
"""Template context processor exposing the running commit to every template."""
|
||||||
|
sha = settings.GIT_SHA
|
||||||
|
return {"git_sha": sha, "git_sha_short": sha[:7]}
|
||||||
|
|
||||||
|
|
||||||
def get_related(
|
def get_related(
|
||||||
form, context
|
form, context
|
||||||
): # Get some other objects to include in the form. Used when there are errors but also nice and quick.
|
): # Get some other objects to include in the form. Used when there are errors but also nice and quick.
|
||||||
|
|||||||
@@ -82,6 +82,16 @@
|
|||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if not is_ajax %}
|
||||||
|
<footer class="container pb-3 text-center text-body-secondary small">
|
||||||
|
{% if git_sha != "unknown" %}
|
||||||
|
Version <a href="https://github.com/nottinghamtec/PyRIGS/commit/{{ git_sha }}" class="text-reset"><code>{{ git_sha_short }}</code></a>
|
||||||
|
{% else %}
|
||||||
|
Version <code>unknown</code>
|
||||||
|
{% endif %}
|
||||||
|
</footer>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="modal fade" id="modal" role="dialog" tabindex=-1></div>
|
<div class="modal fade" id="modal" role="dialog" tabindex=-1></div>
|
||||||
|
|
||||||
<script src="{% static 'js/base.js' %}"></script>
|
<script src="{% static 'js/base.js' %}"></script>
|
||||||
|
|||||||
Reference in New Issue
Block a user