Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Review Apps on CPL Gem on ControlPlane #584

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .controlplane/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile and actions.yml
ARG RUBY_VERSION=3.1.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

Expand Down Expand Up @@ -63,9 +63,13 @@ ENV RAILS_ENV=production \

RUN yarn res:build
RUN bin/rails react_on_rails:locale
RUN bin/rails assets:precompile


# Precompile assets and remove build dependencies
# Note, if you have some Node.js programs, you need to install those in a different
# directory so you don't delete them here.
RUN bin/rails assets:precompile && rm -rf lib/bs && rm -rf node_modules

ENTRYPOINT ["./.controlplane/entrypoint.sh"]

CMD ["./bin/rails", "server"]
18 changes: 16 additions & 2 deletions .controlplane/controlplane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ aliases:
# Production apps will use a different org than staging for security.
# Change this value to your org name
# or set ENV CPLN_ORG to your org name as that will override whatever is used here for all cpl commands
# cpln_org: shakacode-open-source-examples
cpln_org: shakacode-open-source-examples-staging

# Example apps use only location. CPLN offers the ability to use multiple locations.
default_location: aws-us-east-2
Expand All @@ -34,6 +34,9 @@ aliases:
# Configure the workload name used when maintenance mode is on (defaults to "maintenance").
maintenance_workload: maintenance

# Configure the script to run when releasing an app., either with deploy-image or promote-app-from-upstream
release_script: release_script.sh

apps:
react-webpack-rails-tutorial:
# Simulate Production Version
Expand All @@ -47,7 +50,6 @@ apps:

upstream: react-webpack-rails-tutorial-staging

release_script: release_script.sh

react-webpack-rails-tutorial-staging:
<<: *common
Expand All @@ -56,5 +58,17 @@ apps:
# `cpl setup gvc postgres redis rails -a qa-react-webpack-rails-tutorial-pr-1234`
qa-react-webpack-rails-tutorial:
<<: *common
# Order matters!
setup_app_templates:
# GVC template contains the identity
- gvc

# Resources
- postgres
- redis

# Workloads, like Dynos types on Heroku
- daily-task
- rails
# Prefix is used to identify these "qa" apps.
prefix: true
2 changes: 1 addition & 1 deletion .controlplane/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export APP_NAME=react-webpack-rails-tutorial

# Provision all infrastructure on Control Plane.
# app react-webpack-rails-tutorial will be created per definition in .controlplane/controlplane.yml
cpl apply-template gvc postgres redis rails daily-task -a $APP_NAME
cpl setup-app -a $APP_NAME

# Build and push docker image to Control Plane repository
# Note, may take many minutes. Be patient.
Expand Down
22 changes: 18 additions & 4 deletions .controlplane/release_script.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#!/bin/bash -e

echo 'Running release_script.sh per controlplane.yml'

echo 'Run DB migrations'
./bin/rails db:prepare
log() {
echo "[`date +%Y-%m-%d:%H:%M:%S`]: $1"
}

echo 'Completed release_script.sh per controlplane.yml'
error_exit() {
log "$1" 1>&2
exit 1
}

log 'Running release_script.sh per controlplane.yml'

if [ -x ./bin/rails ]; then
log 'Run DB migrations'
./bin/rails db:prepare || error_exit "Failed to run DB migrations"
else
error_exit "./bin/rails does not exist or is not executable"
fi

