Skip to content

Commit e87b36f

Browse files
sandeshitthenav56
authored andcommitted
feat(ci): add schema validation in ci
1 parent 8ad48e4 commit e87b36f

5 files changed

Lines changed: 51 additions & 117 deletions

File tree

.github/go-api-artifacts-add-or-replace.sh

Lines changed: 0 additions & 72 deletions
This file was deleted.

.github/pull_request_template.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
Addresses xxxxxx
1+
## Addresses
2+
- https://github.com/IFRCGo/go-api/issues/XXXX
3+
- ...
4+
5+
## Related PRs
6+
- https://github.com/IFRCGo/go-api-artifacts/pull/XXXX
7+
- ...
28

39
## Changes
410

@@ -13,4 +19,4 @@ Things that should succeed before merging.
1319

1420
## Release
1521

16-
If there is a version update, make sure to tag the repository with the latest version.
22+
If there is a version update, make sure to tag the repository with the latest version.

.github/workflows/ci.yml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ jobs:
5959

6060
steps:
6161
- uses: actions/checkout@main
62+
with:
63+
submodules: true
6264

6365
- name: Login to GitHub Container Registry
6466
uses: docker/login-action@v3
@@ -139,30 +141,18 @@ jobs:
139141
run: docker compose run --rm serve ./manage.py test --keepdb -v 2 --pattern="test_fake.py"
140142

141143
# NOTE: Schema generation requires a valid database. Therefore, this step must run after "Run Django migrations."
142-
- name: 🕮 Generate latest openapi schema
144+
- name: Validate latest OpenAPI schema.
143145
env:
144146
DOCKER_IMAGE: ${{ steps.prep.outputs.tagged_image }}
145147
DJANGO_DB_NAME: "test_test"
146-
run: |
147-
docker compose run --rm serve ./manage.py spectacular --file openapi-schema-latest.yaml
148148

149-
- name: 🕮 Upload latest openapi schema to IFRCGo/go-api-artifacts
150-
env:
151-
# Github
152-
# NOTE: `GO_API_ARTIFACTS_TOKEN` has an expiry date. Please follow `docs/go-artifacts.md` for token rotation.
153-
GITHUB_TOKEN: ${{ secrets.GO_API_ARTIFACTS_TOKEN }}
154-
GITHUB_OWNER: IFRCGo
155-
GITHUB_REPO: go-api-artifacts
156-
GITHUB_BRANCH: main
157-
# File
158-
SOURCE_FILE_PATH: openapi-schema-latest.yaml
159149
run: |
160-
COMMIT_SHA="${{ github.event.pull_request.head.sha || github.sha }}"
161-
export DEST_FILE_PATH="generated/$COMMIT_SHA/openapi-schema.yaml"
162-
export COMMIT_MESSAGE="Add openapi-schema of $GITHUB_REF_NAME@$COMMIT_SHA"
163-
.github/go-api-artifacts-add-or-replace.sh
164-
DEST_FILE_URL="https://github.com/$GITHUB_OWNER/$GITHUB_REPO/blob/$GITHUB_BRANCH/$DEST_FILE_PATH"
165-
echo "::notice::Uploaded latest openapi-schema to: ${DEST_FILE_URL}"
150+
docker compose run --rm serve ./manage.py spectacular --file openapi-schema-latest.yaml &&
151+
cmp --silent ./assets/openapi-schema.yaml openapi-schema-latest.yaml || {
152+
echo 'The openapi-schema is not up to date with the latest changes. Please update and push latest';
153+
diff ./assets/openapi-schema.yaml openapi-schema-latest.yaml;
154+
exit 1;
155+
}
166156
167157
- name: 🤞 Run Test 🧪
168158
env:

docs/go-artifacts.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,39 @@ https://github.com/IFRCGo/go-api-artifacts
44

55
## What is this?
66

7-
This is a **GitHub repository** used to store and manage **build files**, **generated files**, or **automation results** made by other tools or workflows (like GitHub Actions or CI/CD pipelines).
7+
This is a **GitHub repository** used to store and manage **build files**, **generated files**, or **automation results**.
88

99
- Why use this?
1010
- Keeps the main source code clean by storing big or auto-made files separately.
11-
- Other projects (like go-web-app) can download the required files as required.
12-
13-
## Creating a PAT Token
14-
15-
A GitHub PAT (Personal Access Token) is needed to upload files to the `go-api-artifacts` repository.
16-
This token is stored in the GitHub Actions secret: `secrets.GO_API_ARTIFACTS_TOKEN`.
17-
For more details, see [ci.yml](/.github/workflows/ci.yml).
18-
19-
> [!Important]
20-
> Currently, the PAT token is created using the https://github.com/ifrcgo-dev account.
21-
22-
### How to create a new token:
23-
24-
- **Go to**: [GitHub Personal Access Tokens](https://github.com/settings/personal-access-tokens)
25-
- **Token name**: `ifrc-go-artifacts-xyz` (replace `xyz` as needed)
26-
- **Resource owner**: `IFRCGO`
27-
- **Expiration**: No expiration
28-
- **Repository access**: Only select repositories
29-
- Select: `IFRCGo/go-api-artifacts`
30-
- **Permissions**:
31-
- **Repository permissions**:
32-
- **Contents**: Read and write
11+
- It helps avoid high costs from Git LFS - See https://github.com/IFRCGo/go-api/pull/2470#issuecomment-2861654878
12+
13+
> [!Note]
14+
> Previously, GitHub Actions in go-api or related projects uploaded artifacts automatically from CI using a Personal Access Token (PAT).
15+
> Now, CI only checks the current schema against the schema in **go-api-artifacts**.
16+
> **go-api-artifacts** is used as a submodule in **go-api**.
17+
18+
## Developer Workflow
19+
20+
### openapi-schema.yaml
21+
- Generate Schema File locally:
22+
- The required artifact files (e.g., `openapi-schema.yaml` file) should be generated in the local clone of the go-api-artifacts repository.
23+
- Commit and Push Schema:
24+
- The generated files should be pushed to the go-api-artifacts repository.
25+
- Update Submodule Reference:
26+
- The commit reference should be updated in the main repo.
27+
28+
Command
29+
```bash
30+
docker compose run --rm serve ./manage.py spectacular --file .assets/openapi-schema.yaml
31+
```
32+
33+
### 🚨 Manually added files
34+
35+
- In `go-api-artifacts`
36+
- For any files **other than `openapi-schema.yaml`**
37+
- Create a **new branch** in the `go-api-artifacts` repo.
38+
- Add the files there.
39+
- Open a **Pull Request in the `go-api-artifacts` repository**.
40+
- In `go-api`
41+
- **Update the submodule reference** in your `go-api` PR.
42+
- In the corresponding **`go-api` Pull Request**, include the link to the `go-api-artifacts` PR as a *related PR* so reviewers can track both changes together.

0 commit comments

Comments
 (0)