Skip to content

Commit

Permalink
serverless v4 locally and in CICD
Browse files Browse the repository at this point in the history
  • Loading branch information
finlay-jisc committed Nov 18, 2024
1 parent 6b724eb commit 79a5ec1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: github-actions-api-tests
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}

# Note: this step is only required locally when using act since the ubuntu image
# does not come with docker-compose installed, however the alternative to the reinstall
# here is you'd have to install a custom docker image locally which includes docker-compose
Expand All @@ -29,7 +36,9 @@ jobs:
DATACITE_PASSWORD: ${{ secrets.DATACITE_PASSWORD }}
ORCID_ID: ${{ secrets.ORCID_ID }}
ORCID_SECRET: ${{ secrets.ORCID_SECRET }}
run: docker compose -f "docker-compose.yml" up -d --build
run: |
export SERVERLESS_LICENSE_KEY=`aws ssm get-parameter --name ${{ secrets.SERVERLESS_LICENSE_KEY_SSM_PARAMETER_ARN }} --query "Parameter.Value" --output text`
docker compose -f "docker-compose.yml" up -d --build
- name: Wait for API and DB to be ready
run: docker exec -t api-test dockerize -wait tcp://db:5432 -wait tcp://api-test:4003 -wait tcp://mailpit:8025 -wait tcp://localstack:4566 -timeout 120s
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-int.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
push:
branches:
- "int"
- "OC-321"

permissions:
id-token: write # This is required for requesting the JWT
Expand Down Expand Up @@ -61,4 +60,5 @@ jobs:
- name: Deploy serverless app
working-directory: ./api
run: |
export SERVERLESS_LICENSE_KEY=`aws ssm get-parameter --name ${{ secrets.SERVERLESS_LICENSE_KEY_SSM_PARAMETER_ARN }} --query "Parameter.Value" --output text`
npm run deploy:int
1 change: 1 addition & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ jobs:
- name: Deploy serverless app
working-directory: ./api
run: |
export SERVERLESS_LICENSE_KEY=`aws ssm get-parameter --name ${{ secrets.SERVERLESS_LICENSE_KEY_SSM_PARAMETER_ARN }} --query "Parameter.Value" --output text`
npm run deploy:prod
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To run this application locally you will need:

- [Node v20](https://nodejs.org/en/about/releases/)
- [Docker](https://www.docker.com/)
- [Serverless Framework](https://www.serverless.com/)
- [Serverless Framework v4](https://www.serverless.com/)
- [AWS Credentials File](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)

## Getting started
Expand Down
21 changes: 13 additions & 8 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The Octopus API is a [Prisma](https://www.prisma.io/) project, using [PostgreSQL
- Install [Node](https://github.com/nodejs/node) `v20`.
- Recommended: use [`nvm`](https://github.com/nvm-sh/nvm) for managing Node.js versions.
- Install [Docker](https://docs.docker.com/get-docker).
- Install [Serverless](https://www.serverless.com) `v3`, (`3.37` or higher).
- Install [Serverless](https://www.serverless.com) `v4`.
- Obtain credentials to allow access to the [ORCID Public/Member API](https://info.orcid.org/documentation/integration-guide/registering-a-public-api-client/).
- Obtain credentials to allow access to the [DataCite API](https://support.datacite.org/docs/api).
- Create your environment file as described below.
Expand All @@ -22,16 +22,21 @@ Make sure to update the values within to match your environment.

When adding a new item to the .env file, make sure to update the environment variables in the docker-compose.yml file so the API tests can access them.

### AWS Credentials File
### AWS/Serverless credentials setup

You will need an octopus credential profile to run locally. This can be populated with the dummy data below:
You will need AWS credentials to run the app locally with [serverless-offline](https://www.serverless.com/plugins/serverless-offline). These should be exported as local environment variables, like this:

```bash
[octopus]
aws_access_key_id=xxx
aws_secret_access_key=yyy
aws_session_token=zxcxczcx
```
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_SESSION_TOKEN="..."
```

You will also need to tell the system your serverless license key. This can be done by running (from the api directory) `npx sls login` and following the wizard.

Alternatively you can just set it as an environment variable:

`export SERVERLESS_LICENSE_KEY="..."`

## Getting started

Expand Down
6 changes: 5 additions & 1 deletion api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ services:
- '4005:4003'
command: sh -c "npm run seed:local && npm run dev"
environment:
- DATACITE_USER # this and next 3 come from calling environment
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION
- SERVERLESS_LICENSE_KEY
- DATACITE_USER
- DATACITE_PASSWORD
- ORCID_ID
- ORCID_SECRET
Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"deploy:int": "sls deploy --stage int",
"deploy:prod": "sls deploy --stage prod",
"dev": "concurrently npm:dev:start npm:dev:watch npm:dev:type",
"dev:start": "sls offline start -c serverless-offline.yml --reloadHandler --httpPort 4003 --host 0.0.0.0 --stage local --aws-profile octopus-dev",
"dev:start": "sls offline start -c serverless-offline.yml --reloadHandler --httpPort 4003 --host 0.0.0.0 --stage local",
"dev:type": "tsc --noEmit -w --preserveWatchOutput",
"dev:watch": "swc prisma -d dist --copy-files & swc src -d dist --copy-files -w",
"format:check": "npx prettier --check src/",
Expand Down

0 comments on commit 79a5ec1

Please sign in to comment.