From 4efb3d44cf2d10c851a084c9d4a3b15998721eab Mon Sep 17 00:00:00 2001 From: Alvin Huang <17609145+alvin-huang@users.noreply.github.com> Date: Wed, 13 Jan 2021 23:20:19 -0500 Subject: [PATCH] add github action to trigger load tests based on PR label --- .github/workflows/load-test.yml | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .github/workflows/load-test.yml diff --git a/.github/workflows/load-test.yml b/.github/workflows/load-test.yml new file mode 100644 index 0000000000..91f9536eba --- /dev/null +++ b/.github/workflows/load-test.yml @@ -0,0 +1,63 @@ +on: + pull_request: + branches: + - master + types: [labeled] + +jobs: + trigger-load-test: + if: ${{ github.event.label.name == 'test/load' }} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: Trigger CircleCI Load Test Pipeline + run: | + # build json payload to trigger CircleCI Load Test Pipeline + # This only runs the load test pipeline on the 'master' branch + jsonData=$(jq --null-input -r --arg commit ${{ github.event.pull_request.head.sha }} \ + '{branch:"master", parameters: {"commit": $commit, "trigger-load-test": true}}') + echo "Passing JSON data to CircleCI API: $jsonData" + load_test_pipeline_id=$(curl -X POST \ + -H "Circle-Token: ${{ secrets.CIRCLE_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "$jsonData" \ + "https://circleci.com/api/v2/project/gh/${GITHUB_REPOSITORY}/pipeline" | jq -r '.id') + echo "LOAD_TEST_PIPELINE_ID=$load_test_pipeline_id" >> $GITHUB_ENV + - name: Post Load Test URL to PR + env: + PR_COMMENT_URL: ${{ github.event.pull_request.comments_url }} + run: | + echo "LOAD_TEST_PIPELINE_ID is: $LOAD_TEST_PIPELINE_ID" + # get load-test workflow + workflow= + # wait up to a minute for load-test workflow to start + until [ $SECONDS -ge 60 ] && exit 1; do + workflow=$(curl -s -X GET \ + -H "Circle-Token: ${{ secrets.CIRCLE_TOKEN }}" \ + -H "Content-Type: application/json" \ + "https://circleci.com/api/v2/pipeline/${LOAD_TEST_PIPELINE_ID}/workflow" | jq '.items[] | select(.name=="load-test")') + # if we found a workflow we exit + if [ -n "$workflow" ]; then + break + fi + echo -n "." + sleep 5 + done + echo "$workflow" + # get pipeline number + pipeline_number=$(echo "$workflow" | jq -r '.pipeline_number') + # get workflow id + workflow_id=$(echo "$workflow" | jq -r '.id') + # get project slug + project_slug=$(echo "$workflow" | jq -r '.project_slug') + # build load test URL + load_test_url="https://app.circleci.com/pipelines/${project_slug}/${pipeline_number}/workflows/${workflow_id}" + # comment URL to pull request + curl -X POST \ + -H "Authorization: token ${{ secrets.PR_COMMENT_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "{\"body\": \"Load Test Pipeline Started at: $load_test_url\"}" \ + "$PR_COMMENT_URL"