Skip to content

Commit

Permalink
chore: add check on django, python and springboot
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-sartori-zupit committed Sep 29, 2024
1 parent c668ef4 commit e9736c7
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 51 deletions.
56 changes: 50 additions & 6 deletions .github/workflows/django-workflow-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ on:
RUN_ON:
required: false
type: string
default: 'zupit-agents'
default: "zupit-agents"
RUNNERS_CONTAINER_GROUP:
required: false
type: string
default: 'Container'
default: "Container"
RUN:
required: false
type: boolean
Expand All @@ -48,11 +48,46 @@ on:
required: false
type: number
default: 50
CHECK_WORKDIR_CHANGES:
required: true
type: boolean
default: false
CHECK_CUSTOM_DIR:
required: false
type: string
default: ""
CHECK_CHANGES_BY_JOBS:
required: false
type: string
default: "all"
env:
CHECK_DIR: ${{ inputs.WORKING_DIRECTORY }}

jobs:
workdir-has-changes:
runs-on: ubuntu-latest
outputs:
changes-detected: ${{ steps.filter.outputs.changes-detected }}
steps:
- name: Set CHECK_DIR to custom directory if provided
if: ${{ inputs.CHECK_CUSTOM_DIR != '' }}
run: echo "CHECK_DIR=${{ inputs.CHECK_CUSTOM_DIR }}" >> $GITHUB_ENV
- name: Set default CHECK_DIR
if: ${{ inputs.CHECK_CUSTOM_DIR == '' }}
run: echo "CHECK_DIR=${{ inputs.WORKING_DIRECTORY }}" >> $GITHUB_ENV

- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
changes-detected:
- "${{ env.CHECK_DIR }}/**"
django-lint-check:
uses:
./.github/workflows/django-step-lint-check.yml
needs: workdir-has-changes
if: ${{ !inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job)))}}
uses: ./.github/workflows/django-step-lint-check.yml
with:
RUN_ON: ${{inputs.RUN_ON}}
RUNNERS_CONTAINER_GROUP: ${{inputs.RUNNERS_CONTAINER_GROUP}}
Expand All @@ -66,8 +101,9 @@ jobs:
secrets: inherit

django-tests:
uses:
./.github/workflows/django-step-tests.yml
needs: workdir-has-changes
if: ${{ !inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job)))}}
uses: ./.github/workflows/django-step-tests.yml
with:
RUN_ON: ${{inputs.RUN_ON}}
RUNNERS_CONTAINER_GROUP: ${{inputs.RUNNERS_CONTAINER_GROUP}}
Expand All @@ -80,3 +116,11 @@ jobs:
LFS_REPO_PATH: ${{inputs.LFS_REPO_PATH}}
COVERAGE_THRESHOLD: ${{inputs.COVERAGE_THRESHOLD}}
secrets: inherit

jobs-succeded:
needs: ["django-lint-check", "django-tests"]
runs-on: ubuntu-latest
if: ${{ always()}}
steps:
- name: "Jobs: django-lint-check, django-tests didn't fail."
run: if [[ "${{ needs.django-lint-check.result }}" == "failure" || "${{ needs.django-tests.result }}" == "failure" ]]; then exit 1; fi
52 changes: 48 additions & 4 deletions .github/workflows/python-workflow-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ on:
RUN_ON:
required: false
type: string
default: 'zupit-agents'
default: "zupit-agents"
RUNNERS_CONTAINER_GROUP:
required: false
type: string
default: 'Container'
default: "Container"
RUN:
required: false
type: boolean
Expand All @@ -36,11 +36,47 @@ on:
required: false
type: string
default: ""
CHECK_WORKDIR_CHANGES:
required: true
type: boolean
default: false
CHECK_CUSTOM_DIR:
required: false
type: string
default: ""
CHECK_CHANGES_BY_JOBS:
required: false
type: string
default: "all"

env:
CHECK_DIR: ${{ inputs.WORKING_DIRECTORY }}

