Skip to content

Commit 3e4d754

Browse files
authored
Staging apps via github actions (#579)
1 parent 0c254ee commit 3e4d754

File tree

8 files changed

+98
-34
lines changed

8 files changed

+98
-34
lines changed

.controlplane/Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz
1717
rm -rf /tmp/node-build-master
1818

1919
# Rails app lives here
20+
# Entry point and commands will be run from this directory
2021
WORKDIR /app
2122

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

57+
RUN chmod +x /app/.controlplane/*.sh
58+
5659
ENV RAILS_ENV=production \
5760
NODE_ENV=production \
5861
SECRET_KEY_BASE=NOT_USED_NON_BLANK
@@ -62,8 +65,7 @@ RUN yarn res:build
6265
RUN bin/rails react_on_rails:locale
6366
RUN bin/rails assets:precompile
6467

65-
# add entrypoint
66-
COPY .controlplane/entrypoint.sh ./
67-
ENTRYPOINT ["/app/entrypoint.sh"]
6868

69-
CMD ["rails", "server"]
69+
ENTRYPOINT ["./.controlplane/entrypoint.sh"]
70+
71+
CMD ["./bin/rails", "server"]

.controlplane/controlplane.yml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
# Configuration for "Heroku to CPLN playbook" custom scripts
1+
# Configuration for `cpl` commands.
2+
3+
# Keys beginning with "cpln_" correspond to your settings in Control Plane.
4+
5+
# Global settings that apply to `cpl` usage.
6+
# You can opt out of allowing the use of CPLN_ORG and CPLN_APP env vars
7+
# to avoid any accidents with the wrong org / app.
8+
allow_org_override_by_env: true
9+
allow_app_override_by_env: true
10+
211
aliases:
312
common: &common
4-
# Org for staging and QA apps is typically set as an alias
13+
# Org for staging and QA apps is typically set as an alias, shared by all apps, except for production apps.
514
# Production apps will use a different org than staging for security.
615
# Change this value to your org name
7-
# or set ENV value to CPLN_ORG as that will override whatever is used here for all cpl commands
16+
# or set ENV CPLN_ORG to your org name as that will override whatever is used here for all cpl commands
817
# cpln_org: shakacode-open-source-examples
918

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

34+
# Configure the workload name used when maintenance mode is on (defaults to "maintenance").
35+
maintenance_workload: maintenance
36+
2537
apps:
2638
react-webpack-rails-tutorial:
39+
# Simulate Production Version
2740
<<: *common
28-
tutorial-app:
41+
# Don't allow overriding the org and app by ENV vars b/c production is sensitive!
42+
allow_org_override_by_env: false
43+
allow_app_override_by_env: false
44+
45+
# Use a different organization for production.
46+
cpln_org: shakacode-open-source-examples
47+
48+
upstream: react-webpack-rails-tutorial-staging
49+
50+
release_script: release_script.sh
51+
52+
react-webpack-rails-tutorial-staging:
2953
<<: *common
3054
# QA Apps are like Heroku review apps, but the use `prefix` so you can run a commmand like
3155
# this to create a QA app for the tutorial app.
32-
# `cpl setup gvc postgres redis rails -a qa-tutorial-app-pr-1234`
33-
qa-tutorial-app:
56+
# `cpl setup gvc postgres redis rails -a qa-react-webpack-rails-tutorial-pr-1234`
57+
qa-react-webpack-rails-tutorial:
3458
<<: *common
59+
# Prefix is used to identify these "qa" apps.
3560
prefix: true

.controlplane/entrypoint.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash -e
22
# Runs before the main command
3-
# This script is unique to this demo project as it ensures the database and redis are ready before running the rails server
3+
# This script is unique to this demo project as it ensures the database and redis are ready
4+
# before running the rails server or other services.
45

56

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

23-
# If running the rails server then create or migrate existing database
24-
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
25-
echo " -- Preparing database"
26-
./bin/rails db:prepare
27-
fi
28-
2924
echo " -- Finishing entrypoint.sh, executing '$@'"
3025

3126
# Run the main command

.controlplane/readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ You can see the definition of Postgres and Redis in the `.controlplane/templates
1212

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

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

5960
```sh
6061
# Use environment variable to prevent repetition
61-
export APP_NAME=tutorial-app
62+
export APP_NAME=react-webpack-rails-tutorial
6263

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

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

8485
### Promoting code updates
8586

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

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

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

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

.controlplane/release_script.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash -e
2+
3+
echo 'Running release_script.sh per controlplane.yml'
4+
5+
echo 'Run DB migrations'
6+
./bin/rails db:prepare
7+
8+
echo 'Completed release_script.sh per controlplane.yml'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
kind: workload
2+
name: maintenance
3+
spec:
4+
type: standard
5+
containers:
6+
- name: maintenance
7+
env:
8+
- name: PORT
9+
value: "3000"
10+
- name: PAGE_URL
11+
value: ""
12+
image: "shakacode/maintenance-mode"
13+
ports:
14+
- number: 3000
15+
protocol: http
16+
defaultOptions:
17+
autoscaling:
18+
minScale: 1
19+
maxScale: 1
20+
capacityAI: false
21+
timeoutSeconds: 60
22+
firewallConfig:
23+
external:
24+
inboundAllowCIDR:
25+
- 0.0.0.0/0

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,3 @@ yarn-debug.log*
4848
.github/
4949
spec/
5050
scripts/
51-

.github/workflows/deploy-to-control-plane.yml renamed to .github/workflows/deploy-to-control-plane-staging.yml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ on:
1313

1414
# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
1515
env:
16-
CPLN_ORG: ${{secrets.CPLN_ORG}}
17-
CPLN_TOKEN: ${{secrets.CPLN_TOKEN}}
16+
CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}}
17+
CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}}
1818

1919
jobs:
20-
deploy-to-control-plane:
20+
deploy-to-control-plane-staging:
2121
runs-on: ubuntu-latest
2222

2323
steps:
@@ -34,29 +34,38 @@ jobs:
3434
run: |
3535
sudo npm install -g @controlplane/cli
3636
cpln --version
37-
gem install cpl -v 1.1.2
37+
gem install cpl -v 1.2.0
3838
3939
- name: Set Short SHA
4040
id: vars
4141
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
4242

4343
- name: cpl profile
4444
run: |
45-
cpln profile update default --token ${CPLN_TOKEN}
45+
cpln profile update default
46+
# cpln profile update default --token ${CPLN_TOKEN}
4647

4748
# Caching step
4849
- uses: actions/cache@v2
4950
with:
50-
path: /tmp/docker-layers
51-
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile') }}
51+
path: /tmp/.docker-cache
52+
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}-${{ github.sha }}
5253
restore-keys: |
54+
${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}
5355
${{ runner.os }}-docker-
5456
5557
- name: cpl build-image
5658
run: |
5759
cpln image docker-login
58-
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}}
60+
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}} --org ${{secrets.CPLN_ORG_STAGING}}
61+
62+
- name: Run release script
63+
run: |
64+
# Run database migrations (or other release tasks) with the latest image,
65+
# while the app is still running on the previous image.
66+
# This is analogous to the release phase.
67+
cpl run:detached './.controlplane/release_script.sh' -a ${{ secrets.APP_NAME_STAGING }} --image latest
5968
6069
- name: Deploy to Control Plane
6170
run: |
62-
cpl deploy-image -a ${{ secrets.APP_NAME_STAGING }}
71+
cpl deploy-image -a ${{ secrets.APP_NAME_STAGING }} --org ${{secrets.CPLN_ORG_STAGING}}

0 commit comments

Comments
 (0)