Skip to content

Commit 8ead8db

Browse files
authored
fix: auto-deploy (#1835)
* fix: auto-deploy * fix: add timestamp of build * fix: add id-token permission for oidc * fix: remove graasp in ecr uri * fix: remove commented code * fix: change name of tag to nightly
1 parent 3b56230 commit 8ead8db

10 files changed

Lines changed: 84 additions & 170 deletions

File tree

.github/actions/build-images/action.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ inputs:
88
aws-ecr-uri:
99
description: "AWS ECR URI, similar to 'public.ecr.aws/ecr-default-alias'"
1010
required: true
11+
aws-region:
12+
description: "AWS REGION, similar to 'eu-central-1'"
13+
required: true
1114
aws-role-arn:
1215
description: 'AWS Role to assume'
1316
required: true
@@ -19,7 +22,11 @@ runs:
1922
uses: aws-actions/configure-aws-credentials@v4
2023
with:
2124
role-to-assume: ${{ inputs.aws-role-arn }}
22-
aws-region: us-east-1
25+
aws-region: ${{ inputs.aws-region }}
26+
27+
- name: Auth to the ECR
28+
run: bash ./docker/auth.sh ${{ inputs.aws-ecr-uri }} ${{ inputs.aws-region }}
29+
shell: bash
2330

2431
- name: Build and push the images
2532
run: bash ./docker/build.sh ${{ inputs.aws-ecr-uri }} ${{ inputs.tag }}

.github/workflows/deploy-dev.yml

Lines changed: 11 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,30 @@
11
name: Deploy to development environment
22

3-
# Controls when the action will run.
43
on:
5-
# Triggers the workflow on push events only for the main branch
6-
# push:
7-
# branches:
8-
# - main
4+
push:
5+
branches:
6+
- main
97

108
# Allows you to run this workflow manually from the Actions tab
119
workflow_dispatch:
1210

13-
env:
14-
NODE_OPTIONS: '--max_old_space_size=8192' # Allows to increase Node's max heap size
11+
permissions:
12+
id-token: write # This is required for requesting the JWT for OIDC
1513

1614
jobs:
17-
# Build image and push to ECR
1815
build:
1916
name: Build & Push Image
2017
runs-on: ubuntu-latest
21-
# Define job output that is available to all downstream jobs that depend on this job
22-
outputs:
23-
image_url: ${{ steps.image-url.outputs.image_url }}
2418
environment: development
2519

2620
steps:
27-
# Check-out repository under $GITHUB_WORKSPACE, so the job can access it
28-
- name: Check out code
21+
- name: Checkout code
2922
uses: actions/checkout@v4
3023

31-
# Configure AWS credential and region environment variables for use in next steps
32-
- name: Configure AWS Credentials
33-
id: configure-aws
34-
uses: aws-actions/configure-aws-credentials@v4
24+
- name: Build images
25+
uses: ./.github/actions/build-images
3526
with:
36-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }} # todo: add in envs
37-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }} # todo: add in envs
27+
tag: nightly
28+
aws-ecr-uri: ${{ vars.PRIVATE_ECR }}
29+
aws-role-arn: ${{ vars.AWS_ROLE_ARN }}
3830
aws-region: ${{ vars.AWS_REGION }}
39-
40-
# Log in the local Docker client
41-
- name: Login to Amazon ECR
42-
id: login-ecr-build
43-
uses: aws-actions/amazon-ecr-login@v2
44-
45-
# Set output variable tag with the current checked out ref
46-
- name: Set Image Url
47-
id: image-url
48-
env:
49-
TARGET_IMAGE_URL: ${{ steps.login-ecr-build.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.ref_name || github.ref || github.sha }}
50-
run: echo "image_url=${TARGET_IMAGE_URL}" >> $GITHUB_OUTPUT
51-
52-
# Build and tag the docker image
53-
- name: Build, tag and push image to AWS ECR
54-
id: build-image
55-
env:
56-
TARGET_IMAGE_URL: ${{ steps.image-url.outputs.image_url }}
57-
run: |
58-
docker build -t $TARGET_IMAGE_URL -f docker/Dockerfile --build-arg APP_VERSION=${{ github.sha }} .
59-
docker push $TARGET_IMAGE_URL
60-
61-
# Deploy to dev environment
62-
deploy:
63-
needs: build
64-
name: Deploy
65-
runs-on: ubuntu-latest
66-
environment: development
67-
68-
steps:
69-
- name: Check out code
70-
uses: actions/checkout@v4
71-
72-
# Configure AWS credential and region environment variables for use in next steps
73-
- name: Configure AWS credentials
74-
uses: aws-actions/configure-aws-credentials@v4
75-
with:
76-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }} # todo: add in envs
77-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }} # todo: add in envs
78-
aws-region: ${{ vars.AWS_REGION }}
79-
80-
# Log in the local Docker client
81-
- name: Login to Amazon ECR
82-
id: login-ecr-deploy
83-
uses: aws-actions/amazon-ecr-login@v2
84-
85-
# Insert a container image URI into template Amazon ECS task definition JSON file, creating a new task definition file.
86-
- name: Fill in the new image ID in the Amazon ECS task definition
87-
id: task-def-1
88-
uses: aws-actions/amazon-ecs-render-task-definition@v1
89-
with:
90-
task-definition: ${{ vars.ECS_TASK_DEFINITION }}
91-
container-name: ${{ vars.CONTAINER_NAME_GRAASP }}
92-
image: ${{ needs.build.outputs.image_url }}
93-
environment-variables: |
94-
DB_CONNECTION_POOL_SIZE=${{ vars.DB_CONNECTION_POOL_SIZE }}
95-
APPS_JWT_SECRET=${{ secrets.APPS_JWT_SECRET }}
96-
APPS_PUBLISHER_ID=${{ secrets.APPS_PUBLISHER_ID }}
97-
AUTH_TOKEN_EXPIRATION_IN_MINUTES=${{ secrets.AUTH_TOKEN_EXPIRATION_IN_MINUTES }}
98-
AUTH_TOKEN_JWT_SECRET=${{ secrets.AUTH_TOKEN_JWT_SECRET }}
99-
CLIENT_HOST=${{ vars.CLIENT_HOST }}
100-
COOKIE_DOMAIN=${{ vars.COOKIE_DOMAIN }}
101-
CORS_ORIGIN_REGEX=${{ secrets.CORS_ORIGIN_REGEX }}
102-
DB_CONNECTION=postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:5432/${{ secrets.DB_NAME }}
103-
104-
DB_READ_REPLICA_CONNECTIONS=${{ secrets.DB_READ_REPLICA_CONNECTIONS }}
105-
SENTRY_ENV=${{ vars.SENTRY_ENV }}
106-
EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN=${{ vars.EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN }}
107-
ETHERPAD_API_KEY=${{ secrets.ETHERPAD_API_KEY }}
108-
ETHERPAD_COOKIE_DOMAIN=${{ vars.ETHERPAD_COOKIE_DOMAIN }}
109-
ETHERPAD_URL=${{ vars.ETHERPAD_URL }}
110-
FILE_STORAGE_ROOT_PATH=${{ secrets.FILE_STORAGE_ROOT_PATH }}
111-
H5P_CONTENT_ACCESS_KEY_ID=${{ secrets.H5P_CONTENT_ACCESS_KEY_ID }}
112-
H5P_CONTENT_BUCKET=${{ vars.H5P_CONTENT_BUCKET }}
113-
H5P_CONTENT_REGION=${{ vars.H5P_CONTENT_REGION }}
114-
H5P_CONTENT_SECRET_ACCESS_KEY_ID=${{ secrets.H5P_CONTENT_SECRET_ACCESS_KEY }}
115-
H5P_FILE_STORAGE_TYPE=${{ vars.H5P_FILE_STORAGE_TYPE }}
116-
H5P_PATH_PREFIX=${{ vars.H5P_PATH_PREFIX }}
117-
HOSTNAME=${{ vars.HOSTNAME }}
118-
IMAGE_CLASSIFIER_API=${{ vars.IMAGE_CLASSIFIER_API }}
119-
JOB_SCHEDULING=${{ vars.JOB_SCHEDULING || false }}
120-
JWT_SECRET=${{ secrets.JWT_SECRET }}
121-
PASSWORD_RESET_JWT_SECRET=${{ secrets.PASSWORD_RESET_JWT_SECRET }}
122-
EMAIL_CHANGE_JWT_SECRET=${{ secrets.EMAIL_CHANGE_JWT_SECRET }}
123-
LIBRARY_CLIENT_HOST=${{ vars.LIBRARY_CLIENT_HOST }}
124-
LOG_LEVEL=${{ vars.LOG_LEVEL }}
125-
MAILER_CONFIG_FROM_EMAIL=${{ secrets.MAILER_CONFIG_FROM_EMAIL }}
126-
MAILER_CONFIG_PASSWORD=${{ secrets.MAILER_CONFIG_PASSWORD_DEV }}
127-
MAILER_CONFIG_SMTP_HOST=${{ secrets.MAILER_CONFIG_SMTP_HOST }}
128-
MAILER_CONFIG_USERNAME=${{ secrets.MAILER_CONFIG_USERNAME }}
129-
MEILISEARCH_MASTER_KEY=${{ secrets.MEILISEARCH_MASTER_KEY }}
130-
MEILISEARCH_REBUILD_SECRET=${{ secrets.MEILISEARCH_REBUILD_SECRET }}
131-
MEILISEARCH_URL=${{ secrets.MEILISEARCH_URL }}
132-
NODE_ENV=${{ vars.NODE_ENV }}
133-
PORT=${{ vars.PORT }}
134-
PUBLIC_URL=${{ secrets.PUBLIC_URL }}
135-
RECAPTCHA_SECRET_ACCESS_KEY=${{ secrets.RECAPTCHA_SECRET_ACCESS_KEY }}
136-
REDIS_HOST=${{ secrets.REDIS_HOST }}
137-
REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}
138-
REDIS_PORT=${{ secrets.REDIS_PORT }}
139-
REDIS_USERNAME=${{ secrets.REDIS_USERNAME }}
140-
REFRESH_TOKEN_EXPIRATION_IN_MINUTES=${{ vars.REFRESH_TOKEN_EXPIRATION_IN_MINUTES }}
141-
REFRESH_TOKEN_JWT_SECRET=${{ secrets.REFRESH_TOKEN_JWT_SECRET }}
142-
S3_FILE_ITEM_ACCESS_KEY_ID=${{ secrets.S3_FILE_ITEM_ACCESS_KEY_ID }}
143-
S3_FILE_ITEM_BUCKET=${{ vars.S3_FILE_ITEM_BUCKET }}
144-
S3_FILE_ITEM_PLUGIN=${{ vars.S3_FILE_ITEM_PLUGIN }}
145-
S3_FILE_ITEM_REGION=${{ vars.S3_FILE_ITEM_REGION }}
146-
S3_FILE_ITEM_SECRET_ACCESS_KEY=${{ secrets.S3_FILE_ITEM_SECRET_ACCESS_KEY }}
147-
SECURE_SESSION_SECRET_KEY=${{ secrets.SECURE_SESSION_SECRET_KEY }}
148-
SENTRY_DSN=${{ secrets.SENTRY_DSN }}
149-
WEBSOCKETS_PLUGIN=${{ vars.WEBSOCKETS_PLUGIN }}
150-
OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
151-
OPENAI_ORG_ID=${{ secrets.OPENAI_ORG_ID }}
152-
GEOLOCATION_API_HOST=${{ vars.GEOLOCATION_API_HOST }}
153-
GEOLOCATION_API_KEY=${{ secrets.GEOLOCATION_API_KEY }}
154-
155-
# Use latest revision of the task-definition to deploy the application to ECS
156-
- name: Deploy Amazon ECS task definition
157-
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
158-
with:
159-
task-definition: ${{ steps.task-def-1.outputs.task-definition }}
160-
service: ${{ vars.ECS_SERVICE_GRAASP }}
161-
cluster: ${{ vars.ECS_CLUSTER_GRAASP }}
162-
wait-for-service-stability: true