jobs:
workdir-has-changes:
runs-on: ubuntu-latest
outputs:
changes-detected: ${{ steps.filter.outputs.changes-detected }}
steps:
- name: Set CHECK_DIR to custom directory if provided
if: ${{ inputs.CHECK_CUSTOM_DIR != '' }}
run: echo "CHECK_DIR=${{ inputs.CHECK_CUSTOM_DIR }}" >> $GITHUB_ENV
- name: Set default CHECK_DIR
if: ${{ inputs.CHECK_CUSTOM_DIR == '' }}
run: echo "CHECK_DIR=${{ inputs.WORKING_DIRECTORY }}" >> $GITHUB_ENV

- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
changes-detected:
- "${{ env.CHECK_DIR }}/**"
django-lint-check:
uses:
./.github/workflows/python-step-lint-check.yml
needs: workdir-has-changes
if: ${{ !inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job)))}}
uses: ./.github/workflows/python-step-lint-check.yml
with:
RUN_ON: ${{inputs.RUN_ON}}
RUNNERS_CONTAINER_GROUP: ${{inputs.RUNNERS_CONTAINER_GROUP}}
Expand All @@ -51,3 +87,11 @@ jobs:
ENABLE_LFS: ${{inputs.ENABLE_LFS}}
LFS_REPO_PATH: ${{inputs.LFS_REPO_PATH}}
secrets: inherit

jobs-succeded:
needs: ["django-lint-check"]
runs-on: ubuntu-latest
if: ${{ always()}}
steps:
- name: "Jobs: django-lint-check didn't fail."
run: if [[ "${{ needs.django-lint-check.result }}" == "failure" ]]; then exit 1; fi
64 changes: 53 additions & 11 deletions .github/workflows/springboot-workflow-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,49 @@ on:
DATABASE:
required: false
type: string
description: 'Database to use: postgres or mysql'
default: 'postgres'
description: "Database to use: postgres or mysql"
default: "postgres"
CHECK_WORKDIR_CHANGES:
required: true
type: boolean
default: false
CHECK_CUSTOM_DIR:
required: false
type: string
default: ""
CHECK_CHANGES_BY_JOBS:
required: false
type: string
default: "all"

env: "${{secrets}}"
env:
CHECK_DIR: ${{ inputs.WORKING_DIRECTORY }}

jobs:
workdir-has-changes:
runs-on: ubuntu-latest
outputs:
changes-detected: ${{ steps.filter.outputs.changes-detected }}
steps:
- name: Set CHECK_DIR to custom directory if provided
if: ${{ inputs.CHECK_CUSTOM_DIR != '' }}
run: echo "CHECK_DIR=${{ inputs.CHECK_CUSTOM_DIR }}" >> $GITHUB_ENV
- name: Set default CHECK_DIR
if: ${{ inputs.CHECK_CUSTOM_DIR == '' }}
run: echo "CHECK_DIR=${{ inputs.WORKING_DIRECTORY }}" >> $GITHUB_ENV

- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
changes-detected:
- "${{ env.CHECK_DIR }}/**"
springboot-lint-check:
uses:
./.github/workflows/springboot-step-lint-check.yml
needs: workdir-has-changes
if: ${{ !inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job)))}}
uses: ./.github/workflows/springboot-step-lint-check.yml
with:
JAVA_IMAGE: ${{inputs.JAVA_IMAGE}}
WORKING_DIRECTORY: ${{inputs.WORKING_DIRECTORY}}
Expand All @@ -50,9 +84,9 @@ jobs:
secrets: inherit

springboot-tests:
if: ${{ inputs.DATABASE == 'postgres'}}
uses:
./.github/workflows/springboot-step-tests.yml
needs: workdir-has-changes
if: ${{ inputs.DATABASE == 'postgres' && (!inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job))))}}
uses: ./.github/workflows/springboot-step-tests.yml
with:
JAVA_IMAGE: ${{inputs.JAVA_IMAGE}}
WORKING_DIRECTORY: ${{inputs.WORKING_DIRECTORY}}
Expand All @@ -64,9 +98,9 @@ jobs:
secrets: inherit

