Automated preview environments

One of the largest benefits of Architects framework is that provisioning new environments is always limited to a single step, architect deploy. No matter how complex the application is or how many dependencies it has, architect deploy is able to automatically provision it in a new environment.

What this means is that not only can developers run the stack privately, but the stack can also be provisioned automatically whenever there is a new branch or pull request. This automation is perfect for creating previews of impending code changes so that product managers can review and integration tests can be run end to end.

Create preview environment

Github Actions

The workflow below can be pasted into a file in your repository in the .github folder to trigger automated preview environments via Architect. These previews will be created whenever a pull request is submitted that targets the master branch. Be sure to set values in Github Secrets for the architect fields: EMAIL, PASSWORD, ACCOUNT, PLATFORM, and COMPONENT_NAME.

name: Architect Create Preview

on:
  pull_request:
    branches:
      - master

jobs:
  architect_create_preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - name: Architect Create Preview
        uses: architect-team/create-preview@v1.0.1
        with:
          email: ${{ secrets.EMAIL }}
          password: ${{ secrets.PASSWORD }}
          account: ${{ secrets.ACCOUNT }}
          environment: preview-${{ github.event.number }}
          platform: ${{ secrets.PLATFORM }}
          component_name: ${{ secrets.COMPONENT_NAME }}

Gitlab CI

This job can be pasted into your .gitlab-ci.yml at the root of your repository. You are welcome to change the stage to whatever fits your needs to allow you to run tests before the preview is generated, and please be sure to assign correct values for the variables in the job. Additionally, you'll need to assign values for variables in the below config not prefixed with $CI_ in your repository's CI variables configuration so that the architect commands will run successfully.

This configuration takes advantage of GitLab environments in order to give you better control and visibility into what environments exist and what's deployed to them. On PR creation, both a GitLab and Architect environment will be created. The component specified in the repository will be registered with the Architect Cloud and deployed to the environment. When the PR is either merged or closed, the GitLab environment will be automatically deleted and the component deployed to the environment in the Architect Cloud will be destroyed.

# this example assumes that the repo has ARCHITECT_ACCOUNT and ARCHITECT_PLATFORM set as CI/CD variables
stages:
  - preview

default:
  image: docker:latest
  services:
    - docker:dind
  before_script:
    - apk add --update npm git
    - apk add yq --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
    - npm install -g @architect-io/cli --unsafe-perm
    - architect login -e $ARCHITECT_EMAIL -p $ARCHITECT_PASSWORD

deploy_preview:
  stage: preview
  variables:
    ARCHITECT_ENVIRONMENT: preview-$CI_MERGE_REQUEST_ID
  script: |
    architect register architect.yml -t $ARCHITECT_ENVIRONMENT
    architect environment:create $ARCHITECT_ENVIRONMENT || true
    architect deploy --auto_approve $ARCHITECT_COMPONENT_NAME:$ARCHITECT_ENVIRONMENT $ARCHITECT_DEPLOY_FLAGS
  environment:
    name: architect/preview-$CI_MERGE_REQUEST_ID
    url: https://cloud.architect.io/$ARCHITECT_ACCOUNT/environments/preview-$CI_MERGE_REQUEST_ID/
    on_stop: destroy_preview
  rules:
    - if: $CI_MERGE_REQUEST_ID

destroy_preview:
  stage: preview
  variables:
    ARCHITECT_ENVIRONMENT: preview-$CI_MERGE_REQUEST_ID
  script: |
    architect destroy --auto_approve -c $ARCHITECT_COMPONENT_NAME:$ARCHITECT_ENVIRONMENT
    architect env:destroy --auto_approve $ARCHITECT_ENVIRONMENT
  environment:
    name: architect/preview-$CI_MERGE_REQUEST_ID
    action: stop
  rules:
    - if: $CI_MERGE_REQUEST_ID
      when: manual

Cleanup preview environment

Github Actions

You certainly don't want your auto-generated preview environments to remain live forever eating up valuable cluster resources. Paste the snippet below into another Github workflow file in your repository to cleanup preview environments triggered on pull requests whenever the PRs close:

name: Architect Destroy Preview

on:
  pull_request:
    branches:
      - master
    types:
      - closed

jobs:
  architect_destroy_preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-node@v1
      - name: Architect Destroy Preview
        uses: architect-team/destroy-preview@v1.0.1
        with:
          email: ${{ secrets.EMAIL }}
          password: ${{ secrets.PASSWORD }}
          account: ${{ secrets.ACCOUNT }}
          environment: preview-${{ github.event.number }}
          component_name: ${{ secrets.COMPONENT_NAME }}
    Create preview environment
      Github Actions
      Gitlab CI
    Cleanup preview environment
      Github Actions