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

Added Github Actions workflows to create environments at pull request #22

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
22 changes: 22 additions & 0 deletions .github/workflows/bunnyshell-delete-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Bunnyshell - Delete Preview Environment
concurrency: bns-delete-${{ github.event.number || github.event.issue.number }}
on:
pull_request_target:
types: [closed]
branches:
- '*'
issue_comment:
types: [ created, edited ]
permissions:
pull-requests: write
jobs:
start:
name: Delete Environment
uses: bunnyshell/workflows/.github/workflows/delete-env.yaml@v1
with:
project-id: ${{ vars.BUNNYSHELL_PROJECT_ID }}
env-name: 'DemoBooks PR #${{ github.event.number || github.event.issue.number }}'
allowed-users: ${{ vars.BUNNYSHELL_ALLOWED_USERS }}
comment-on-pr: true
secrets:
bunnyshell-access-token: ${{ secrets.BUNNYSHELL_ACCESS_TOKEN }}
91 changes: 91 additions & 0 deletions .github/workflows/bunnyshell-deploy-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Bunnyshell - Deploy Preview Environment
on:
workflow_run:
workflows:
- "Bunnyshell - Prepare Preview Environment Configuration"
types:
- completed
permissions:
pull-requests: write
jobs:
load-artifact:
name: Load artifact
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
outputs:
pr-number: ${{ env.PR_NUMBER }}
skip-deployment: ${{ fromJSON(env.FLAGS_JSON).skip_deployment }}
is-pull-request-event: ${{ fromJSON(env.EVENT_JSON).pull_request != '' }}
bunnyshell-yaml-contents: ${{ env.BUNNYSHELL_YAML_CONTENTS }}
steps:
- name: Download artifact
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "bunnyshell"
})[0];
if (matchArtifact === undefined) {
throw TypeError('Build Artifact not found!');
}
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/artifact.zip`, Buffer.from(download.data));
- name: Load artifact data
run: |
unzip artifact.zip

echo 'EVENT_JSON<<EOFEVENTJSON' >> $GITHUB_ENV
cat event.json >> $GITHUB_ENV
echo -e '\nEOFEVENTJSON' >> $GITHUB_ENV

echo 'FLAGS_JSON<<EOFFLAGSJSON' >> $GITHUB_ENV
cat flags.json >> $GITHUB_ENV
echo -e '\nEOFFLAGSJSON' >> $GITHUB_ENV

if [ -f "bunnyshell.yaml" ]; then
echo 'BUNNYSHELL_YAML_CONTENTS<<EOFBNSYAML' >> $GITHUB_ENV
cat bunnyshell.yaml >> $GITHUB_ENV
echo -e '\nEOFBNSYAML' >> $GITHUB_ENV
fi
- name: Set variables
run: |
echo "IS_PULL_REQUEST=${{ fromJSON(env.EVENT_JSON).pull_request != '' }}" >> $GITHUB_ENV
echo "IS_ISSUE_COMMENT=${{ fromJSON(env.EVENT_JSON).issue.pull_request != '' }}" >> $GITHUB_ENV
echo "PR_NUMBER=${{ fromJSON(env.EVENT_JSON).issue.number || fromJSON(env.EVENT_JSON).number }}" >> $GITHUB_ENV
- name: Comment unapproved deployment
uses: thollander/actions-comment-pull-request@v2
if: ${{ fromJSON(env.FLAGS_JSON).skip_deployment }}
with:
message: |
### Bunnyshell Preview Environment automatic creation skipped due to restricted files changes.

Add a comment containing `/bns:deploy` to approve the creation of the environment.
comment_tag: bunnyshell-preview-env
pr_number: ${{ env.PR_NUMBER }}
deploy:
name: Deploy Environment
needs: load-artifact
uses: bunnyshell/workflows/.github/workflows/deploy-env.yaml@v1
concurrency: bns-deploy-${{ needs.load-artifact.outputs.prNumber }}
if: ${{ github.event.workflow_run.conclusion == 'success' && needs.load-artifact.outputs.skip-deployment == 'false' }}
with:
pr-number: "${{ needs.load-artifact.outputs.pr-number }}"
project-id: ${{ vars.BUNNYSHELL_PROJECT_ID }}
cluster-id: ${{ vars.BUNNYSHELL_CLUSTER_ID }}
env-name: "DemoBooks PR #${{ needs.load-artifact.outputs.pr-number }}"
bunnyshell-yaml-contents: ${{ needs.load-artifact.outputs.bunnyshell-yaml-contents }}
comment-on-pr: true
deploy-as-stopped: ${{ needs.load-artifact.outputs.is-pull-request-event == 'true' }}
secrets:
bunnyshell-access-token: ${{ secrets.BUNNYSHELL_ACCESS_TOKEN }}
18 changes: 18 additions & 0 deletions .github/workflows/bunnyshell-prepare-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Bunnyshell - Prepare Preview Environment Configuration
on:
pull_request:
types: [ opened, reopened, synchronize ]
branches:
- '*'
paths-ignore:
- "*.md"
issue_comment:
types: [ created, edited ]
jobs:
deploy:
name: Deploy Environment
uses: bunnyshell/workflows/.github/workflows/prepare-configuration.yaml@v1
with:
bunnyshell-yaml-path: .bunnyshell/templates/express-react-postgres/bunnyshell.yaml
allowed-users: ${{ vars.BUNNYSHELL_ALLOWED_USERS }}
git-ref: refs/pull/${{ github.event.number || github.event.issue.number }}/head
18 changes: 18 additions & 0 deletions .github/workflows/bunnyshell-start-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Bunnyshell - Start Preview Environment
concurrency: bns-start-stop-${{ github.event.issue.number }}
on:
issue_comment:
types: [created, edited]
permissions:
pull-requests: write
jobs:
start:
name: Start Environment
uses: bunnyshell/workflows/.github/workflows/start-env.yaml@v1
with:
project-id: ${{ vars.BUNNYSHELL_PROJECT_ID }}
env-name: 'DemoBooks PR #${{ github.event.issue.number }}'
allowed-users: ${{ vars.BUNNYSHELL_ALLOWED_USERS }}
comment-on-pr: true
secrets:
bunnyshell-access-token: ${{ secrets.BUNNYSHELL_ACCESS_TOKEN }}
18 changes: 18 additions & 0 deletions .github/workflows/bunnyshell-stop-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Bunnyshell - Stop Preview Environment
concurrency: bns-start-stop-${{ github.event.issue.number }}
on:
issue_comment:
types: [created, edited]
permissions:
pull-requests: write
jobs:
start:
name: Stop Environment
uses: bunnyshell/workflows/.github/workflows/stop-env.yaml@v1
with:
project-id: ${{ vars.BUNNYSHELL_PROJECT_ID }}
env-name: 'DemoBooks PR #${{ github.event.issue.number }}'
allowed-users: ${{ vars.BUNNYSHELL_ALLOWED_USERS }}
comment-on-pr: true
secrets:
bunnyshell-access-token: ${{ secrets.BUNNYSHELL_ACCESS_TOKEN }}
3 changes: 2 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const process = require("process");
const app = express();

var corsOptions = {
origin: process.env.FRONTEND_URL
// need to strip ending slash from frontendUrl for CORS to work
origin: process.env.FRONTEND_URL.replace(/\/+$/, '')
};

app.use(cors(corsOptions));
Expand Down