Skip to content

Commit 088eb8c

Browse files
committed
Added copy Spec task.
1 parent 7eac65b commit 088eb8c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

azure/common/deploy-stage.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,18 @@ stages:
285285
spec_file: ${{ parameters.spec_file }}
286286
pr_label: $(PR_LABEL)
287287
aws_account: ${{ parameters.aws_account }}
288+
289+
- bash: |
290+
set -euo pipefail
291+
292+
echo Working: $(Pipeline.Workspace)/s/${{ parameters.service_name }}/$(SERVICE_ARTIFACT_NAME)
293+
echo s3bucket: nhsd-apm-management-${{ parameters.aws_account }}-proxygen
294+
295+
pip install boto3
296+
297+
python copy_spec.py "nhsd-apm-management-${{ parameters.aws_account }}-proxygen" "${{ parameters.service_name }}"
298+
displayName: Copy Spec
299+
workingDirectory: $(Pipeline.Workspace)/s/${{ parameters.service_name }}/$(SERVICE_ARTIFACT_NAME)/utils/scripts
288300
289301
- ${{ if parameters.notify }}:
290302
- template: '../components/update-github-status.yml'

scripts/copy_spec.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
import sys
3+
import boto3
4+
import json
5+
from pathlib import Path
6+
from botocore.exceptions import ClientError
7+
8+
def upload_to_s3(file_path: Path, bucket_name: str, folder_name: str):
9+
"""Upload one file to the given S3 bucket under folder_name/."""
10+
s3 = boto3.client("s3")
11+
key = f"apis/{folder_name}/{file_path}"
12+
13+
try:
14+
s3.upload_file(str(file_path), bucket_name, key)
15+
print(f"[OK] Uploaded → s3://{bucket_name}/apis/{key}")
16+
except ClientError as e:
17+
print(f"[ERROR] Upload failed: {file_path} → s3://{bucket_name}/{key}")
18+
print(e)
19+
sys.exit(1)
20+
21+
22+
def main(bucket_name: str, repo_name: str):
23+
cwd = os.getcwd()
24+
print("Current working directory:", cwd)
25+
26+
root_dir = Path.cwd().parents[1]
27+
json_file = root_dir / f"{repo_name}.json"
28+
minified_json = "spec.json"
29+
30+
print(json_file)
31+
32+
if not json_file.is_file():
33+
print(f"[ERROR] JSON spec not found: {json_file}")
34+
return 1
35+
36+
with open(json_file, "r") as f:
37+
data = json.load(f)
38+
39+
with open(minified_json, "w") as f:
40+
json.dump(data, f, separators=(",", ":"))
41+
42+
upload_to_s3(minified_json, bucket_name, repo_name)
43+
44+
print("[DONE] Processing complete.")
45+
return 0
46+
47+
if __name__ == "__main__":
48+
print("Hitting main")
49+
50+
if len(sys.argv) != 3:
51+
print("Usage: python copy_spec_to_s3.py <s3_bucket_name> <repo_name>")
52+
sys.exit(1)
53+
54+
bucket_name = sys.argv[1]
55+
repo_name = sys.argv[2]
56+
print(f"Repo name: {repo_name}")
57+
print(f"Bucket name: {bucket_name}")
58+
59+
sys.exit(main(bucket_name,repo_name))

0 commit comments

Comments
 (0)