diff --git a/.github/workflows/prod-deploy.yml b/.github/workflows/prod-deploy.yml index 8f51ae0..da7a4a9 100644 --- a/.github/workflows/prod-deploy.yml +++ b/.github/workflows/prod-deploy.yml @@ -9,76 +9,21 @@ on: jobs: deploy: - name: Deploy to production runs-on: ubuntu-latest + + # deploy to the production environment + environment: + name: production + url: http://github-actions-demo-app.dokku.proxima-web.com + steps: - name: Cloning repo uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Create deployment - id: create_deployment - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments - repository: ${{ github.repository }} - ref: ${{ github.sha }} - environment: production - required_contexts: '[]' - production_environment: true - auto_merge: false - env: - GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' - - - name: Set deployment status to in progress - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: production - environment_url: http://github-actions-demo-app.dokku.proxima-web.com - log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - state: in_progress - mediaType: '{"previews": ["flash", "ant-man"]}' # required for setting in_progress state and environment_url - env: - GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' - - name: Push to dokku uses: dokku/github-action@master with: git_remote_url: 'ssh://dokku@dokku.proxima-web.com:22/github-actions-demo-app' ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} - - - name: Set GitHub deployment status to success - id: successful_deployment - if: success() - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: review - environment_url: http://github-actions-demo-app.dokku.proxima-web.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 GitHub deployment status to failure - id: failed_deployment - uses: octokit/request-action@v2.0.26 - if: failure() - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: review - environment_url: http://github-actions-demo-app.dokku.proxima-web.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 }}' diff --git a/.github/workflows/review-app.yml b/.github/workflows/review-app.yml index 0f7b110..04dcf6a 100644 --- a/.github/workflows/review-app.yml +++ b/.github/workflows/review-app.yml @@ -3,48 +3,51 @@ name: 'Deploy Review App' # yamllint disable-line rule:truthy on: - # onl run this workflow on pull request events - pull_request + # only run this workflow on pull request events + pull_request: + types: [opened, synchronize, closed] jobs: review_app: runs-on: ubuntu-latest - # only run when a pull request is opened - if: github.event_name == 'pull_request' - environment: review + # only run when a pull request is opened or synchronized + if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'synchronize') + + # deploy to the review environment + environment: + name: review + url: http://github-actions-demo-app-${{ github.event.pull_request.number }}.dokku.proxima-web.com + steps: - name: Cloning repo uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Create deployment - id: create_deployment - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments - repository: ${{ github.repository }} - ref: ${{ github.event.pull_request.head.ref }} - environment: review - required_contexts: '[]' - production_environment: true - auto_merge: false + - name: Dump GitHub context env: - GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' - - - name: Set deployment status to in progress - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: review - environment_url: http://github-actions-demo-app-${{ github.event.pull_request.number }}.dokku.proxima-web.com - log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - state: in_progress - mediaType: '{"previews": ["flash", "ant-man"]}' # required for setting in_progress state and environment_url + GITHUB_CONTEXT: ${{ toJSON(github) }} + run: echo "$GITHUB_CONTEXT" + - name: Dump job context + env: + JOB_CONTEXT: ${{ toJSON(job) }} + run: echo "$JOB_CONTEXT" + - name: Dump steps context + env: + STEPS_CONTEXT: ${{ toJSON(steps) }} + run: echo "$STEPS_CONTEXT" + - name: Dump runner context + env: + RUNNER_CONTEXT: ${{ toJSON(runner) }} + run: echo "$RUNNER_CONTEXT" + - name: Dump strategy context env: - GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' + STRATEGY_CONTEXT: ${{ toJSON(strategy) }} + run: echo "$STRATEGY_CONTEXT" + - name: Dump matrix context + env: + MATRIX_CONTEXT: ${{ toJSON(matrix) }} + run: echo "$MATRIX_CONTEXT" - name: Push to dokku uses: dokku/github-action@master @@ -57,39 +60,6 @@ jobs: review_app_name: github-actions-demo-app-${{ github.event.pull_request.number }} ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} - - name: Set GitHub deployment status to success - id: successful_deployment - if: success() - uses: octokit/request-action@v2.0.26 - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: review - environment_url: http://github-actions-demo-app-${{ github.event.pull_request.number }}.dokku.proxima-web.com - log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - mediaType: '{"previews": ["ant-man"]}' - state: success - auto_inactive: false - env: - GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' - - - name: Set GitHub deployment status to failure - id: failed_deployment - uses: octokit/request-action@v2.0.26 - if: failure() - with: - route: POST /repos/:repository/deployments/:deployment/statuses - repository: ${{ github.repository }} - deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} - environment: review - environment_url: http://github-actions-demo-app-${{ github.event.pull_request.number }}.dokku.proxima-web.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 }}' - destroy_review_app: runs-on: ubuntu-latest # only run when a pull request is closed diff --git a/README.md b/README.md index 644cec9..6c417f1 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,51 @@ # Dokku GitHub Action Example -An example repo showing how to use the official [dokku GitHub action](https://github.com/dokku/github-action) to deploy your app with continuous delivery. +How to use the official [dokku GitHub action](https://github.com/dokku/github-action) to deploy your app using continuous delivery with GitHub Actions and GitHub Deploys. -## GitHub Secrets +GitHub Deploys is still in public beta but it's stablish and is a useful mechanism to manage your deployment workflows. You can use [environment protection rules](https://docs.github.com/en/actions/reference/environments#environment-protection-rules) to give you granular control over [who can deploy](https://docs.github.com/en/actions/managing-workflow-runs/reviewing-deployments) and to where. -The following secrets must be set: +## Workflows -- `SSH_PRIVATE_KEY` +This repository demonstrates how to: -You'll see reference to a secret called `GITHUB_TOKEN` in the workflow files, but there's no need to manually set this yourself as this is set by GitHub Actions. +- Deploy `review` apps to a `review` environment when a pull request is created or updated. +- Deploy a `production` app to a `production` environment on changes to the main/master branch. -If you want to see debug information, you can create a secret called `ACTIONS_STEP_DEBUG` with a value of `true`. +You'll need to create the following following environments in the repository settings: -## dokku Setup +- production +- review -```bash -# on server -dokku apps:create github-actions-demo-app -``` +Take advantage of the [environment protection rules](https://docs.github.com/en/actions/reference/environments#environment-protection-rules) to enable deploy restrictions. -```bash -# on local -git remote add dokku dokku@dokku.proxima-web.com:github-actions-demo-app -git push dokku -``` +### Review Apps -Visit http://github-actions-demo-app.dokku.proxima-web.com/ to confirm the deployment was successful. +Each time a pull request is opened or updated a new review app is deployed to url `http://github-actions-demo-app-${{ github.event.pull_request.number }}.dokku.proxima-web.com`. -## Review Apps +#### Screenshots -Each time a pull request is opened a new review app is deployed. +The following screenshots shows the deploy workflow when the `review` environment requires reviewers: -The [example workflow](./.github/workflows/review-app.yml) uses [GitHub Deploys](https://docs.github.com/en/rest/reference/repos#deployments) and requires the following [environments](https://docs.github.com/en/actions/reference/environments) to be created: +##### Pull request created, but deploy is awaiting approval -- production -- review + + +##### GitHub Actions deploy workflow awaiting approval + + + +##### Approving the deploy workflow + + + +##### Deployment in progress + + + +##### Deployment successful + + + +### Production App -You can add new environments in your repo settings. You can also take advantage of [environment protection rules](https://docs.github.com/en/actions/reference/environments#environment-protection-rules) to request reviews before deployments can take place. +Each time the main/master branch is created or updated a new production app is deployed to `http://github-actions-demo-app.dokku.proxima-web.com`. diff --git a/screenshots/awaiting-approval.png b/screenshots/awaiting-approval.png new file mode 100644 index 0000000..d88e45a Binary files /dev/null and b/screenshots/awaiting-approval.png differ diff --git a/screenshots/deploy-started.png b/screenshots/deploy-started.png new file mode 100644 index 0000000..85ab51b Binary files /dev/null and b/screenshots/deploy-started.png differ diff --git a/screenshots/github-actions-approve-deploy.png b/screenshots/github-actions-approve-deploy.png new file mode 100644 index 0000000..f1375ed Binary files /dev/null and b/screenshots/github-actions-approve-deploy.png differ diff --git a/screenshots/github-actions-review.png b/screenshots/github-actions-review.png new file mode 100644 index 0000000..e6ba156 Binary files /dev/null and b/screenshots/github-actions-review.png differ diff --git a/screenshots/successful-deploy.png b/screenshots/successful-deploy.png new file mode 100644 index 0000000..574abd4 Binary files /dev/null and b/screenshots/successful-deploy.png differ