Add github actions & footer

This commit is contained in:
Gabriel Augendre 2023-01-27 10:55:43 +01:00
parent d5ed71483d
commit 607592801d
10 changed files with 212 additions and 68 deletions

View file

@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

View file

@ -0,0 +1,80 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '35 4 * * 3'
permissions:
contents: read
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'javascript', 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '{{ cookiecutter.python_version }}'
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: {% raw %}${{ matrix.language }}{% endraw %}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: {% raw %}"/language:${{matrix.language}}"{% endraw %}

View file

@ -0,0 +1,52 @@
name: Build, publish & deploy
on:
push:
branches:
- master
permissions:
contents: read
jobs:
tests:
uses: ./.github/workflows/test.yaml
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: [tests]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Log in to Docker Hub
uses: docker/login-action@v2{% raw %}
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}{% endraw %}
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: crocmagnon/{{cookiecutter.project_slug}}:latest
cache-from: type=registry,ref=crocmagnon/{{cookiecutter.project_slug}}:latest
cache-to: type=inline
platforms: linux/amd64
deploy:
name: Deploy new image
runs-on: ubuntu-latest
needs: [push_to_registry]
steps:
- name: Deploy{% raw %}
run: |
TEMP=$(mktemp)
echo "${{ secrets.DEPLOY_KEY }}" > $TEMP
ssh -o StrictHostKeyChecking=no -i $TEMP -p ${{ secrets.DEPLOY_PORT }} ${{ secrets.DEPLOY_USERNAME }}@${{ secrets.DEPLOY_HOST }} /mnt/data/{{cookiecutter.project_slug}}/update{% endraw %}
- name: Check
uses: nick-fields/retry@v2
with:
timeout_seconds: 30
max_attempts: 5
retry_wait_seconds: 2
warning_on_retry: false
command: curl -sSL --fail -m 10 https://{{cookiecutter.project_slug}}.augendre.info | grep ${GITHUB_SHA::7} > /dev/null

View file

@ -0,0 +1,29 @@
name: Test
on:
workflow_call:
pull_request:
branches: [ "master" ]
permissions:
contents: read
jobs:
tests:
name: Python tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '{{cookiecutter.python_version}}'
cache: pip
- name: Install dependencies
run: |
pip install pip-tools
pip-sync requirements.txt requirements-dev.txt
- name: Test
run: pytest --cov=. --cov-branch --cov-report term-missing:skip-covered
working-directory: ./src/

View file

@ -34,7 +34,7 @@ repos:
- id: django-upgrade - id: django-upgrade
args: [--target-version, "4.1"] args: [--target-version, "4.1"]
- repo: https://github.com/PyCQA/isort - repo: https://github.com/PyCQA/isort
rev: v5.11.3 rev: 5.11.4
hooks: hooks:
- id: isort - id: isort
args: [--profile, black] args: [--profile, black]
@ -67,16 +67,16 @@ repos:
- id: prettier - id: prettier
types_or: [javascript, css] types_or: [javascript, css]
- repo: https://github.com/pre-commit/mirrors-eslint - repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.29.0 rev: v8.32.0
hooks: hooks:
- id: eslint - id: eslint
args: [--fix] args: [--fix]
types_or: [javascript, css] types_or: [javascript, css]
additional_dependencies: additional_dependencies:
- eslint@8.29.0 - eslint@8.32.0
- eslint-config-prettier@8.5.0 - eslint-config-prettier@8.5.0
- repo: https://github.com/jazzband/pip-tools - repo: https://github.com/jazzband/pip-tools
rev: 6.11.0 rev: 6.12.1
hooks: hooks:
- id: pip-compile - id: pip-compile
name: pip-compile requirements.txt name: pip-compile requirements.txt
@ -87,6 +87,6 @@ repos:
args: [-q, --allow-unsafe, --resolver=backtracking, --strip-extras, --output-file=constraints.txt, requirements.in] args: [-q, --allow-unsafe, --resolver=backtracking, --strip-extras, --output-file=constraints.txt, requirements.in]
files: ^requirements\.in|constraints\.txt$ files: ^requirements\.in|constraints\.txt$
- id: pip-compile - id: pip-compile
name: pip-compile requirements-dev.in name: pip-compile requirements-dev.txt
args: [-q, --allow-unsafe, --resolver=backtracking, --generate-hashes, requirements-dev.in] args: [-q, --allow-unsafe, --resolver=backtracking, --generate-hashes, requirements-dev.in]
files: ^requirements-dev\.(in|txt)$ files: ^requirements-dev\.(in|txt)$

View file

@ -8,6 +8,7 @@
</head> </head>
<body> <body>
{% include "common/hello-random.html" %} {% include "common/hello-random.html" %}
{% include "common/footer.html" %}
<script src="{% static 'vendor/htmx-1.8.2.min.js' %}" defer></script> <script src="{% static 'vendor/htmx-1.8.2.min.js' %}" defer></script>
{% django_htmx_script %} {% django_htmx_script %}
{% if debug %} {% if debug %}