.github/workflows/deploy-prod.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,8 @@ jobs:
103103
CLIENT_HOST=${{ vars.CLIENT_HOST }}
104104
COOKIE_DOMAIN=${{ vars.COOKIE_DOMAIN }}
105105
CORS_ORIGIN_REGEX=${{ secrets.CORS_ORIGIN_REGEX }}
106-
DB_HOST=${{ secrets.DB_HOST }}
107-
DB_NAME=${{ secrets.DB_NAME }}
108-
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
106+
DB_CONNECTION=postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:5432/${{ secrets.DB_NAME }}
109107
DB_READ_REPLICA_CONNECTIONS=${{ secrets.DB_READ_REPLICA_CONNECTIONS }}
110-
DB_USERNAME=${{ secrets.DB_USERNAME }}
111108
SENTRY_ENV=${{ vars.SENTRY_ENV }}
112109
EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN=${{ vars.EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN }}
113110
ETHERPAD_API_KEY=${{ secrets.ETHERPAD_API_KEY }}

.github/workflows/release-please.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
tag: ${{ steps.release.outputs.tag_name }}
3333
aws-ecr-uri: ${{ vars.GRAASP_PUBLIC_ECR }}
3434
aws-role-arn: ${{ vars.AWS_ROLE_ARN }}
35+
aws-region: us-east-1
3536

