|
1 | | -# Control Plane GitHub Action |
2 | | - |
3 | 1 | name: Deploy Review App to Control Plane |
4 | 2 |
|
5 | | -# Controls when the workflow will run |
6 | 3 | on: |
7 | | - # Allows you to run this workflow manually from the Actions tab |
8 | 4 | workflow_dispatch: |
9 | | - |
10 | | - # Uncomment these lines to trigger the workflow on pull request events |
11 | | - # pull_request: |
12 | | - # branches: |
13 | | - # - master |
14 | | - |
15 | | - # deploy on comment "/deploy-review-app" |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + branches: [master] |
16 | 8 | issue_comment: |
17 | | - types: [created, edited] |
| 9 | + types: [created] |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: review-app-${{ github.event.pull_request.number || github.event.issue.number }} |
| 13 | + cancel-in-progress: true |
18 | 14 |
|
19 | | -# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI |
20 | 15 | env: |
21 | 16 | CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}} |
22 | 17 | CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}} |
23 | | - # Uncomment this line to use the PR number from the pull requests trigger event (that trigger is commented) |
24 | | - # PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }} |
25 | | - PR_NUMBER: ${{ github.event.issue.number }} |
| 18 | + PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }} |
26 | 19 |
|
27 | 20 | jobs: |
28 | 21 | deploy-to-control-plane-review: |
29 | | - if: ${{ github.event_name != 'issue_comment' || (github.event.comment.body == '/deploy-review-app' && github.event.issue.pull_request) }} |
| 22 | + if: | |
| 23 | + github.event_name == 'workflow_dispatch' || |
| 24 | + github.event_name == 'pull_request' || |
| 25 | + (github.event_name == 'issue_comment' && |
| 26 | + github.event.comment.body == '/deploy-review-app' && |
| 27 | + github.event.issue.pull_request) |
30 | 28 | runs-on: ubuntu-latest |
31 | 29 |
|
| 30 | + outputs: |
| 31 | + app_url: ${{ steps.deploy.outputs.app_url }} |
| 32 | + deployment_id: ${{ steps.create-deployment.outputs.deployment_id }} |
| 33 | + |
32 | 34 | steps: |
| 35 | + - name: Create GitHub Deployment |
| 36 | + id: create-deployment |
| 37 | + uses: actions/github-script@v7 |
| 38 | + with: |
| 39 | + script: | |
| 40 | + const deployment = await github.rest.repos.createDeployment({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + ref: context.sha, |
| 44 | + environment: 'review-app', |
| 45 | + auto_merge: false, |
| 46 | + required_contexts: [] |
| 47 | + }); |
| 48 | + return deployment.data.id; |
| 49 | +
|
33 | 50 | - name: Get PR HEAD Ref |
34 | 51 | if: ${{ github.event_name == 'issue_comment' }} |
35 | 52 | id: getRef |
36 | | - run: echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT |
| 53 | + run: | |
| 54 | + echo "PR_REF=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT |
37 | 55 | env: |
38 | 56 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
39 | 57 |
|
40 | | - - name: Checkout source code from Github |
| 58 | + - name: Checkout source code |
41 | 59 | uses: actions/checkout@v4 |
42 | 60 | with: |
43 | 61 | fetch-depth: 0 |
44 | 62 | ref: ${{ steps.getRef.outputs.PR_REF || github.ref }} |
45 | 63 |
|
46 | | - - name: Add GitHub Comment |
47 | | - if: ${{ github.event_name == 'issue_comment' }} |
| 64 | + - name: Update deployment status (in_progress) |
48 | 65 | uses: actions/github-script@v7 |
49 | 66 | with: |
50 | 67 | script: | |
51 | | - github.rest.issues.createComment({ |
52 | | - issue_number: context.issue.number, |
| 68 | + await github.rest.repos.createDeploymentStatus({ |
53 | 69 | owner: context.repo.owner, |
54 | 70 | repo: context.repo.repo, |
55 | | - 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." |
56 | | - }) |
| 71 | + deployment_id: ${{ steps.create-deployment.outputs.deployment_id }}, |
| 72 | + state: 'in_progress', |
| 73 | + description: 'Deployment is in progress' |
| 74 | + }); |
57 | 75 |
|
58 | | - - name: Get PR number |
59 | | - if: ${{ github.event_name != 'issue_comment' }} |
60 | | - run: | |
61 | | - echo "GITHUB_REPOSITORY: \"$GITHUB_REPOSITORY\"" |
62 | | - if [ -z "$PR_NUMBER" ]; then |
63 | | - echo "PR_NUMBER is not in the trigger event. Fetching PR number from open PRs." |
64 | | - REF="${{ github.ref }}" |
65 | | - REF=${REF#refs/heads/} # Remove 'refs/heads/' prefix |
66 | | - echo "REF: \"$REF\"" |
67 | | - API_RESPONSE=$(curl --location --request GET "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls?state=open" \ |
68 | | - --header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}') |
69 | | - PR_NUMBER=$(echo "$API_RESPONSE" | jq '.[] | select(.head.ref=="'$REF'") | .number') |
70 | | - fi |
71 | | - echo "PR_NUMBER: $PR_NUMBER" |
72 | | - if [ -z "$PR_NUMBER" ]; then |
73 | | - echo "PR_NUMBER is not set. Aborting." |
74 | | - exit 1 |
75 | | - fi |
76 | | - echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
77 | | - - name: Get App Name |
78 | | - run: | |
79 | | - echo "PR_NUMBER: ${{ env.PR_NUMBER }}" |
80 | | - echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ env.PR_NUMBER }}" >> "$GITHUB_ENV" |
81 | | - echo "App Name: ${{ env.APP_NAME }}" |
82 | | - - uses: ./.github/actions/deploy-to-control-plane |
| 76 | + # ... rest of your existing steps ... |
| 77 | + |
| 78 | + - name: Update deployment status (success) |
| 79 | + if: success() |
| 80 | + uses: actions/github-script@v7 |
83 | 81 | with: |
84 | | - app_name: ${{ env.APP_NAME }} |
85 | | - org: ${{ env.CPLN_ORG }} |
| 82 | + script: | |
| 83 | + await github.rest.repos.createDeploymentStatus({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + deployment_id: ${{ steps.create-deployment.outputs.deployment_id }}, |
| 87 | + state: 'success', |
| 88 | + environment_url: '${{ steps.deploy.outputs.app_url }}', |
| 89 | + description: 'Deployment successful' |
| 90 | + }); |
| 91 | +
|
| 92 | + - name: Update deployment status (failure) |
| 93 | + if: failure() |
| 94 | + uses: actions/github-script@v7 |
| 95 | + with: |
| 96 | + script: | |
| 97 | + await github.rest.repos.createDeploymentStatus({ |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + deployment_id: ${{ steps.create-deployment.outputs.deployment_id }}, |
| 101 | + state: 'failure', |
| 102 | + description: 'Deployment failed' |
| 103 | + }); |
0 commit comments