log 'Completed release_script.sh per controlplane.yml'
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Template setup of the GVC, roughly corresponding to a Heroku app
kind: gvc
name: APP_GVC
name: {{APP_NAME}}
spec:
# For using templates for test apps, put ENV values here, stored in git repo.
# Production apps will have values configured manually after app creation.
env:
- name: DATABASE_URL
# Password does not matter because host postgres.APP_GVC.cpln.local can only be accessed
# Password does not matter because host postgres.{{APP_NAME}}.cpln.local can only be accessed
# locally within CPLN GVC, and postgres running on a CPLN workload is something only for a
# test app that lacks persistence.
value: 'postgres://the_user:the_password@postgres.APP_GVC.cpln.local:5432/APP_GVC'
value: 'postgres://the_user:the_password@postgres.{{APP_NAME}}.cpln.local:5432/{{APP_NAME}}'
- name: RAILS_ENV
value: production
- name: NODE_ENV
Expand All @@ -18,8 +18,14 @@ spec:
value: 'true'
- name: REDIS_URL
# No password for GVC local Redis. See comment above for postgres.
value: 'redis://redis.APP_GVC.cpln.local:6379'
value: 'redis://redis.{{APP_NAME}}.cpln.local:6379'
# Part of standard configuration
staticPlacement:
locationLinks:
- /org/APP_ORG/location/APP_LOCATION
- {{APP_LOCATION_LINK}}

---

# Identity is needed to access secrets
kind: identity
name: {{APP_IDENTITY}}
5 changes: 3 additions & 2 deletions .controlplane/templates/daily-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
- rake
- daily
inheritEnv: true
image: "/org/APP_ORG/image/APP_IMAGE"
image: {{APP_IMAGE_LINK}}
defaultOptions:
autoscaling:
minScale: 1
Expand All @@ -30,4 +30,5 @@ spec:
external:
outboundAllowCIDR:
- 0.0.0.0/0
identityLink: /org/APP_ORG/gvc/APP_GVC/identity/postgres-poc-identity
# Identity is used for binding workload to secrets
identityLink: {{APP_IDENTITY_LINK}}
23 changes: 23 additions & 0 deletions .controlplane/templates/org.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Org level secrets are used to store sensitive information that is
# shared across multiple apps in the same organization. This is
# useful for storing things like API keys, database credentials, and
# other sensitive information that is shared across multiple apps
# in the same organization.

# This is how you apply this once (not during CI)
# cpl apply-template secrets -a qa-react-webpack-rails-tutorial --org shakacode-open-source-examples-staging

kind: secret
name: {{APP_SECRETS}}
type: dictionary
data:
SOME_ENV: "123456"

---

# Policy is needed to allow identities to access secrets
kind: policy
name: {{APP_SECRETS_POLICY}}
targetKind: secret
targetLinks:
- //secret/{{APP_SECRETS}}
5 changes: 1 addition & 4 deletions .controlplane/templates/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bindings:
# - use
# - view
principalLinks:
- //gvc/APP_GVC/identity/postgres-poc-identity
- //gvc/{{APP_NAME}}/identity/postgres-poc-identity
targetKind: secret
targetLinks:
- //secret/postgres-poc-credentials
Expand Down Expand Up @@ -139,9 +139,6 @@ spec:
args:
- "-c"
- "cat /usr/local/bin/cpln-entrypoint.sh >> ./cpln-entrypoint.sh && chmod u+x ./cpln-entrypoint.sh && ./cpln-entrypoint.sh postgres"
#command: "cpln-entrypoint.sh"
#args:
# - "postgres"
ports:
- number: 5432
protocol: tcp
Expand Down
4 changes: 3 additions & 1 deletion .controlplane/templates/rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
value: debug
# Inherit other ENV values from GVC
inheritEnv: true
image: '/org/APP_ORG/image/APP_IMAGE'
image: {{APP_IMAGE_LINK}}
# 512 corresponds to a standard 1x dyno type
memory: 512Mi
ports:
Expand All @@ -34,3 +34,5 @@ spec:
# Could configure outbound for more security
outboundAllowCIDR:
- 0.0.0.0/0
# Identity is used for binding workload to secrets
identityLink: {{APP_IDENTITY_LINK}}
32 changes: 16 additions & 16 deletions .github/actions/deploy-to-control-plane/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ name: Deploy-To-Control-Plane
description: 'Deploys both to staging and to review apps'

inputs:
# The name of the app to deploy
app_name:
description: 'The name of the app to deploy'
required: true
Expand All @@ -20,15 +19,15 @@ runs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2' # Specify your Ruby version here
ruby-version: '3.1.2' # Specify your Ruby version here

