From 607592801d36e72e5afd6508948f51a80610b83a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 27 Jan 2023 10:55:43 +0100 Subject: [PATCH] Add github actions & footer --- .../.github/dependabot.yaml | 14 ++++ .../.github/workflows/codeql-analysis.yaml | 80 +++++++++++++++++++ .../.github/workflows/publish.yaml | 52 ++++++++++++ .../.github/workflows/test.yaml | 29 +++++++ .../.pre-commit-config.yaml | 10 +-- .../src/common/templates/common/base.html | 1 + .../src/common/templates/common/footer.html | 10 +++ .../context_processors.py | 5 ++ .../{{cookiecutter.project_slug}}/settings.py | 1 + {{cookiecutter.project_slug}}/tasks.py | 78 ++++-------------- 10 files changed, 212 insertions(+), 68 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/.github/dependabot.yaml create mode 100644 {{cookiecutter.project_slug}}/.github/workflows/codeql-analysis.yaml create mode 100644 {{cookiecutter.project_slug}}/.github/workflows/publish.yaml create mode 100644 {{cookiecutter.project_slug}}/.github/workflows/test.yaml create mode 100644 {{cookiecutter.project_slug}}/src/common/templates/common/footer.html create mode 100644 {{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/context_processors.py diff --git a/{{cookiecutter.project_slug}}/.github/dependabot.yaml b/{{cookiecutter.project_slug}}/.github/dependabot.yaml new file mode 100644 index 0000000..7c4b9c9 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/dependabot.yaml @@ -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" diff --git a/{{cookiecutter.project_slug}}/.github/workflows/codeql-analysis.yaml b/{{cookiecutter.project_slug}}/.github/workflows/codeql-analysis.yaml new file mode 100644 index 0000000..3610541 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/workflows/codeql-analysis.yaml @@ -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 %} diff --git a/{{cookiecutter.project_slug}}/.github/workflows/publish.yaml b/{{cookiecutter.project_slug}}/.github/workflows/publish.yaml new file mode 100644 index 0000000..2354758 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/workflows/publish.yaml @@ -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 diff --git a/{{cookiecutter.project_slug}}/.github/workflows/test.yaml b/{{cookiecutter.project_slug}}/.github/workflows/test.yaml new file mode 100644 index 0000000..c4ada04 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/workflows/test.yaml @@ -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/ diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 09429c7..69b3773 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -34,7 +34,7 @@ repos: - id: django-upgrade args: [--target-version, "4.1"] - repo: https://github.com/PyCQA/isort - rev: v5.11.3 + rev: 5.11.4 hooks: - id: isort args: [--profile, black] @@ -67,16 +67,16 @@ repos: - id: prettier types_or: [javascript, css] - repo: https://github.com/pre-commit/mirrors-eslint - rev: v8.29.0 + rev: v8.32.0 hooks: - id: eslint args: [--fix] types_or: [javascript, css] additional_dependencies: - - eslint@8.29.0 + - eslint@8.32.0 - eslint-config-prettier@8.5.0 - repo: https://github.com/jazzband/pip-tools - rev: 6.11.0 + rev: 6.12.1 hooks: - id: pip-compile name: pip-compile requirements.txt @@ -87,6 +87,6 @@ repos: args: [-q, --allow-unsafe, --resolver=backtracking, --strip-extras, --output-file=constraints.txt, requirements.in] files: ^requirements\.in|constraints\.txt$ - 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] files: ^requirements-dev\.(in|txt)$ diff --git a/{{cookiecutter.project_slug}}/src/common/templates/common/base.html b/{{cookiecutter.project_slug}}/src/common/templates/common/base.html index 462000c..8b1a5b0 100644 --- a/{{cookiecutter.project_slug}}/src/common/templates/common/base.html +++ b/{{cookiecutter.project_slug}}/src/common/templates/common/base.html @@ -8,6 +8,7 @@ {% include "common/hello-random.html" %} + {% include "common/footer.html" %} {% django_htmx_script %} {% if debug %} diff --git a/{{cookiecutter.project_slug}}/src/common/templates/common/footer.html b/{{cookiecutter.project_slug}}/src/common/templates/common/footer.html new file mode 100644 index 0000000..139c314 --- /dev/null +++ b/{{cookiecutter.project_slug}}/src/common/templates/common/footer.html @@ -0,0 +1,10 @@ + diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/context_processors.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/context_processors.py new file mode 100644 index 0000000..18ab923 --- /dev/null +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/context_processors.py @@ -0,0 +1,5 @@ +from django.conf import settings + + +def app(request): + return settings.APP diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/settings.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/settings.py index 7b6b13d..cca80f3 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/settings.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/settings.py @@ -100,6 +100,7 @@ TEMPLATES = [ "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", + "{{cookiecutter.project_slug}}.context_processors.app", ], }, }, diff --git a/{{cookiecutter.project_slug}}/tasks.py b/{{cookiecutter.project_slug}}/tasks.py index 102a108..3371e45 100644 --- a/{{cookiecutter.project_slug}}/tasks.py +++ b/{{cookiecutter.project_slug}}/tasks.py @@ -1,7 +1,6 @@ -import time from pathlib import Path -from invoke import task +from invoke import Context, task BASE_DIR = Path(__file__).parent.resolve(strict=True) SRC_DIR = BASE_DIR / "src" @@ -11,8 +10,10 @@ TEST_ENV = {"ENV_FILE": BASE_DIR / "envs" / "test-envs.env"} @task -def update_dependencies(ctx): - common_args = "-q --allow-unsafe --resolver=backtracking --upgrade" +def sync_dependencies(ctx: Context, update: bool = False): + common_args = "-q --allow-unsafe --resolver=backtracking" + if update: + common_args += " --upgrade" with ctx.cd(BASE_DIR): ctx.run( f"pip-compile {common_args} --generate-hashes requirements.in", @@ -33,25 +34,30 @@ def update_dependencies(ctx): @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): ctx.run("./manage.py makemessages -l en -l fr", pty=True, echo=True) @task -def compilemessages(ctx): +def compilemessages(ctx: Context): with ctx.cd(SRC_DIR): ctx.run("./manage.py compilemessages -l en -l fr", pty=True, echo=True) @task -def test(ctx): +def test(ctx: Context): with ctx.cd(SRC_DIR): ctx.run("pytest", pty=True, echo=True, env=TEST_ENV) @task -def test_cov(ctx): +def test_cov(ctx: Context): with ctx.cd(SRC_DIR): ctx.run( "pytest --cov=. --cov-branch --cov-report term-missing:skip-covered", @@ -62,61 +68,7 @@ def test_cov(ctx): @task -def pre_commit(ctx): - 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): +def download_db(ctx: Context): with ctx.cd(BASE_DIR): ctx.run("scp ubuntu:/mnt/data/{{cookiecutter.project_slug}}/db/db.sqlite3 ./db/db.sqlite3") ctx.run("rm -rf src/media/")