3637
# ------ Legacy behavior ---
3738
# put created tag in an env variable to be sent to the dispatch

docker/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ FROM node:${NODE_VERSION}-bookworm-slim AS runner
3434
ARG APP_VERSION
3535
# Set APP_VERSION as ENV variable from ARG passed at build step
3636
ENV APP_VERSION=${APP_VERSION:-latest}
37+
# Set BUILD_TIMESTAMP as ENV variable from ARG passed at build step
38+
ARG BUILD_TIMESTAMP
39+
ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP:-not-provided}
3740
# Set NODE_ENV to production so we don't trigger .husky/install.mjs
3841
ENV NODE_ENV=production
3942

docker/auth.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
function show_help() {
4+
echo "This script allows to auth to the ECR service"
5+
echo "Usage: bash docker/auth.sh ECR_URI REGION"
6+
echo "Arguments:"
7+
echo " ECR_URI URI of the ECR in AWS, example: public.ecr.aws/qzuer78 or a private ECR XXXXXXXX.dkr.ecr.region.amazonaws.com/repo."
8+
echo " REGION REGION of the ECR in AWS, example: eu-central-1"
9+
}
10+
11+
# Check if no arguments are provided
12+
if [ "$#" -eq 0 ]; then
13+
show_help
14+
exit 1
15+
fi
16+
17+
# Get inputs from command line
18+
if [ -z "$1" ]; then
19+
echo "Missing required ECR_URI argument"
20+
exit 1
21+
fi
22+
aws_ecr_uri=$1
23+
24+
# Get inputs from command line
25+
if [ -z "$2" ]; then
26+
echo "Missing required REGION argument"
27+
exit 1
28+
fi
29+
aws_region=$2
30+
31+
# Check if the aws cli is authenticated
32+
aws_identity=$(aws sts get-caller-identity)
33+
if [ $? -eq 0 ]; then
34+
echo -e "Using AWS identity:\n$aws_identity"
35+
else
36+
echo "AWS CLI is not authenticated, please ensure the cli is authenticated before running this script";
37+
exit 1
38+
fi
39+
40+
if [[ $aws_ecr_uri == public.ecr* ]]; then
41+
echo "Using Public ECR"
42+
ecr_credentials=$(aws ecr-public get-login-password --region us-east-1)
43+
else
44+
echo "Using Private ECR"
45+
ecr_credentials=$(aws ecr get-login-password --region $aws_region)
46+
fi
47+
48+
if [ -z $ecr_credentials ]; then
49+
echo "The AWS credentials were not received"
50+
echo "Please check that the ECR_URI is correct and that you are logged into an account or role that is allowed to use that ECR"
51+
exit 1
52+
fi
53+
# login the docker client with the ECR credentials derived from the currently authenticated user
54+
echo $ecr_credentials | docker login --username AWS --password-stdin $aws_ecr_uri

