diff --git a/.github/workflows/bunnyshell-delete-env.yml b/.github/workflows/bunnyshell-delete-env.yml new file mode 100644 index 0000000..ab5838a --- /dev/null +++ b/.github/workflows/bunnyshell-delete-env.yml @@ -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 }} \ No newline at end of file diff --git a/.github/workflows/bunnyshell-deploy-env.yml b/.github/workflows/bunnyshell-deploy-env.yml new file mode 100644 index 0000000..71640f1 --- /dev/null +++ b/.github/workflows/bunnyshell-deploy-env.yml @@ -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<> $GITHUB_ENV + cat event.json >> $GITHUB_ENV + echo -e '\nEOFEVENTJSON' >> $GITHUB_ENV + + echo 'FLAGS_JSON<> $GITHUB_ENV + cat flags.json >> $GITHUB_ENV + echo -e '\nEOFFLAGSJSON' >> $GITHUB_ENV + + if [ -f "bunnyshell.yaml" ]; then + echo 'BUNNYSHELL_YAML_CONTENTS<> $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 }} \ No newline at end of file diff --git a/.github/workflows/bunnyshell-prepare-config.yml b/.github/workflows/bunnyshell-prepare-config.yml new file mode 100644 index 0000000..2f46288 --- /dev/null +++ b/.github/workflows/bunnyshell-prepare-config.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/bunnyshell-start-env.yml b/.github/workflows/bunnyshell-start-env.yml new file mode 100644 index 0000000..9360f9e --- /dev/null +++ b/.github/workflows/bunnyshell-start-env.yml @@ -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 }} \ No newline at end of file diff --git a/.github/workflows/bunnyshell-stop-env.yml b/.github/workflows/bunnyshell-stop-env.yml new file mode 100644 index 0000000..063fee7 --- /dev/null +++ b/.github/workflows/bunnyshell-stop-env.yml @@ -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 }} \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index d394218..43bcfee 100644 --- a/backend/server.js +++ b/backend/server.js @@ -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));