Skip to content

Commit 2b50a0c

Browse files
authored
[45] deploy to AWS uv (#46)
1 parent 395dd43 commit 2b50a0c

9 files changed

Lines changed: 309 additions & 5 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# A test workflow to check that the deploy-to-aws-uv workflow works
2+
name: deploy-to-aws-uv test workflow
3+
4+
on:
5+
# Allows you to run this workflow manually from the Actions tab
6+
workflow_dispatch:
7+
# schedules this to run weekly
8+
# schedule:
9+
# - cron: '5 3 * * 0' # At 03:05 on Sunday
10+
push:
11+
branches: [main]
12+
pull_request:
13+
branches: [main]
14+
15+
jobs:
16+
call-test-workflow:
17+
strategy:
18+
matrix:
19+
node-pkg-manager: [npm, yarn2]
20+
uses: ./.github/workflows/deploy-to-aws-uv.yml
21+
with:
22+
python-version: "3.12"
23+
node-version: 22.22.1
24+
working-directory: samplePythonProjectUv
25+
aws-login: false
26+
smoketest-query: 'query Query {country(code: "NZ") {capital}}'
27+
smoketest-expected: 'Wellington'
28+
node-pkg-manager: ${{ matrix.node-pkg-manager }}
29+
secrets: inherit
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
name: Deploy to AWS (uv)
2+
3+
# Reusable workflow for Python projects deploy to AWS (uv variant)
4+
# supports `npm` or `yarn2` package manager ($PakMan)
5+
# Deploys with `$PakMan run-script deploy`
6+
# Any environment variables required to run `$PakMan run-script deploy` must be specified in the AWS_PROD or AWS_TEST environments or at repo level.
7+
# If using serverless v4, please specify the secret `SERVERLESS_ACCESS_KEY`
8+
9+
10+
# required secrets:
11+
# - AWS_ACCESS_KEY_ID
12+
# - AWS_SECRET_ACCESS_KEY
13+
14+
# supported secrets for deployment step:
15+
# - NZSHM22_KORORAA_API_KEY
16+
# - NZSHM22_TOSHI_API_KEY
17+
# - NZSHM22_NSHM_MODEL_API_KEY
18+
# - NZSHM22_SOLVIS_API_KEY
19+
# - NZSHM22_HAZARD_API_KEY
20+
# - SERVERLESS_ACCESS_KEY
21+
22+
# supported vars for deployment step:
23+
# - NZSHM22_KORORAA_API_URL
24+
# - NZSHM22_TOSHI_API_URL
25+
# - NZSHM22_NSHM_MODEL_API_URL
26+
# - NZSHM22_SOLVIS_API_URL
27+
# - NZSHM22_HAZARD_API_URL
28+
# - ES_HOST (Elastic search specified as an environment variable)
29+
30+
on:
31+
workflow_call:
32+
inputs:
33+
python-version:
34+
description: The Python version to use. If set to "None", no python features are installed.
35+
required: true
36+
type: string
37+
default: '3.11'
38+
uv-version:
39+
description: The uv version to use
40+
required: false
41+
type: string
42+
default: 'latest'
43+
operating-system:
44+
description: The operating system to use
45+
required: false
46+
type: string
47+
default: 'ubuntu-latest'
48+
node-version:
49+
description: The Node version to use
50+
required: false
51+
type: string
52+
default: '22'
53+
environment:
54+
description: If true, will use AWS_PROD or AWS_TEST environments
55+
required: false
56+
type: boolean
57+
default: false
58+
node-pkg-manager:
59+
description: The node package manager to use, either `npm` or `yarn2`
60+
required: true
61+
type: string
62+
default: 'npm'
63+
docker:
64+
description: If true, prerequisites for a docker deployment are run
65+
required: false
66+
type: boolean
67+
default: false
68+
working-directory:
69+
description: The working directory
70+
required: false
71+
type: string
72+
default: .
73+
aws-login:
74+
description: whether to log in to AWS. Should only be false for testing
75+
required: false
76+
type: boolean
77+
default: true
78+
smoketest-url-prod:
79+
description: Used to overwrite the internal URL for smoke tests
80+
required: false
81+
type: string
82+
smoketest-url-test:
83+
description: Used to overwrite the internal URL for smoke tests
84+
required: false
85+
type: string
86+
smoketest-query:
87+
description: A GraphQL query to sendto the deployed API as a smoke test
88+
required: false
89+
type: string
90+
default: "query {about}"
91+
smoketest-expected:
92+
description: A regex to be used for verifying the result of the smoketest
93+
required: false
94+
type: string
95+
default: "data.*about.*Hello"
96+
97+
jobs:
98+
99+
deploy:
100+
runs-on: ${{inputs.operating-system}}
101+
defaults:
102+
run:
103+
shell: bash
104+
working-directory: ${{ inputs.working-directory }}
105+
environment: ${{ (inputs.environment && ((github.ref == 'refs/heads/main') && 'AWS_PROD' || 'AWS_TEST')) || '' }}
106+
steps:
107+
- uses: actions/checkout@v5
108+
109+
- name: Install uv and Python
110+
if: ${{ inputs.python-version != 'None' }}
111+
uses: astral-sh/setup-uv@v6
112+
with:
113+
version: ${{ inputs.uv-version }}
114+
python-version: ${{ inputs.python-version }}
115+
enable-cache: true
116+
cache-dependency-glob: ${{ inputs.working-directory }}/uv.lock
117+
118+
- name: Ensure latest requirements.txt
119+
if: ${{ inputs.docker && inputs.python-version != 'None' }}
120+
run: |
121+
uv export --no-hashes --format requirements-txt > requirements.txt
122+
123+
- name: Use Node.js ${{ inputs.node-version }}
124+
uses: actions/setup-node@v4
125+
with:
126+
node-version: ${{ inputs.node-version }}
127+
check-latest: true
128+
registry-url: https://npm.pkg.github.com/
129+
scope: '@gns-science'
130+
131+
- name: Setup NPM package manager, install and list dependencies
132+
if: ${{ inputs.node-pkg-manager == 'npm' }}
133+
run: |
134+
npm install --location=global npm@latest
135+
npm ci
136+
npm ls
137+
138+
- name: Setup Yarn2 package manager, install and list dependencies
139+
if: ${{ inputs.node-pkg-manager == 'yarn2' }}
140+
run: |
141+
corepack enable
142+
yarn set version berry
143+
yarn install --immutable
144+
yarn info
145+
146+
- name: Configure AWS Credentials
147+
if: ${{ inputs.aws-login }}
148+
uses: aws-actions/configure-aws-credentials@v4
149+
with:
150+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID || secrets.AWS_TOSHI_ACCESS_KEY_ID}}
151+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY || secrets.AWS_TOSHI_SECRET_ACCESS_KEY}}
152+
aws-region: ap-southeast-2
153+
154+
- name: Login to ECR
155+
if: ${{ inputs.docker }}
156+
uses: docker/login-action@v3
157+
with:
158+
registry: 461564345538.dkr.ecr.ap-southeast-2.amazonaws.com
159+
160+
- name: Serverless Deploy (NPM)
161+
env:
162+
# secrets
163+
NZSHM22_KORORAA_API_KEY: ${{ secrets.NZSHM22_KORORAA_API_KEY }}
164+
NZSHM22_TOSHI_API_KEY: ${{ secrets.NZSHM22_TOSHI_API_KEY }}
165+
NZSHM22_NSHM_MODEL_API_KEY: ${{ secrets.NZSHM22_NSHM_MODEL_API_KEY }}
166+
NZSHM22_SOLVIS_API_KEY: ${{ secrets.NZSHM22_SOLVIS_API_KEY }}
167+
NZSHM22_HAZARD_API_KEY: ${{ secrets.NZSHM22_HAZARD_API_KEY }}
168+
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
169+
# vars
170+
NZSHM22_KORORAA_API_URL: ${{ vars.NZSHM22_KORORAA_API_URL }}
171+
NZSHM22_TOSHI_API_URL: ${{ vars.NZSHM22_TOSHI_API_URL }}
172+
NZSHM22_NSHM_MODEL_API_URL: ${{ vars.NZSHM22_NSHM_MODEL_API_URL }}
173+
NZSHM22_SOLVIS_API_URL: ${{ vars.NZSHM22_SOLVIS_API_URL }}
174+
NZSHM22_HAZARD_API_URL: ${{ vars.NZSHM22_HAZARD_API_URL }}
175+
ES_HOST: ${{ vars.ES_HOST }}
176+
177+
if: ${{ inputs.node-pkg-manager == 'npm' }}
178+
run: |
179+
STAGE=${{ (github.ref == 'refs/heads/main') && 'prod' || 'test'}} REGION=ap-southeast-2 npm run-script deploy 2>&1 | tee deploy.out
180+
181+
- name: Serverless Deploy (Yarn2)
182+
env:
183+
# secrets
184+
NZSHM22_KORORAA_API_KEY: ${{ secrets.NZSHM22_KORORAA_API_KEY }}
185+
NZSHM22_TOSHI_API_KEY: ${{ secrets.NZSHM22_TOSHI_API_KEY }}
186+
NZSHM22_NSHM_MODEL_API_KEY: ${{ secrets.NZSHM22_NSHM_MODEL_API_KEY }}
187+
NZSHM22_SOLVIS_API_KEY: ${{ secrets.NZSHM22_SOLVIS_API_KEY }}
188+
NZSHM22_HAZARD_API_KEY: ${{ secrets.NZSHM22_HAZARD_API_KEY }}
189+
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
190+
# vars
191+
NZSHM22_KORORAA_API_URL: ${{ vars.NZSHM22_KORORAA_API_URL }}
192+
NZSHM22_TOSHI_API_URL: ${{ vars.NZSHM22_TOSHI_API_URL }}
193+
NZSHM22_NSHM_MODEL_API_URL: ${{ vars.NZSHM22_NSHM_MODEL_API_URL }}
194+
NZSHM22_SOLVIS_API_URL: ${{ vars.NZSHM22_SOLVIS_API_URL }}
195+
NZSHM22_HAZARD_API_URL: ${{ vars.NZSHM22_HAZARD_API_URL }}
196+
ES_HOST: ${{ vars.ES_HOST }}
197+
198+
if: ${{ inputs.node-pkg-manager == 'yarn2' }}
199+
run: |
200+
STAGE=${{ (github.ref == 'refs/heads/main') && 'prod' || 'test'}} REGION=ap-southeast-2 yarn run deploy 2>&1 | tee deploy.out
201+
202+
- name: Smoke Test
203+
uses: GNS-Science/nshm-github-actions/.github/actions/apiSmokeTest@main
204+
with:
205+
query: ${{ inputs.smoketest-query }}
206+
expected-regex: ${{ inputs.smoketest-expected }}
207+
working-directory: ${{ inputs.working-directory}}
208+
url: ${{ (github.ref == 'refs/heads/main') && inputs.smoketest-url-prod || inputs.smoketest-url-test }}

