|
| 1 | +--- |
| 2 | +# When a push is made to a branch, deploy an instance of the app using |
| 3 | +# that branch. |
| 4 | +# The deployed url will be <pr-number>-<app-name>.<domain> |
| 5 | + |
| 6 | +name: PR Preview |
| 7 | +on: |
| 8 | + pull_request |
| 9 | +jobs: |
| 10 | + pr_preview: |
| 11 | + runs-on: ubuntu-20.04 |
| 12 | + timeout-minutes: 60 |
| 13 | + environment: |
| 14 | + name: Testing |
| 15 | + url: ${{ steps.set_subdomain.outputs.preview_url }} |
| 16 | + concurrency: |
| 17 | + group: ${{ github.ref }} |
| 18 | + cancel-in-progress: true |
| 19 | + steps: |
| 20 | + |
| 21 | + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." |
| 22 | + - run: echo "🐧 This job is now running on a ${{ runner.os }} server." |
| 23 | + - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." |
| 24 | + - name: Check out repository code |
| 25 | + uses: actions/checkout@v3 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." |
| 29 | + - name: List files in the repository |
| 30 | + run: | |
| 31 | + ls ${{ github.workspace }} |
| 32 | +
|
| 33 | +
|
| 34 | + - name: Prepare runner with ssh keys |
| 35 | + env: |
| 36 | + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} |
| 37 | + DOKKU_HOST: ${{ secrets.DOKKU_HOST }} |
| 38 | + run: | |
| 39 | + set -x |
| 40 | + mkdir -p ~/.ssh |
| 41 | + eval `ssh-agent -s` |
| 42 | + ssh-add - <<< "$SSH_PRIVATE_KEY" |
| 43 | + ssh-keyscan $DOKKU_HOST >> ~/.ssh/known_hosts |
| 44 | +
|
| 45 | + - name: Set subdomain (ensure is lowercase for dokku) |
| 46 | + id: set_subdomain |
| 47 | + run: | |
| 48 | + set -x |
| 49 | + echo SUBDOMAIN=`echo "${{ github.head_ref }}" | tr '[:upper:]' '[:lower:]' | cut -c -60` >> $GITHUB_ENV |
| 50 | + echo "::set-output name=preview_url::http://${{ github.head_ref }}.${{ secrets.DOKKU_DOMAIN }}" |
| 51 | +
|
| 52 | + - name: Create dokku app for pr branch if dosent already exist using dokku apps:create |
| 53 | + env: |
| 54 | + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} |
| 55 | + DOKKU_HOST: ${{ secrets.DOKKU_HOST }} |
| 56 | + DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }} |
| 57 | + run: | |
| 58 | + set -x |
| 59 | + echo The PR was raised by: ${{ github.event.pull_request.user.login }} |
| 60 | + eval `ssh-agent -s` |
| 61 | + ssh-add - <<< "$SSH_PRIVATE_KEY" |
| 62 | + ssh dokku@$DOKKU_HOST -C "dokku apps:unlock --force ${{ env.SUBDOMAIN }}" | true |
| 63 | + echo deleting dokku app ${{ github.head_ref }} |
| 64 | + ssh dokku@$DOKKU_HOST -C "dokku -- --force apps:destroy ${{ env.SUBDOMAIN }}" | true |
| 65 | + echo Creating dokku app ${{ github.head_ref }} |
| 66 | + ssh dokku@$DOKKU_HOST -C "dokku apps:create ${{ env.SUBDOMAIN }}" | true |
| 67 | + ssh dokku@$DOKKU_HOST -C dokku builder:set ${{ env.SUBDOMAIN }} build-dir src |
| 68 | + ssh dokku@$DOKKU_HOST -C "dokku builder-dockerfile:set ${{ env.SUBDOMAIN }} dockerfile-path Dockerfile" |
| 69 | + ssh dokku@$DOKKU_HOST -C "dokku git:initialize ${{ env.SUBDOMAIN }}" |
| 70 | + ssh dokku@$DOKKU_HOST -C "dokku git:set ${{ env.SUBDOMAIN }} deploy-branch ${{ github.head_ref }}" |
| 71 | +
|
| 72 | + - name: Deploy branch ${{ github.head_ref }} to dokku |
| 73 | + uses: idoberko2/dokku-deploy-github-action@v1 |
| 74 | + with: |
| 75 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 76 | + dokku-host: ${{ secrets.DOKKU_HOST }} |
| 77 | + app-name: ${{ env.SUBDOMAIN }} |
| 78 | + git-push-flags: '--force' |
| 79 | + remote-branch: ${{ github.head_ref }} |
| 80 | + |
| 81 | + - name: Click to see your PR web address |
| 82 | + env: |
| 83 | + DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }} |
| 84 | + run: | |
| 85 | + echo Visit your pr here: ${{ steps.set_subdomain.outputs.preview_url }} |
| 86 | +
|
| 87 | + - name: 'Comment PR with web address of application live preview' |
| 88 | + env: |
| 89 | + DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }} |
| 90 | + uses: actions/github-script@v3 |
| 91 | + if: github.event_name == 'pull_request' |
| 92 | + with: |
| 93 | + script: | |
| 94 | + github.issues.createComment({ |
| 95 | + issue_number: context.issue.number, |
| 96 | + owner: context.repo.owner, |
| 97 | + repo: context.repo.repo, |
| 98 | + body: "🙌 Live preview is here: ${{ steps.set_subdomain.outputs.preview_url }}" |
| 99 | + }) |
| 100 | +
|
0 commit comments