Skip to content

Commit 3c1ef4b

Browse files
Update the swagger json github workflow (#359)
* chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * fix(swagger): update the workflow and fix the running issue * fix(swagger): fix the swagger json workflow * chore(swagger): add fixed branch name in workflow * chore(ci): prevent multiple swagger sync PRs by using fixed branch * chore(swagger): add Dev/UAT/Demo servers to OpenAPI config * chore(swagger): avoid default server URLs * chore(swagger): remove field injection and inject URLs into OpenAPI bean
1 parent f60f36e commit 3c1ef4b

3 files changed

Lines changed: 59 additions & 25 deletions

File tree

.github/workflows/swagger-json.yml

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ jobs:
2222
cache: maven
2323

2424
- name: Build API (skip tests)
25-
run: mvn clean package -DskipTests
26-
25+
run: mvn -B clean package -DskipTests
26+
2727
- name: Install jq
2828
run: sudo apt-get update && sudo apt-get install -y jq
2929

@@ -37,19 +37,27 @@ jobs:
3737
3838
- name: Wait for API & fetch Swagger
3939
run: |
40-
for i in {1..30}; do
40+
for i in {1..40}; do
4141
CODE=$(curl --connect-timeout 2 --max-time 5 -s -o swagger_raw.json -w "%{http_code}" http://localhost:9090/v3/api-docs || true)
42+
4243
if [ "$CODE" = "200" ]; then
43-
if jq . swagger_raw.json > common-api.json; then
44-
echo "Swagger generated successfully"
45-
exit 0
46-
else
47-
echo "Failed to parse swagger_raw.json with jq"
44+
jq . swagger_raw.json > common-api.json || {
45+
echo "Swagger JSON invalid"
46+
cat swagger_raw.json
47+
exit 1
48+
}
49+
50+
if [ "$(jq '.paths | length' common-api.json)" -eq 0 ]; then
51+
echo "Swagger paths empty – failing"
4852
exit 1
4953
fi
54+
55+
echo "Swagger generated successfully"
56+
exit 0
5057
fi
58+
5159
echo "Waiting for API... ($i)"
52-
sleep 5
60+
sleep 4
5361
done
5462
5563
echo "Swagger not generated"
@@ -59,31 +67,43 @@ jobs:
5967
- name: Stop API
6068
if: always()
6169
run: |
70+
# Graceful shutdown of the process group
71+
sleep 5
72+
# Force kill the process group if still running
6273
if [ -f api_pid.txt ]; then
63-
kill $(cat api_pid.txt) || true
64-
fi
74+
PID=$(cat api_pid.txt)
75+
kill -TERM -- -"$PID" 2>/dev/null || true
76+
sleep 2
77+
kill -9 -- -"$PID" 2>/dev/null || true
78+
fi
79+
# Fallback: kill any remaining java process on port 9090
80+
fuser -k 9090/tcp 2>/dev/null || true
6581
6682
- name: Checkout AMRIT-Docs
6783
uses: actions/checkout@v4
6884
with:
6985
repository: PSMRI/AMRIT-Docs
7086
token: ${{ secrets.DOCS_REPO_TOKEN }}
7187
path: amrit-docs
88+
fetch-depth: 0
7289

7390
- name: Copy Swagger JSON
7491
run: |
7592
mkdir -p amrit-docs/docs/swagger
7693
cp common-api.json amrit-docs/docs/swagger/common-api.json
7794
95+
# Use a fixed branch name for PRs to avoid accumulating stale PRs.
96+
# This ensures only one open PR is updated per run; delete-branch: true cleans up after merge.
7897
- name: Create Pull Request
79-
uses: peter-evans/create-pull-request@v6
98+
uses: peter-evans/create-pull-request@v8
8099
with:
81100
token: ${{ secrets.DOCS_REPO_TOKEN }}
82101
path: amrit-docs
83-
branch: auto/swagger-update-${{ github.run_id }}
102+
branch: auto/swagger-update-common-api
84103
base: main
85-
commit-message: Auto-update Common-API swagger
86-
title: Auto-update Common-API swagger
104+
commit-message: "chore(docs): auto-update Common-API swagger"
105+
title: "chore(docs): auto-update Common-API swagger"
106+
delete-branch: true
87107
body: |
88-
This PR automatically updates the Common-API Swagger JSON
108+
This PR automatically updates Common-API Swagger JSON
89109
from the latest main branch build.

src/main/java/com/iemr/common/config/SwaggerConfig.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.env.Environment;
56

67
import io.swagger.v3.oas.models.Components;
78
import io.swagger.v3.oas.models.OpenAPI;
@@ -11,14 +12,23 @@
1112

1213
@Configuration
1314
public class SwaggerConfig {
14-
15-
@Bean
16-
public OpenAPI customOpenAPI() {
17-
return new OpenAPI().info(new
18-
Info().title("Common API").version("version").description("A microservice for the creation and management of beneficiaries."))
19-
.addSecurityItem(new SecurityRequirement().addList("my security"))
20-
.components(new Components().addSecuritySchemes("my security",
21-
new SecurityScheme().name("my security").type(SecurityScheme.Type.HTTP).scheme("bearer")));
15+
private static final String DEFAULT_SERVER_URL = "http://localhost:9090";
16+
17+
@Bean
18+
public OpenAPI customOpenAPI(Environment env) {
19+
String devUrl = env.getProperty("api.dev.url", DEFAULT_SERVER_URL);
20+
String uatUrl = env.getProperty("api.uat.url", DEFAULT_SERVER_URL);
21+
String demoUrl = env.getProperty("api.demo.url", DEFAULT_SERVER_URL);
22+
return new OpenAPI()
23+
.info(new Info().title("Common API").version("version").description("A microservice for the creation and management of beneficiaries."))
24+
.addSecurityItem(new SecurityRequirement().addList("my security"))
25+
.components(new Components().addSecuritySchemes("my security",
26+
new SecurityScheme().name("my security").type(SecurityScheme.Type.HTTP).scheme("bearer")))
27+
.servers(java.util.Arrays.asList(
28+
new io.swagger.v3.oas.models.servers.Server().url(devUrl).description("Dev"),
29+
new io.swagger.v3.oas.models.servers.Server().url(uatUrl).description("UAT"),
30+
new io.swagger.v3.oas.models.servers.Server().url(demoUrl).description("Demo")
31+
));
2232
}
2333

2434
}

src/main/resources/application-swagger.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ secondary.datasource.url=jdbc:h2:mem:reportingdb
2525
secondary.datasource.driver-class-name=org.h2.Driver
2626

2727
springdoc.api-docs.enabled=true
28-
springdoc.swagger-ui.enabled=true
28+
springdoc.swagger-ui.enabled=true
29+
30+
api.dev.url=${API_DEV_URL:https://amritwprdev.piramalswasthya.org}
31+
api.uat.url=${API_UAT_URL:https://uatamrit.piramalswasthya.org}
32+
api.demo.url=${API_DEMO_URL:https://amritdemo.piramalswasthya.org}

0 commit comments

Comments
 (0)