.github/workflows/deploy-to-aws.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
run: |
120120
poetry self add poetry-plugin-export
121121
122-
- name: Ensure latest requiremments.txt
122+
- name: Ensure latest requirements.txt
123123
if: ${{ inputs.docker && inputs.python-version != 'None'}}
124124
run: |
125125
poetry export --without-hashes --format=requirements.txt > requirements.txt

samplePythonProject/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
"scripts": {
66
"deploy": "poetry run python deploy_check.py --stage ${STAGE} --region ${REGION}"
77
},
8-
"dependencies": {},
9-
"devDependencies": {}
10-
}
8+
"packageManager": "yarn@4.14.1"
9+
}

samplePythonProject/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Manual changes might be lost - proceed with caution!
33

44
__metadata:
5-
version: 8
5+
version: 9
66
cacheKey: 10c0
77

88
"samplepythonproject@workspace:.":
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
# import os
3+
4+
# for name, value in os.environ.items():
5+
# print("{0}: {1}".format(name, value))
6+
7+
stage = sys.argv[2]
8+
region = sys.argv[4]
9+
10+
if stage != "prod" and stage != "test":
11+
print("unexpected stage: " + stage)
12+
sys.exit(1)
13+
14+
if region != "ap-southeast-2":
15+
print("unexpected region: " + region)
16+
sys.exit(1)
17+
18+
print(
19+
20+
"""> nshm-model-graphql-api@0.3.0 deploy
21+
> serverless deploy --stage
22+
23+
24+
api keys:
25+
TempApiKey-nzshm22-model-graphql-api-test: the-totally-real-api-key Api key until we have an auth function
26+
endpoints:
27+
OPTIONS - https://countries.trevorblades.com/graphql
28+
POST - https://fu7kuwh.execute-api.ap-southeast-2.amazonaws.com/test/graphql
29+
GET - https://fu7kuwh.execute-api.ap-southeast-2.amazonaws.com/test/graphql
30+
GET - https://fu7kuwh.execute-api.ap-southeast-2.amazonaws.com/test/graphql/{proxy+}
31+
GET - https://fu7kuwh.execute-api.ap-southeast-2.amazonaws.com/test/static/{proxy+}
32+
functions:
33+
app: nzshm22-model-graphql-api-test-app (18 MB)"""
34+
)

samplePythonProjectUv/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samplePythonProjectUv/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "samplepythonprojectuv",
3+
"version": "0.0.1",
4+
"description": "Used for testing workflows (uv variant).",
5+
"scripts": {
6+
"deploy": "uv run python deploy_check.py --stage ${STAGE} --region ${REGION}"
7+
},
8+
"packageManager": "yarn@4.14.1"
9+
}

samplePythonProjectUv/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 9
6+
cacheKey: 10c0
7+
8+
"samplepythonprojectuv@workspace:.":
9+
version: 0.0.0-use.local
10+
resolution: "samplepythonprojectuv@workspace:."
11+
languageName: unknown
12+
linkType: soft

0 commit comments

Comments
 (0)