docker/build.sh

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function show_help() {
44
echo "This script allows to build and push the docker images for the core and the migration"
55
echo "Usage: bash docker/build.sh ECR_URI VERSION"
66
echo "Arguments:"
7-
echo " ECR_URI URI of the ECR in AWS, example: public.ecr.aws/qzuer78"
7+
echo " ECR_URI URI of the ECR in AWS, example: public.ecr.aws/qzuer78 or a private ECR XXXXXXXX.dkr.ecr.region.amazonaws.com/repo."
88
echo " VERSION The version that is deployed, should be a semantic version i.e 1.45.8"
99
}
1010

@@ -27,32 +27,14 @@ if [ -z "$2" ]; then
2727
fi
2828
tag_version=$2
2929

30-
# Check if the aws cli is authenticated
31-
aws_identity=$(aws sts get-caller-identity)
32-
if [ $? -eq 0 ]; then
33-
echo -e "Using AWS identity:\n$aws_identity"
34-
else
35-
echo "AWS CLI is not authenticated, please ensure the cli is authenticated before running this script";
36-
exit 1
37-
fi
38-
39-
ecr_credentials=$(aws ecr-public get-login-password --region us-east-1)
40-
if [ -z $ecr_credentials ]; then
41-
echo "The AWS credentials were not received"
42-
echo "Please check that the ECR_URI is correct and that you are logged into an account or role that is allowed to use that ECR"
43-
exit 1
44-
fi
45-
# login the docker client with the ECR credentials derived from the currently authenticated user
46-
echo $ecr_credentials | docker login --username AWS --password-stdin $aws_ecr_uri
47-
4830
# define the image tags including the ecr uri
4931
core_tag_short="graasp:core-$tag_version"
5032
core_tag_full="$aws_ecr_uri/$core_tag_short"
5133