springboot-tests-mysql:
if: ${{ inputs.DATABASE == 'mysql'}}
uses:
./.github/workflows/springboot-step-tests-mysql.yml
needs: workdir-has-changes
if: ${{ inputs.DATABASE == 'mysql' && (!inputs.CHECK_WORKDIR_CHANGES || (needs.workdir-has-changes.outputs.changes-detected == 'true' && (inputs.CHECK_CHANGES_BY_JOBS == 'all' || contains(fromJson(inputs.CHECK_CHANGES_BY_JOBS), github.job))))}}
uses: ./.github/workflows/springboot-step-tests-mysql.yml
with:
JAVA_IMAGE: ${{inputs.JAVA_IMAGE}}
WORKING_DIRECTORY: ${{inputs.WORKING_DIRECTORY}}
Expand All @@ -75,3 +109,11 @@ jobs:
COVERAGE_ARTIFACT_NAME: ${{ inputs.COVERAGE_ARTIFACT_NAME }}
RUN: ${{ inputs.RUN }}
secrets: inherit

jobs-succeded:
needs: ["springboot-lint-check", "springboot-tests", "springboot-tests-mysql"]
runs-on: ubuntu-latest
if: ${{ always()}}
steps:
- name: "Jobs: springboot-lint-check, springboot-tests, springboot-tests-mysql didn't fail."
run: if [[ "${{ needs.springboot-lint-check.result }}" == "failure" || "${{ needs.springboot-tests.result }}" == "failure" || "${{ needs.springboot-tests-mysql.result }}" == "failure" ]]; then exit 1; fi
58 changes: 28 additions & 30 deletions docs/CHANGE_DETECTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Add the parameters `CHECK_` parameters configuration to the workflow.

- `CHECK_CUSTOM_DIR`: Specifies a custom DIR to check (use as override dir when the dir to check is differnt from the `WORKING_DIRECTORY`)

- `CHECK_CHANGES_BY_JOBS`: String array that specifies the job id where this dir check is ran. Default is `'all'`. (ex: `"['dotnet-common']"`)
- `CHECK_CHANGES_BY_JOBS`: String array that specifies the job id where this dir check is ran. Default is `'all'`. (ex: `"['pr-backend']"`)

## To implement and use them on custom/local workflows:

1. Add a job (ex: `workdir-has-changes`) that checks for changes on specific directories and outputs a variable (ex: `run-backend`) which stores `true` if it has detected changes or `false` if it didn't

2. Add a job (ex: `pr-backend-success`) that fails when the child steps fails and succeeds when child jobs succeed or get skipped.
2. Add a job (ex: `jobs-succeded`) that fails when the child steps fails and succeeds when child jobs succeed or get skipped.
This will be the job that will be set on the branch protection policy

3. Add a `needs` statement that refers to the check-changes job (ex: `workdir-has-changes`)
Expand All @@ -29,32 +29,30 @@ Add the parameters `CHECK_` parameters configuration to the workflow.

```yml
jobs:
workdir-has-changes:
runs-on: ubuntu-latest
outputs:
run-backend: ${{ steps.filter.outputs.backend }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'Backend/**'
pr-backend:
needs: workdir-has-changes
if: ${{ needs.workdir-has-changes.outputs.run-backend == 'true' }}
uses: ./.github/workflows/common-backend.yml
secrets: inherit

pr-backend-success:
runs-on: ubuntu-latest
needs: pr-backend
if: ${{ always() }}
steps:
- name: Checks success of common-backend.yml.
run: if [ "${{ needs.pr-backend.result }}" == "failure" ]; then exit 1; fi
workdir-has-changes:
runs-on: ubuntu-latest
outputs:
run-backend: ${{ steps.filter.outputs.backend }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'Backend/**'
pr-backend:
needs: workdir-has-changes
if: ${{ needs.workdir-has-changes.outputs.run-backend == 'true' }}
uses: ./.github/workflows/common-backend.yml
secrets: inherit

jobs-succeded:
needs: ["pr-backend"]
runs-on: ubuntu-latest
if: ${{ always()}}
steps:
- name: "Jobs: pr-backend didn't fail."
run: if [[ "${{ needs.pr-backend.result }}" == "failure" ]]; then exit 1; fi
```
<img src="image.png" alt="Example with edits view" width="600"/>
Binary file removed docs/image.png
Binary file not shown.

0 comments on commit e9736c7

Please sign in to comment.