Laptop mit Code und Notizblock
Lucas, Kiya | 28.8.2023

GitLab Review Apps with AWS CDK

Web > GitLab Review Apps with AWS CDK

GitLab Review Apps with AWS CDK

With GitLab Review Apps, an environment can be launched for every merge request. This allows to directly test changes made, without having to check out the MR separately. This results in faster reviews and moreover allows people without programming knowledge, e.g., from marketing, to provide feedback directly on the MR.

Here is how I activated review apps for our homepage.

As we deploy our homepage with the AWS CDK, the process was not too difficult:

  • I had to adjust the cdk.ts to create the stack dynamically based on environment variables:

1const ENV_NAME = process.env.HOMEPAGE_ENV_NAME;
2const ENV_TYPE = process.env.HOMEPAGE_ENV_TYPE;
3
4if (ENV_NAME && ENV_TYPE) {
5  new CdkStack(app, ENV_NAME, {
6    tags: {
7      Project: "Homepage",
8      responsible: "someone@newcubator.com",
9      environment: ENV_TYPE,
10    },
11  });
12} else {
13  throw new Error(
14    "Missing environment variables: HOMEPAGE_ENV_NAME, HOMEPAGE_ENV_TYPE",
15  );
16}
  • Adjust scripts in package.json:

1  "cdk": "cdk",
2  "cdk:deploy": "cdk deploy ${HOMEPAGE_ENV_NAME} --require-approval never --outputs-file ./cdk.out.json",
3  "cdk:destroy": "cdk destroy ${HOMEPAGE_ENV_NAME} --require-approval never",
  • Write a script which parses the output from the CDK to get the deployed URL:

1cat cdk.out.json
2DYNAMIC_ENVIRONMENT_URL=https://$(cat cdk.out.json | jq -r --arg env_name "$HOMEPAGE_ENV_NAME" '.[$env_name].AppRunnerServiceURL')
3
4echo "Deployed successfully to $DYNAMIC_ENVIRONMENT_URL 🚀"
5
6echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
  • Adjust gitlab-ci.yml to create the corresponding pipelines:

1
2.deploy:
3  extends: .default
4  stage: deploy
5  services:
6    - docker:24.0.5-dind
7  variables:
8    DOCKER_HOST: tcp://docker:2375
9  script:
10    - npm run cdk:deploy
11    - ./scripts/parse_cdk_output.sh  # looks in the cdk.out.json for the deployed url
12  artifacts:
13    reports:
14      dotenv: deploy.env              # lets gitlab know to look here for environment variables
15  environment:
16    url: $DYNAMIC_ENVIRONMENT_URL     # after the job, this is fetched from the deploy.env
17
18...
19

Currently, the review app domains default to AWS App Runner domains, for example https://mt2arkm2a4.eu-west-1.awsapprunner.com/. In the future, we could look into custom domains like review-env-name.review.newcubator.com.

Of course, please consider that additional costs will incur due to the additional environments, we will need to monitor this and possibly reduce the lifespan of the environments or only deploy the environments manually.

Content
  • What is a GitLab Review App?
  • How to set up a dynamic environment using AWS CDK?
  • How to adjust scripts in package.json to work with dynamic environment?
  • What is a script to parse the output of CDK?
  • How to adjust gitlab-ci.yml to create corresponding pipelines?
  • What are the additional costs that need to be considered?
Lucas Meurer
Lucas (Softwareentwickler)

... ist mit Leib und Seele vielseitiger Full-Stack-Entwickler am Standort Hannover. Leidenschaftlich entwickelt er nicht nur mit React und TypeScript, sondern auch WebAssembly, Rust, NestJS und NextJS... mehr anzeigen

Github
Kiya

... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund