Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderKnape committed May 3, 2020
0 parents commit a5d0895
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
101 changes: 101 additions & 0 deletions .github/workflows/pr-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: pr-deployment

on:
issue_comment:
types: [created]

jobs:
deploy-check:
runs-on: ubuntu-latest
steps:
- name: acknowledge deployment request to commenter
id: check
uses: khan/pull-request-comment-trigger@master
with:
trigger: "/deploy"
reaction: rocket
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
outputs:
triggered: ${{ steps.check.outputs.triggered }}

deploy:
runs-on: ubuntu-latest
needs: deploy-check
if: needs.deploy-check.outputs.triggered == 'true'
steps:
- name: get pull request ref
id: get_pull_request_ref
uses: octokit/[email protected]
with:
route: GET /repos/:repository/pulls/:issue_id
repository: ${{ github.repository }}
issue_id: ${{ github.event.issue.number }}
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

- name: create deployment
id: create_deployment
uses: octokit/[email protected]
with:
route: POST /repos/:repository/deployments
repository: ${{ github.repository }}
ref: ${{ fromJson(steps.get_pull_request_ref.outputs.data).head.ref }}
environment: dev
auto_merge: false
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

- name: set deployment status to in progress
id: start_deployment
uses: octokit/[email protected]
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
environment: dev
environment_url: https://example.com
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
state: in_progress
mediaType: '{"previews": ["flash", "ant-man"]}'
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

- name: deploy the pull request
run: |
# deployment logic goes here
sleep 10
# instead we randomly succeed or fail the deployment
exit $(( $RANDOM % 10 >= 5 ))
- name: set deployment status to success
id: successful_deployment
uses: octokit/[email protected]
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
environment: dev
environment_url: https://example.com
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
mediaType: '{"previews": ["ant-man"]}'
state: success
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

- name: set deployment status to failure
id: failed_deployment
uses: octokit/[email protected]
if: failure()
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
environment: dev
environment_url: https://example.com
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
mediaType: '{"previews": ["ant-man"]}'
state: failure
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Pull request deployments with GitHub Actions and GitHub Deployments

The workflow in this repository shows how a `/deploy` comment on a pull request can deploy the source code to an environment. In addition, the user is kept up-to-date on the deployment status using the GitHub Deployments construct.

More information about this workflow can be found in my blog post: [Deploy your pull requests with GitHub Actions and GitHub Deployments](https://sanderknape.com/2020/05/deploy-pull-requests-github-actions-deployments/)

0 comments on commit a5d0895

Please sign in to comment.