Skip to content

Commit

Permalink
Fix - GHA are not triggered in case of merge (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Dhenain Ambroise <ambroise.dhenain@gmail.com>
  • Loading branch information
Demmonius and Vadorequest authored Jun 16, 2020
1 parent 1552b75 commit ee4cd97
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/deploy-zeit-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ name: Deploy to Zeit (production)

on:
# There are several ways to trigger Github actions - See https://help.github.com/en/actions/reference/events-that-trigger-workflows#example-using-a-single-event for a comprehensive list:
# "push": Triggers each time a commit is pushed
# "pull_request": Triggers each time a commit is pushed within a pull request
# In production, we use the "push" trigger, because we want to deploy whether any pushed commit, whether it belongs to a PR.
push:
# - "push": Triggers each time a commit is pushed
# - "pull_request": Triggers each time a commit is pushed within a pull request, it makes it much easier to write comments within the PR, but it suffers some strong limitations:
# - There is no way to trigger when a PR is merged into another - See https://github.saobby.my.eu.orgmunity/t/pull-request-action-does-not-run-on-merge/16092?u=vadorequest
# - It won't trigger when the PR is conflicting with its base branch - See https://github.saobby.my.eu.orgmunity/t/run-actions-on-pull-requests-with-merge-conflicts/17104/2?u=vadorequest
push: # Triggers on each pushed commit
branches:
- 'master'

Expand All @@ -34,10 +35,15 @@ jobs:
needs: setup-environment
steps:
- uses: actions/checkout@v1 # Get last commit pushed - XXX See https://github.com/actions/checkout
- name: Deploying on Zeit
- name: Deploying on Zeit (production)
# Workflow:
# Deploy the customer in production
run: yarn deploy:$(cat now.json | jq -r '.build.env.NEXT_PUBLIC_CUSTOMER_REF'):production --token $ZEIT_TOKEN
run: |
# Print the version of the "now" CLI being used (helps debugging)
now --version
# Deploy the customer on Vercel using the customer specified in now.json (e.g: "yarn deploy:customer1:production")
yarn deploy:$(cat now.json | jq -r '.build.env.NEXT_PUBLIC_CUSTOMER_REF'):production --token $ZEIT_TOKEN
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }} # Passing github's secret to the worker

Expand Down
86 changes: 68 additions & 18 deletions .github/workflows/deploy-zeit-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ name: Deploy to Zeit (staging)

on:
# There are several ways to trigger Github actions - See https://help.github.com/en/actions/reference/events-that-trigger-workflows#example-using-a-single-event for a comprehensive list:
# "push": Triggers each time a commit is pushed
# "pull_request": Triggers each time a commit is pushed within a pull request
# In staging, we use the "pull_request" trigger, because it makes it much easier to write comments within the PR.
pull_request: # Triggers on each pushed commit associated with a pull request
# - "push": Triggers each time a commit is pushed
# - "pull_request": Triggers each time a commit is pushed within a pull request, it makes it much easier to write comments within the PR, but it suffers some strong limitations:
# - There is no way to trigger when a PR is merged into another - See https://github.saobby.my.eu.orgmunity/t/pull-request-action-does-not-run-on-merge/16092?u=vadorequest
# - It won't trigger when the PR is conflicting with its base branch - See https://github.saobby.my.eu.orgmunity/t/run-actions-on-pull-requests-with-merge-conflicts/17104/2?u=vadorequest
push: # Triggers on each pushed commit
branches-ignore:
- 'master'

