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

Staging apps via github actions #579

Merged
merged 17 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions .controlplane/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz
rm -rf /tmp/node-build-master

# Rails app lives here
# Entry point and commands will be run from this directory
WORKDIR /app

# Set production environment
Expand Down Expand Up @@ -53,6 +54,8 @@ RUN apt-get update -qq && \
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /app /app

RUN chmod +x /app/.controlplane/*.sh
Copy link
Member Author

@justin808 justin808 Jan 5, 2024

Choose a reason for hiding this comment

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

@borela is this typical? Without, the file permissions were unset.

Copy link
Member

Choose a reason for hiding this comment

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

This means the files were committed to the repo without +x permission, you can fix that, commit the files again, and remove that chmod.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did and it didn't fix the issue.


ENV RAILS_ENV=production \
NODE_ENV=production \
SECRET_KEY_BASE=NOT_USED_NON_BLANK
Expand All @@ -62,8 +65,7 @@ RUN yarn res:build
RUN bin/rails react_on_rails:locale
RUN bin/rails assets:precompile

# add entrypoint
COPY .controlplane/entrypoint.sh ./
ENTRYPOINT ["/app/entrypoint.sh"]

CMD ["rails", "server"]
ENTRYPOINT ["./.controlplane/entrypoint.sh"]

CMD ["./bin/rails", "server"]
37 changes: 31 additions & 6 deletions .controlplane/controlplane.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Configuration for "Heroku to CPLN playbook" custom scripts
# Configuration for `cpl` commands.

# Keys beginning with "cpln_" correspond to your settings in Control Plane.

# Global settings that apply to `cpl` usage.
# You can opt out of allowing the use of CPLN_ORG and CPLN_APP env vars
# to avoid any accidents with the wrong org / app.
allow_org_override_by_env: true
allow_app_override_by_env: true

aliases:
common: &common
# Org for staging and QA apps is typically set as an alias
# Org for staging and QA apps is typically set as an alias, shared by all apps, except for production apps.
# Production apps will use a different org than staging for security.
# Change this value to your org name
# or set ENV value to CPLN_ORG as that will override whatever is used here for all cpl commands
# 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

# Example apps use only location. CPLN offers the ability to use multiple locations.
Expand All @@ -22,14 +31,30 @@ aliases:
- redis
- postgres

# Configure the workload name used when maintenance mode is on (defaults to "maintenance").
maintenance_workload: maintenance

apps:
react-webpack-rails-tutorial:
# Simulate Production Version
<<: *common
tutorial-app:
# Don't allow overriding the org and app by ENV vars b/c production is sensitive!
allow_org_override_by_env: false
allow_app_override_by_env: false

# Use a different organization for production.
cpln_org: shakacode-open-source-examples

upstream: react-webpack-rails-tutorial-staging

release_script: release_script.sh

react-webpack-rails-tutorial-staging:
<<: *common
# QA Apps are like Heroku review apps, but the use `prefix` so you can run a commmand like
# this to create a QA app for the tutorial app.
# `cpl setup gvc postgres redis rails -a qa-tutorial-app-pr-1234`
qa-tutorial-app:
# `cpl setup gvc postgres redis rails -a qa-react-webpack-rails-tutorial-pr-1234`
qa-react-webpack-rails-tutorial:
<<: *common
# Prefix is used to identify these "qa" apps.
prefix: true
9 changes: 2 additions & 7 deletions .controlplane/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash -e
# Runs before the main command
# This script is unique to this demo project as it ensures the database and redis are ready before running the rails server
# This script is unique to this demo project as it ensures the database and redis are ready
# before running the rails server or other services.


wait_for_service()
Expand All @@ -20,12 +21,6 @@ echo " -- Waiting for services"
wait_for_service $(echo $DATABASE_URL | sed -e 's|^.*@||' -e 's|/.*$||')
wait_for_service $(echo $REDIS_URL | sed -e 's|redis://||' -e 's|/.*$||')

# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
echo " -- Preparing database"
./bin/rails db:prepare
fi

echo " -- Finishing entrypoint.sh, executing '$@'"

# Run the main command
Expand Down
15 changes: 8 additions & 7 deletions .controlplane/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ You can see the definition of Postgres and Redis in the `.controlplane/templates

1. Ensure your [Control Plane](https://controlplane.com) account is set up.
You should have an `organization` `<your-org>` for testing in that account.
You will modify value for `aliases.common.cpln_org` in `.controlplane/controlplane.yml`.
Set ENV variable `CPLN_ORG` to `<your-org>`. Alternatively, you may modify the
value for `aliases.common.cpln_org` in `.controlplane/controlplane.yml`.
If you need an organization, please [contact Shakacode](mailto:[email protected]).

2. Install Control Plane CLI (and configure access) using `npm install -g @controlplane/cli`.
Expand Down Expand Up @@ -58,10 +59,10 @@ and not `cpln` which is the Control Plane CLI.

```sh
# Use environment variable to prevent repetition
export APP_NAME=tutorial-app
export APP_NAME=react-webpack-rails-tutorial

# Provision all infrastructure on Control Plane.
# app tutorial-app will be created per definition in .controlplane/controlplane.yml
# 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

# Build and push docker image to Control Plane repository
Expand All @@ -83,11 +84,11 @@ cpl open -a $APP_NAME

### Promoting code updates

After committing code, you will update your deployment of `tutorial-app` with the following commands:
After committing code, you will update your deployment of `react-webpack-rails-tutorial` with the following commands:

```sh
# Assuming you have already set APP_NAME env variable to tutorial-app
# Build and push new image with sequential image tagging, e.g. 'tutorial-app:1', then 'tutorial-app:2', etc.
# Assuming you have already set APP_NAME env variable to react-webpack-rails-tutorial
# Build and push new image with sequential image tagging, e.g. 'react-webpack-rails-tutorial:1', then 'react-webpack-rails-tutorial:2', etc.
cpl build-image -a $APP_NAME

# Run database migrations (or other release tasks) with latest image,
Expand All @@ -102,7 +103,7 @@ cpl deploy-image -a $APP_NAME
If you needed to push a new image with a specific commit SHA, you can run the following command:

```sh
# Build and push with sequential image tagging and commit SHA, e.g. 'tutorial-app:123_ABCD'
# Build and push with sequential image tagging and commit SHA, e.g. 'react-webpack-rails-tutorial:123_ABCD'
cpl build-image -a $APP_NAME --commit ABCD
```

Expand Down
8 changes: 8 additions & 0 deletions .controlplane/release_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -e

echo 'Running release_script.sh per controlplane.yml'

echo 'Run DB migrations'
./bin/rails db:prepare

echo 'Completed release_script.sh per controlplane.yml'
25 changes: 25 additions & 0 deletions .controlplane/templates/maintenance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
kind: workload
name: maintenance
spec:
type: standard
containers:
- name: maintenance
env:
- name: PORT
value: "3000"
- name: PAGE_URL
value: ""
image: "shakacode/maintenance-mode"
ports:
- number: 3000
protocol: http
defaultOptions:
autoscaling:
minScale: 1
maxScale: 1
capacityAI: false
timeoutSeconds: 60
firewallConfig:
external:
inboundAllowCIDR:
- 0.0.0.0/0
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ yarn-debug.log*
.github/
spec/
scripts/

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ on:

# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
env:
CPLN_ORG: ${{secrets.CPLN_ORG}}
CPLN_TOKEN: ${{secrets.CPLN_TOKEN}}
CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}}
CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}}

jobs:
deploy-to-control-plane:
deploy-to-control-plane-staging:
runs-on: ubuntu-latest

steps:
Expand All @@ -34,29 +34,38 @@ jobs:
run: |
sudo npm install -g @controlplane/cli
cpln --version
gem install cpl -v 1.1.2
gem install cpl -v 1.2.0

- name: Set Short SHA
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

- name: cpl profile
run: |
cpln profile update default --token ${CPLN_TOKEN}
cpln profile update default
# cpln profile update default --token ${CPLN_TOKEN}

# Caching step
- uses: actions/cache@v2
with:
path: /tmp/docker-layers
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile') }}
path: /tmp/.docker-cache
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}
${{ runner.os }}-docker-

- name: cpl build-image
run: |
cpln image docker-login
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}}
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}} --org ${{secrets.CPLN_ORG_STAGING}}

- name: Run release script
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 ${{ secrets.APP_NAME_STAGING }} --image latest

- name: Deploy to Control Plane
run: |
cpl deploy-image -a ${{ secrets.APP_NAME_STAGING }}
cpl deploy-image -a ${{ secrets.APP_NAME_STAGING }} --org ${{secrets.CPLN_ORG_STAGING}}