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

Improvements to deployments #615

Merged
merged 59 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
ee08d3a
Pass correct arg
justin808 Jan 23, 2025
c790151
Set args with =
justin808 Jan 23, 2025
8355030
Address deprecation messages
justin808 Jan 23, 2025
f19d79f
Updates to deployment
justin808 Jan 23, 2025
e487791
updates
justin808 Jan 23, 2025
25e9502
Fixed the deployment_id output in the
justin808 Jan 23, 2025
de55983
fixes
justin808 Jan 23, 2025
5240c9b
fixes
justin808 Jan 23, 2025
2a1b9a3
fixes
justin808 Jan 23, 2025
8918cd2
fixes
justin808 Jan 23, 2025
fe262cb
fixes
justin808 Jan 23, 2025
80d841b
fixes
justin808 Jan 23, 2025
084f0d0
fixes
justin808 Jan 23, 2025
cac41c4
Fixed failure to deploy to review app
justin808 Jan 23, 2025
745ca0e
fixes
justin808 Jan 23, 2025
f77a610
fixes
justin808 Jan 23, 2025
87662fa
Updates, fix crash
justin808 Jan 24, 2025
4970fbe
fixes
justin808 Jan 24, 2025
e26cc72
fixes
justin808 Jan 24, 2025
29ade6f
Fixes
justin808 Jan 24, 2025
8db0511
fixes
justin808 Jan 24, 2025
b7a7229
fixes
justin808 Jan 24, 2025
1cff976
fixes
justin808 Jan 24, 2025
6cbdb04
fixes
justin808 Jan 24, 2025
0b15816
fixes
justin808 Jan 24, 2025
ae8a473
fixes
justin808 Jan 24, 2025
e6d5025
fixes
justin808 Jan 24, 2025
444ec85
fixes
justin808 Jan 24, 2025
8041a01
fixes
justin808 Jan 24, 2025
b03b6d7
fixes
justin808 Jan 24, 2025
a12e0d4
fixes
justin808 Jan 24, 2025
98649f2
fixes
justin808 Jan 24, 2025
b2e710a
fixes
justin808 Jan 24, 2025
c54a544
fixes
justin808 Jan 24, 2025
c80ff43
fixes
justin808 Jan 24, 2025
40473cc
fixes
justin808 Jan 25, 2025
038cf8f
fixes
justin808 Jan 25, 2025
c25211f
fixes
justin808 Jan 25, 2025
304e642
fixes
justin808 Jan 25, 2025
4744c76
fixes
justin808 Jan 25, 2025
5c8b3ab
fixes
justin808 Jan 26, 2025
2a51c92
fixes
justin808 Jan 26, 2025
61213b9
fixes
justin808 Jan 26, 2025
e431bdf
fixes
justin808 Jan 26, 2025
895986c
fixes
justin808 Jan 26, 2025
3fe506e
fixes
justin808 Jan 26, 2025
585c02d
fixes
justin808 Jan 26, 2025
c46f595
fixes
justin808 Jan 26, 2025
65c64d1
fixes
justin808 Jan 26, 2025
e96ac82
doc changes
justin808 Jan 26, 2025
2be9c2f
Changes made:
justin808 Jan 26, 2025
5f50639
fixes
justin808 Jan 26, 2025
e0cf883
fixes
justin808 Jan 26, 2025
b27783e
added promotion and other fixes
justin808 Jan 26, 2025
66a4114
fixes
justin808 Jan 26, 2025
3426aca
fix-for-delete
justin808 Jan 26, 2025
92ad3a7
fix-for-delete
justin808 Jan 26, 2025
7d75738
fix-for-delete
justin808 Jan 26, 2025
d5b8b7f
fix-for-delete
justin808 Jan 26, 2025
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: 4 additions & 4 deletions .controlplane/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
ARG RUBY_VERSION=3.3.4
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Current commit hash environment variable
ARG GIT_COMMIT
ENV GIT_COMMIT_SHA=${GIT_COMMIT}

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3
Expand Down Expand Up @@ -76,7 +80,3 @@ ENTRYPOINT ["./.controlplane/entrypoint.sh"]
# Default args to pass to the entry point that can be overridden
# For Kubernetes and ControlPlane, these are the "workload args"
CMD ["./bin/rails", "server"]

# Current commit hash environment variable
ARG GIT_COMMIT_SHA
ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA}
18 changes: 9 additions & 9 deletions .github/actions/deploy-to-control-plane/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ inputs:
app_name:
description: 'The name of the app to deploy'
required: true
default:
org:
description: 'The org of the app to deploy'
required: true
default:

