Skip to content

Commit 5d8e960

Browse files
[CAI-710] move sitemap generation (#1941)
* move sitemap sync to script * add changeset --------- Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com>
1 parent 95f22b0 commit 5d8e960

File tree

5 files changed

+74
-27
lines changed

5 files changed

+74
-27
lines changed

.changeset/loose-actors-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-docs": patch
3+
---
4+
5+
Move sitemap generation after the s3 sync

.github/workflows/sync_gitbook_docs.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ jobs:
186186
fi
187187
fi
188188
189+
- name: Sync sitemap to S3
190+
env:
191+
ENVIRONMENT: ${{ inputs.environment }}
192+
S3_BUCKET_NAME: devportal-${{ env.ENV_SHORT }}-website-static-content
193+
SITEMAP_URL: ${{ env.SITEMAP_URL || 'https://developer.pagopa.it/sitemap.xml' }}
194+
run: npm run sync-sitemap -w gitbook-docs
195+
189196
- name: Invalidate CloudFront asset bucket cache
190197
run: |
191198
aws cloudfront create-invalidation \
@@ -311,6 +318,13 @@ jobs:
311318
done
312319
fi
313320
321+
- name: Sync sitemap to S3
322+
env:
323+
ENVIRONMENT: 'prod'
324+
S3_BUCKET_NAME: devportal-p-website-static-content
325+
SITEMAP_URL: 'https://developer.pagopa.it/sitemap.xml'
326+
run: npm run sync-sitemap -w gitbook-docs
327+
314328
- name: Invalidate CloudFront asset bucket cache
315329
run: |
316330
aws cloudfront create-invalidation \

packages/gitbook-docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"parse-docs": "ts-node src/scripts/parseDocUrlsAndIncludes.ts",
2929
"replace-escaped-tokens-with-backticks": "ts-node src/scripts/replaceEscapedTokensWithBackticks.ts",
3030
"sync-all-metadata": "ts-node src/scripts/syncAllMetadata.ts",
31+
"sync-sitemap": "ts-node src/scripts/syncSitemap.ts",
3132
"delete-unused-directories": "ts-node src/scripts/deleteUnusedDirectories.ts"
3233
},
3334
"dependencies": {

packages/gitbook-docs/src/scripts/syncAllMetadata.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
makeS3Client,
1515
putS3File,
1616
} from '../helpers/s3Bucket.helper';
17-
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
17+
import { S3Client } from '@aws-sdk/client-s3';
1818
import { extractTitleFromMarkdown } from '../helpers/extractTitle.helper';
1919
import { fetchFromStrapi } from '../helpers/fetchFromStrapi';
2020
import { MetadataInfo, MetadataType } from '../helpers/guidesMetadataHelper';
@@ -27,7 +27,6 @@ import {
2727
getSyncedSolutionsResponseJsonPath,
2828
} from '../syncedResponses';
2929
import { DOCUMENTATION_PATH } from '../helpers/documentationParsing.helper';
30-
import { baseUrl } from 'nextjs-website/src/config';
3130
import {
3231
StrapiApiData,
3332
StrapiGuide,
@@ -95,9 +94,6 @@ const S3_RELEASE_NOTES_DIRNAMES_JSON_PATH =
9594
process.env.S3_RELEASE_NOTES_DIRNAMES_JSON_PATH ||
9695
'release-notes-dirNames.json';
9796

98-
const SITEMAP_URL = process.env.SITEMAP_URL || `${baseUrl}/sitemap.xml`;
99-
const S3_SITEMAP_PATH = process.env.S3_SITEMAP_PATH || 'sitemap.xml';
100-
10197
const DOCUMENTATION_ABSOLUTE_PATH = path.resolve(DOCUMENTATION_PATH);
10298

10399
interface StrapiData {
@@ -199,19 +195,6 @@ async function fetchAllStrapiData(): Promise<StrapiData> {
199195
};
200196
}
201197

202-
async function fetchSitemapXml(): Promise<string> {
203-
console.log(`Fetching sitemap from ${SITEMAP_URL}...`);
204-
const response = await fetch(SITEMAP_URL);
205-
if (!response.ok) {
206-
// eslint
207-
// eslint-disable-next-line functional/no-throw-statements
208-
throw new Error(
209-
`Failed to fetch sitemap: ${response.status} ${response.statusText}`
210-
);
211-
}
212-
return await response.text();
213-
}
214-
215198
// Generate URL path helper functions
216199
function generateUrlPath(
217200
filePath: string,
@@ -647,15 +630,6 @@ async function main() {
647630
S3_BUCKET_NAME!,
648631
getS3Client()
649632
);
650-
const sitemapXml = await fetchSitemapXml();
651-
652-
await getS3Client().send(
653-
new PutObjectCommand({
654-
Bucket: `${S3_BUCKET_NAME}`,
655-
Key: S3_SITEMAP_PATH,
656-
Body: sitemapXml,
657-
})
658-
);
659633
}
660634

661635
// Save synced responses
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-disable functional/prefer-readonly-type */
2+
/* eslint-disable functional/no-loop-statements */
3+
/* eslint-disable functional/no-expression-statements */
4+
/* eslint-disable functional/immutable-data */
5+
/* eslint-disable functional/no-try-statements */
6+
/* eslint-disable functional/no-let */
7+
8+
import { baseUrl } from 'nextjs-website/src/config';
9+
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
10+
import { makeS3Client } from '../helpers/s3Bucket.helper';
11+
import dotenv from 'dotenv';
12+
13+
// Load environment variables
14+
dotenv.config();
15+
16+
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME;
17+
const SITEMAP_URL = process.env.SITEMAP_URL || `${baseUrl}/sitemap.xml`;
18+
const S3_SITEMAP_PATH = process.env.S3_SITEMAP_PATH || 'sitemap.xml';
19+
20+
let s3Client: S3Client | undefined;
21+
22+
async function fetchSitemapXml(): Promise<string> {
23+
console.log(`Fetching sitemap from ${SITEMAP_URL}...`);
24+
const response = await fetch(SITEMAP_URL);
25+
if (!response.ok) {
26+
// eslint-disable-next-line functional/no-throw-statements
27+
throw new Error(
28+
`Failed to fetch sitemap: ${response.status} ${response.statusText}`
29+
);
30+
}
31+
return await response.text();
32+
}
33+
34+
function getS3Client(): S3Client {
35+
if (!s3Client) {
36+
s3Client = makeS3Client();
37+
}
38+
return s3Client;
39+
}
40+
41+
async function main() {
42+
const sitemapXml = await fetchSitemapXml();
43+
44+
await getS3Client().send(
45+
new PutObjectCommand({
46+
Bucket: `${S3_BUCKET_NAME}`,
47+
Key: S3_SITEMAP_PATH,
48+
Body: sitemapXml,
49+
})
50+
);
51+
}
52+
53+
main();

0 commit comments

Comments
 (0)