- name: Install Control Plane CLI
shell: bash
run: |
sudo npm install -g @controlplane/cli
cpln --version
gem install cpl -v 1.2.0

gem install cpl
cpl --version
- name: Set Short SHA
id: vars
shell: bash
Expand All @@ -38,7 +37,6 @@ runs:
shell: bash
run: |
cpln profile update default
# cpln profile update default --token ${CPLN_TOKEN}

# Caching step
- uses: actions/cache@v2
Expand All @@ -49,22 +47,24 @@ runs:
${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}
${{ runner.os }}-docker-

- name: cpl setup-app
shell: bash
run: |
if ! cpl exists -a ${{ inputs.app_name }} ; then
cpl setup-app -a ${{ inputs.app_name }}
fi
# Provision all infrastructure on Control Plane.
# app react-webpack-rails-tutorial will be created per definition in .controlplane/controlplane.yml
- name: cpl build-image
shell: bash
run: |
cpln image docker-login
# Use BUILDKIT_PROGRESS=plain to get more verbose logging of the build
# BUILDKIT_PROGRESS=plain cpl build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
cpl build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
# --cache /tmp/.docker-cache

- name: Run release script
shell: bash
run: |
# Run database migrations (or other release tasks) with the latest image,
# while the app is still running on the previous image.
# This is analogous to the release phase.
cpl run:detached './.controlplane/release_script.sh' -a ${{ inputs.app_name }} --image latest

# --cache /tmp/.docker-cache
- name: Deploy to Control Plane
shell: bash
run: |
cpl deploy-image -a ${{ inputs.app_name }} --org ${{inputs.org}}
echo "Deploying to Control Plane"
cpl deploy-image -a ${{ inputs.app_name }} --run-release-phase --org ${{inputs.org}} --verbose
33 changes: 32 additions & 1 deletion .github/workflows/deploy-to-control-plane-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ name: Deploy Review App to Control Plane
on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# inputs:
# pr_number:
# description: 'Pull request number'
# required: false
# Triggers the workflow on pull request events
# pull_request:
# branches:
# - main # or the branch you want to trigger the workflow

# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
env:
Expand All @@ -20,7 +28,30 @@ jobs:
- name: Check out the repo
uses: actions/checkout@v2

- name: Get PR number
run: |
echo "GITHUB_REPOSITORY: \"$GITHUB_REPOSITORY\""
REF=${{ github.ref }}
REF=${REF#refs/heads/} # Remove 'refs/heads/' prefix
echo "REF: \"$REF\""
API_RESPONSE=$(curl --location --request GET "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?state=open" \
--header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}')
echo "API_RESPONSE: $API_RESPONSE"
PR_NUMBER=$(echo $API_RESPONSE | jq '.[] | select(.head.ref=="'$REF'") | .number')
echo "PR_NUMBER: $PR_NUMBER"
if [ -z "$PR_NUMBER" ]; then
echo "PR_NUMBER is not set. Aborting."
exit 1
fi
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
Comment on lines +31 to +46
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error handling for API requests.

The current implementation does not handle potential errors from the GitHub API request. Consider adding error handling to ensure robustness.

Here's a suggestion to improve error handling:

API_RESPONSE=$(curl --location --request GET "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?state=open" \
--header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' || { echo "Failed to fetch PRs"; exit 1; })


- name: Get App Name
run: |
echo "PR_NUMBER: ${{ env.PR_NUMBER }}"
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}" >> $GITHUB_ENV
echo "App Name: ${{ env.APP_NAME }}"

- uses: ./.github/actions/deploy-to-control-plane
with:
app_name: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number }}
app_name: ${{ env.APP_NAME }}
org: ${{ secrets.CPLN_ORG_STAGING }}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let make = (~fetchData) => {
}

let forms: array<formData> = [
{formName: "Horizontal Form", formType: Horizontal},
{formName: "Horizontal Form XXXX", formType: Horizontal},
{formName: "Inline Form", formType: Inline},
{formName: "Stacked Form", formType: Stacked},
]
Expand Down
Loading