Expand All @@ -34,17 +35,27 @@ jobs:
needs: setup-environment
steps:
- uses: actions/checkout@v1 # Get last commit pushed - XXX See https://github.com/actions/checkout
- name: Deploying on Zeit
- name: Deploying on Zeit (staging)
# Workflow:
# - Get stdout from deploy command (stderr shows build steps and stdout shows final url, which is what we are really looking for)
# - Set the deployment url that will be included in the eventual PR comment
# - Create a branch alias and link it to the deployment (so that each branch has its own domain automatically aliased to the lastest commit)
run: |
# Print the version of the "now" CLI being used (helps debugging)
now --version
# Deploy the customer on Vercel using the customer specified in now.json (e.g: "yarn deploy:customer1")
# Store the output in a variable so we can extract metadata from it
ZEIT_DEPLOYMENT_OUTPUT=`yarn deploy:$(cat now.json | jq -r '.build.env.NEXT_PUBLIC_CUSTOMER_REF') --token $ZEIT_TOKEN`
# Extract the Vercel deployment url from the deployment output
ZEIT_DEPLOYMENT_URL=`echo $ZEIT_DEPLOYMENT_OUTPUT | egrep -o 'https?://[^ ]+.vercel.app'`
echo "::set-env name=ZEIT_DEPLOYMENT_URL::$ZEIT_DEPLOYMENT_URL"
echo "Deployment url: " $ZEIT_DEPLOYMENT_URL
# Build the alias domain based on our own rules (NRN own rules)
# Basically, if a branch starts with "v[0-9]" then we consider it to be a "preset branch" and we use the branch name as alias (e.g: "v2-mst-xxx")
# Otherwise, we use the deployment name and suffix it using the branch name (e.g: "v2-mst-xxx-branch-name")
if [[ ${CURRENT_BRANCH##*/} =~ ^v[0-9]{1,}- ]]; then # Checking if pattern matches with "vX-" where X is a number
ZEIT_DEPLOYMENT_ALIAS=${CURRENT_BRANCH##*/}
else
Expand All @@ -53,43 +64,53 @@ jobs:
# Zeit alias only allows 41 characters in the domain name, so we only keep the first 41 (41 = 53 - 11 - 1) characters (because Zeit needs 7 chars for ".vercel.app" at the end of the domain name, and count starts at 1, not 0)
# Also, in order to remove forbidden characters, we transform every characters which are not a "alnum" and \n or \r into '-'
ZEIT_DEPLOYMENT_ALIAS=$(echo $ZEIT_DEPLOYMENT_ALIAS | head -c 41 | tr -c '[:alnum:]\r\n' - | tr '[:upper:]' '[:lower:]')
# Recursively remove any trailing dash ('-')
# Recursively remove any trailing dash ('-') to protect against invalid alias domain (not allowed to end with a trailing slash)
while [[ "$ZEIT_DEPLOYMENT_ALIAS" == *- ]]
do
ZEIT_DEPLOYMENT_ALIAS=${ZEIT_DEPLOYMENT_ALIAS::-1}
ZEIT_DEPLOYMENT_ALIAS=${ZEIT_DEPLOYMENT_ALIAS::-1}
done
# Build alias url and create Vercel alias
ZEIT_DEPLOYMENT_ALIAS=$ZEIT_DEPLOYMENT_ALIAS.vercel.app
echo "::set-env name=ZEIT_DEPLOYMENT_ALIAS::https://$ZEIT_DEPLOYMENT_ALIAS"
echo "Alias domain: " $ZEIT_DEPLOYMENT_ALIAS
echo "Aliasing the deployment using the git branch alias:"
echo "npx now alias " $ZEIT_DEPLOYMENT_URL " https://" $ZEIT_DEPLOYMENT_ALIAS
npx now alias $ZEIT_DEPLOYMENT_URL https://$ZEIT_DEPLOYMENT_ALIAS --token $ZEIT_TOKEN
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }} # Passing github's secret to the worker
CURRENT_BRANCH: ${{ github.ref }} # Passing current branch to the worker

# We need to find the PR id. Will be used later to comment on that PR.
- name: Finding Pull Request ID
uses: jwalton/gh-find-current-pr@v1
id: pr_id_finder
if: always() # It forces the job to be always executed, even if a previous job fail.
with:
github-token: ${{ secrets.GITHUB_CI_PR_COMMENT }}

# On deployment failure, add a comment to the PR
- name: Comment PR (Deployment failure)
uses: peter-evans/create-or-update-comment@v1
if: failure()
with:
token: ${{ secrets.GITHUB_CI_PR_COMMENT }}
issue-number: ${{ github.event.number }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
[GitHub Actions]
Deployment **FAILED**
Commit ${{ github.sha }} failed to deploy to ${{ env.ZEIT_DEPLOYMENT_URL }}
[click to see logs](https://github.com/UnlyEd/next-right-now/pull/${{ github.event.number }}/checks)
[click to see logs](https://github.com/UnlyEd/next-right-now/pull/${{ steps.pr_id_finder.outputs.number }}/checks)
# On deployment success, add a comment to the PR
- name: Comment PR (Deployment success)
uses: peter-evans/create-or-update-comment@v1
if: success()
with:
token: ${{ secrets.GITHUB_CI_PR_COMMENT }}
issue-number: ${{ github.event.number }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
[GitHub Actions]
Deployment **SUCCESS**
Expand Down Expand Up @@ -141,25 +162,33 @@ jobs:
name: videos
path: cypress/videos/

# We need to find the PR id. Will be used later to comment on that PR.
- name: Finding Pull Request ID
uses: jwalton/gh-find-current-pr@v1
id: pr_id_finder
if: always() # It forces the job to be always executed, even if a previous job fail.
with:
github-token: ${{ secrets.GITHUB_CI_PR_COMMENT }}

# On E2E failure, add a comment to the PR with additional information
- name: Comment PR (E2E failure)
uses: peter-evans/create-or-update-comment@v1
if: failure()
with:
token: ${{ secrets.GITHUB_CI_PR_COMMENT }}
issue-number: ${{ github.event.number }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
[GitHub Actions]
E2E tests **FAILED**
Download artifacts (screenshots + videos) from [`checks`](https://github.com/UnlyEd/next-right-now/pull/${{ github.event.number }}/checks) section
Download artifacts (screenshots + videos) from [`checks`](https://github.com/UnlyEd/next-right-now/pull/${{ steps.pr_id_finder.outputs.number }}/checks) section
# On E2E success, add a comment to the PR
- name: Comment PR (E2E success)
uses: peter-evans/create-or-update-comment@v1
if: success()
with:
token: ${{ secrets.GITHUB_CI_PR_COMMENT }}
issue-number: ${{ github.event.number }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
[GitHub Actions]
E2E tests **SUCCESS**
Expand Down Expand Up @@ -195,8 +224,8 @@ jobs:
uses: foo-software/lighthouse-check-action@v1.0.14
id: lighthouseCheck
with: # See https://github.com/marketplace/actions/lighthouse-check#inputs for all options
accessToken: ${{ secrets.GITHUB_CI_PR_COMMENT }} # Providing a token to comment the PR.
prCommentEnabled: true # Whether to comment on the PR (default: true).
#accessToken: ${{ secrets.GITHUB_CI_PR_COMMENT }} # Providing a token to comment the PR if you are using a "pull_request" as trigger.
prCommentEnabled: false # Whether to comment on the PR (default: true). Disabled because we use our own "Comment PR (LightHouse report)" action
prCommentSaveOld: true # If true, then add a new comment for each commit. Otherwise, update the latest comment (default: false).
outputDirectory: /tmp/lighthouse-artifacts # Used to upload artifacts.
urls: ${{ env.ZEIT_DEPLOYMENT_URL }}
Expand All @@ -210,7 +239,6 @@ jobs:

# Using a pre-build action to make the action fail if your score is too low. It can be really interesting to track a low score on a commit
# You can remove this action IF you don't want lighthouse to be a blocking point in your CI
# This default values so you need to change them by your own criteria
# Official documentation: https://github.com/foo-software/lighthouse-check-status-action
- name: Handle Lighthouse Check results
uses: foo-software/lighthouse-check-status-action@v1.0.1
Expand All @@ -221,3 +249,25 @@ jobs:
minPerformanceScore: "30"
minProgressiveWebAppScore: "50"
minSeoScore: "50"

# We need to find the PR id. Will be used later to comment on that PR.
- name: Finding Pull Request ID
uses: jwalton/gh-find-current-pr@v1
id: pr_id_finder
if: always() # It forces the job to be always executed, even if a previous job fail.
with:
github-token: ${{ secrets.GITHUB_CI_PR_COMMENT }}

# Add a comment to the PR with lighthouse report
- name: Comment PR (LightHouse report)
uses: peter-evans/create-or-update-comment@v1
with:
token: ${{ secrets.GITHUB_CI_PR_COMMENT }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
# Report provided by steps.lighthouseCheck.outputs.lighthouseCheckResults is a string and need to be transform into an object.
# Then, using badges with results provided by `data[0].scores.<category>`
# data is an array containing results of every urls tested. Thankfully, we only test one url so we don't care about the index.
# See "fromJson" documentation: https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson
body: |
[GitHub Actions] - **Lighthouse report** for ${{ env.ZEIT_DEPLOYMENT_URL }}
![](https://img.shields.io/badge/Accessibility-${{ fromJson(steps.lighthouseCheck.outputs.lighthouseCheckResults).data[0].scores.accessibility }}-green?style=flat-square) ![](https://img.shields.io/badge/Best%20Practices-${{ fromJson(steps.lighthouseCheck.outputs.lighthouseCheckResults).data[0].scores.bestPractices }}-green?style=flat-square) ![](https://img.shields.io/badge/Performance-${{ fromJson(steps.lighthouseCheck.outputs.lighthouseCheckResults).data[0].scores.performance }}-orange?style=flat-square) ![](https://img.shields.io/badge/Progressive%20Web%20App-${{ fromJson(steps.lighthouseCheck.outputs.lighthouseCheckResults).data[0].scores.progressiveWebApp }}-orange?style=flat-square) ![](https://img.shields.io/badge/SEO-${{ fromJson(steps.lighthouseCheck.outputs.lighthouseCheckResults).data[0].scores.seo }}-green?style=flat-square)

0 comments on commit ee4cd97

Please sign in to comment.