runs:
using: 'composite'
Expand All @@ -22,13 +20,14 @@ runs:
- name: Set Short SHA
id: vars
shell: bash
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
run: |
echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

# Caching step
- uses: actions/cache@v2
# Updated caching step to v3
- uses: actions/cache@v3
with:
path: /tmp/.docker-cache
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/package.json', '**/yarn.lock') }}-${{ github.sha }}
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-
Expand All @@ -39,6 +38,7 @@ runs:
if ! cpflow exists -a ${{ inputs.app_name }} ; then
cpflow 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: cpflow build-image
Expand All @@ -47,10 +47,10 @@ runs:
cpln image docker-login
# Use BUILDKIT_PROGRESS=plain to get more verbose logging of the build
# BUILDKIT_PROGRESS=plain cpflow build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
cpflow build-image -a ${{ inputs.app_name }} --commit ${{steps.vars.outputs.sha_short}} --org ${{inputs.org}}
# --cache /tmp/.docker-cache
cpflow build-image -a ${{ inputs.app_name }} --commit=${{steps.vars.outputs.sha_short}} --org=${{inputs.org}}

- name: Deploy to Control Plane
shell: bash
run: |
echo "Deploying to Control Plane"
cpflow deploy-image -a ${{ inputs.app_name }} --run-release-phase --org ${{inputs.org}} --verbose
cpflow deploy-image -a ${{ inputs.app_name }} --run-release-phase --org ${{inputs.org}} --verbose
4 changes: 2 additions & 2 deletions .github/actions/setup-environment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ runs:
- name: Install Control Plane CLI and cpflow gem
shell: bash
run: |
sudo npm install -g @controlplane/cli@3.1.0
sudo npm install -g @controlplane/cli@3.3.0
cpln --version
gem install cpflow -v 4.0.0
gem install cpflow -v 4.1.0
cpflow --version

- name: cpln profile
Expand Down
187 changes: 138 additions & 49 deletions .github/workflows/deploy-to-control-plane-review.yml
Original file line number Diff line number Diff line change
@@ -1,85 +1,174 @@
# Control Plane GitHub Action

name: Deploy Review App to Control Plane

# Controls when the workflow will run
on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Uncomment these lines to trigger the workflow on pull request events
# pull_request:
# branches:
# - master

# deploy on comment "/deploy-review-app"
pull_request:
types: [opened, synchronize, reopened]
branches: [master]
issue_comment:
types: [created, edited]
types: [created]

concurrency:
group: review-app-${{ github.event.pull_request.number || github.event.issue.number }}
cancel-in-progress: true

# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI
env:
CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}}
CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}}
# Uncomment this line to use the PR number from the pull requests trigger event (that trigger is commented)
# PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
PR_NUMBER: ${{ github.event.issue.number }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}

jobs:
check-concurrent:
runs-on: ubuntu-latest
outputs:
cancelled: ${{ steps.check.outputs.cancelled }}
steps:
- name: Check for concurrent deployment
id: check
run: |
if [ "${{ github.run_attempt }}" != "1" ]; then
echo "⚠️ Cancelling previous deployment due to new code push..."
echo "cancelled=true" >> $GITHUB_OUTPUT
else
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix shell script quoting in concurrent check.

Add quotes around GITHUB_OUTPUT to prevent word splitting:

-            echo "cancelled=true" >> $GITHUB_OUTPUT
+            echo "cancelled=true" >> "$GITHUB_OUTPUT"
-            echo "cancelled=false" >> $GITHUB_OUTPUT
+            echo "cancelled=false" >> "$GITHUB_OUTPUT"

Also applies to: 34-34

echo "cancelled=false" >> $GITHUB_OUTPUT
fi

deploy-to-control-plane-review:
if: ${{ github.event_name != 'issue_comment' || (github.event.comment.body == '/deploy-review-app' && github.event.issue.pull_request) }}
needs: check-concurrent
if: |
needs.check-concurrent.outputs.cancelled != 'true' &&
(github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request' ||
(github.event_name == 'issue_comment' &&
github.event.comment.body == '/deploy-review-app' &&
github.event.issue.pull_request))
runs-on: ubuntu-latest

permissions:
contents: read
deployments: write
pull-requests: write

outputs:
app_url: ${{ steps.deploy.outputs.app_url }}
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }}

steps:
- name: Notify deployment start
uses: actions/github-script@v7
with:
script: |
const message = `🚀 Starting new deployment for commit: ${context.sha.substring(0, 7)}
${context.payload.commits ? `\nChanges: ${context.payload.commits[0].message}` : ''}`;

await github.rest.issues.createComment({
issue_number: context.issue.number || context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});