View file

@ -0,0 +1,10 @@
<footer>
<div>
<span>
Version : {{ build.describe }} &bull; {{ build.date }}
</span>
<div>
<a href="https://github.com/Crocmagnon"><i class="fa-brands fa-github"></i></a>
</div>
</div>
</footer>

View file

@ -0,0 +1,5 @@
from django.conf import settings
def app(request):
return settings.APP

View file

@ -100,6 +100,7 @@ TEMPLATES = [
"django.template.context_processors.request", "django.template.context_processors.request",
"django.contrib.auth.context_processors.auth", "django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages", "django.contrib.messages.context_processors.messages",
"{{cookiecutter.project_slug}}.context_processors.app",
], ],
}, },
}, },

View file

@ -1,7 +1,6 @@
import time
from pathlib import Path from pathlib import Path
from invoke import task from invoke import Context, task
BASE_DIR = Path(__file__).parent.resolve(strict=True) BASE_DIR = Path(__file__).parent.resolve(strict=True)
SRC_DIR = BASE_DIR / "src" SRC_DIR = BASE_DIR / "src"
@ -11,8 +10,10 @@ TEST_ENV = {"ENV_FILE": BASE_DIR / "envs" / "test-envs.env"}
@task @task
def update_dependencies(ctx): def sync_dependencies(ctx: Context, update: bool = False):
common_args = "-q --allow-unsafe --resolver=backtracking --upgrade" common_args = "-q --allow-unsafe --resolver=backtracking"
if update:
common_args += " --upgrade"
with ctx.cd(BASE_DIR): with ctx.cd(BASE_DIR):
ctx.run( ctx.run(
f"pip-compile {common_args} --generate-hashes requirements.in", f"pip-compile {common_args} --generate-hashes requirements.in",
@ -33,25 +34,30 @@ def update_dependencies(ctx):
@task @task
def makemessages(ctx): def update_dependencies(ctx: Context):
return sync_dependencies(ctx, update=True)
@task
def makemessages(ctx: Context):
with ctx.cd(SRC_DIR): with ctx.cd(SRC_DIR):
ctx.run("./manage.py makemessages -l en -l fr", pty=True, echo=True) ctx.run("./manage.py makemessages -l en -l fr", pty=True, echo=True)
@task @task
def compilemessages(ctx): def compilemessages(ctx: Context):
with ctx.cd(SRC_DIR): with ctx.cd(SRC_DIR):
ctx.run("./manage.py compilemessages -l en -l fr", pty=True, echo=True) ctx.run("./manage.py compilemessages -l en -l fr", pty=True, echo=True)
@task @task
def test(ctx): def test(ctx: Context):
with ctx.cd(SRC_DIR): with ctx.cd(SRC_DIR):
ctx.run("pytest", pty=True, echo=True, env=TEST_ENV) ctx.run("pytest", pty=True, echo=True, env=TEST_ENV)
@task @task
def test_cov(ctx): def test_cov(ctx: Context):
with ctx.cd(SRC_DIR): with ctx.cd(SRC_DIR):
ctx.run( ctx.run(
"pytest --cov=. --cov-branch --cov-report term-missing:skip-covered", "pytest --cov=. --cov-branch --cov-report term-missing:skip-covered",
@ -62,61 +68,7 @@ def test_cov(ctx):
@task @task
def pre_commit(ctx): def download_db(ctx: Context):
with ctx.cd(BASE_DIR):
ctx.run("pre-commit run --all-files", pty=True)
@task(pre=[pre_commit, test_cov])
def check(ctx):
pass
@task
def build(ctx):
with ctx.cd(BASE_DIR):
ctx.run(
"docker-compose build django", pty=True, echo=True, env=COMPOSE_BUILD_ENV
)
@task
def publish(ctx):
with ctx.cd(BASE_DIR):
ctx.run(
"docker-compose push django", pty=True, echo=True, env=COMPOSE_BUILD_ENV
)
@task
def deploy(ctx):
ctx.run("ssh ubuntu /mnt/data/checkout/update", pty=True, echo=True)
@task
def check_alive(ctx):
import requests
exception = None
for _ in range(5):
try:
res = requests.get("https://{{cookiecutter.project_slug}}.augendre.info")
res.raise_for_status()
print("Server is up & running")
return
except requests.exceptions.HTTPError as e:
time.sleep(2)
exception = e
raise RuntimeError("Failed to reach the server") from exception
@task(pre=[check, build, publish, deploy], post=[check_alive])
def beam(ctx):
pass
@task
def download_db(ctx):
with ctx.cd(BASE_DIR): with ctx.cd(BASE_DIR):
ctx.run("scp ubuntu:/mnt/data/{{cookiecutter.project_slug}}/db/db.sqlite3 ./db/db.sqlite3") ctx.run("scp ubuntu:/mnt/data/{{cookiecutter.project_slug}}/db/db.sqlite3 ./db/db.sqlite3")
ctx.run("rm -rf src/media/") ctx.run("rm -rf src/media/")