5234
migrate_tag_short="graasp:migrate-$tag_version"
5335
migrate_tag_full="$aws_ecr_uri/$migrate_tag_short"
5436

55-
docker build -t $core_tag_full -f docker/Dockerfile --platform linux/amd64 --build-arg APP_VERSION=$tag_version .
37+
docker build -t $core_tag_full -f docker/Dockerfile --platform linux/amd64 --build-arg APP_VERSION=$tag_version --build-arg BUILD_TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%S) .
5638
docker push $core_tag_full
5739

5840
docker build -t $migrate_tag_full -f docker/migrate.Dockerfile --platform linux/amd64 .

docker/compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ services:
126126
# copy the init script inside the docker container where it will be executed
127127
# WARNING: It will only be executed when there is no data mounted to the container
128128
# If you want to execute it, down the container, delete the volume associated to it and up the container again, it should execute.
129-
- ../bootstrapDB.sql:/docker-entrypoint-initdb.d/init.sql
129+
- ../.devcontainer/postgresql:/docker-entrypoint-initdb.d
130130
environment:
131131
POSTGRES_DB: docker
132132
POSTGRES_USER: docker

src/plugins/meta.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SearchService } from '../services/item/plugins/publication/published/pl
1212
import { assertIsError } from '../utils/assertions';
1313
import {
1414
APP_VERSION,
15+
BUILD_TIMESTAMP,
1516
EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN,
1617
ETHERPAD_URL,
1718
} from '../utils/config';
@@ -112,7 +113,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
112113
fastify.get('/version', async (_, reply) => {
113114
// allow request cross origin
114115
reply.header('Access-Control-Allow-Origin', '*');
115-
return APP_VERSION;
116+
return `${APP_VERSION} @ ${BUILD_TIMESTAMP}`;
116117
});
117118
};
118119

src/utils/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const DEV = ENVIRONMENT === Environment.development;
4747
export const TEST = ENVIRONMENT === Environment.test;
4848

4949
export const APP_VERSION = process.env.APP_VERSION;
50+
export const BUILD_TIMESTAMP = process.env.BUILD_TIMESTAMP;
5051

5152
export const CLIENT_HOST = process.env.CLIENT_HOST ?? 'http://localhost:3114';
5253

0 commit comments

Comments
 (0)