- name: Create GitHub Deployment
id: create-deployment
uses: actions/github-script@v7
with:
script: |
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
environment: 'review-app',
auto_merge: false,
required_contexts: []
});
return { deployment_id: deployment.data.id };

- name: Get PR HEAD Ref
if: ${{ github.event_name == 'issue_comment' }}
id: getRef
run: echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
run: |
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix shell script quoting.

Add double quotes around variables to prevent word splitting and globbing:

-          echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
+          echo "PR_REF=$(gh pr view "$PR_NUMBER" --repo "${{ github.repository }}" --json headRefName | jq -r '.headRefName')" >> "$GITHUB_OUTPUT"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
run: |
echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "PR_REF=$(gh pr view "$PR_NUMBER" --repo "${{ github.repository }}" --json headRefName | jq -r '.headRefName')" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
🧰 Tools
🪛 actionlint (1.7.4)

53-53: shellcheck reported issue in this script: SC2086:info:1:27: Double quote to prevent globbing and word splitting

(shellcheck)


53-53: shellcheck reported issue in this script: SC2086:info:1:117: Double quote to prevent globbing and word splitting

(shellcheck)

- name: Checkout source code from Github
- name: Checkout source code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ steps.getRef.outputs.PR_REF || github.ref }}

- name: Add GitHub Comment
if: ${{ github.event_name == 'issue_comment' }}
- name: Update deployment status (in_progress)
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
body: "We started working on your review-app deployment. You can track progress in the `Actions` Tab [here](https://github.com/shakacode/react-webpack-rails-tutorial/actions/workflows/deploy-to-control-plane-review.yml) on Github."
})
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
state: 'in_progress',
description: 'Deployment is in progress'
});

- name: Get PR number
if: ${{ github.event_name != 'issue_comment' }}
run: |
echo "GITHUB_REPOSITORY: \"$GITHUB_REPOSITORY\""
if [ -z "$PR_NUMBER" ]; then
echo "PR_NUMBER is not in the trigger event. Fetching PR number from open PRs."
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 }}')
PR_NUMBER=$(echo "$API_RESPONSE" | jq '.[] | select(.head.ref=="'$REF'") | .number')
fi
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
- name: Get App Name
- name: Configure app name
id: app-config
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
APP_NAME="qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}"
echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
echo "app_name=$APP_NAME" >> $GITHUB_OUTPUT

- name: Deploy to Control Plane
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix shell script quoting in app name configuration.

Add double quotes around variables to prevent word splitting and globbing:

-          echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
-          echo "app_name=$APP_NAME" >> $GITHUB_OUTPUT
+          echo "APP_NAME=$APP_NAME" >> "$GITHUB_ENV"
+          echo "app_name=$APP_NAME" >> "$GITHUB_OUTPUT"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Configure app name
id: app-config
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
APP_NAME="qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}"
echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
echo "app_name=$APP_NAME" >> $GITHUB_OUTPUT
- name: Configure app name
id: app-config
run: |
APP_NAME="qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}"
echo "APP_NAME=$APP_NAME" >> "$GITHUB_ENV"
echo "app_name=$APP_NAME" >> "$GITHUB_OUTPUT"
🧰 Tools
🪛 actionlint (1.7.4)

114-114: shellcheck reported issue in this script: SC2086:info:2:30: Double quote to prevent globbing and word splitting

(shellcheck)


114-114: shellcheck reported issue in this script: SC2086:info:3:30: Double quote to prevent globbing and word splitting

(shellcheck)

id: deploy
uses: ./.github/actions/deploy-to-control-plane
with:
app_name: ${{ env.APP_NAME }}
org: ${{ env.CPLN_ORG }}

- name: Update deployment status (success)
if: success()
uses: actions/github-script@v7
with:
script: |
const message = `✅ Deployment successful!
Environment: review-app
Commit: ${context.sha.substring(0, 7)}
URL: ${{ steps.deploy.outputs.app_url }}`;

await github.rest.issues.createComment({
issue_number: context.issue.number || context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});

await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
state: 'success',
environment_url: '${{ steps.deploy.outputs.app_url }}',
description: 'Deployment successful'
});

- name: Update deployment status (failure)
if: failure()
uses: actions/github-script@v7
with:
script: |
const message = `❌ Deployment failed
Commit: ${context.sha.substring(0, 7)}
Please check the [workflow logs](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;

await github.rest.issues.createComment({
issue_number: context.issue.number || context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});

await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create-deployment.outputs.result.deployment_id }},
state: 'failure',
description: 'Deployment failed'
});
Loading