|
| 1 | +name: RHEL7-openshift4-tests@TF |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: |
| 6 | + - created |
| 7 | +jobs: |
| 8 | + build: |
| 9 | + # This job only runs for '[test]' pull request comments by owner, member |
| 10 | + name: Schedule test on Testing Farm service for RHEL7 - OpenShift 4 |
| 11 | + runs-on: ubuntu-20.04 |
| 12 | + if: | |
| 13 | + github.event.issue.pull_request |
| 14 | + && contains(github.event.comment.body, '[test-openshift-4]') |
| 15 | + && contains(fromJson('["OWNER", "MEMBER"]'), github.event.comment.author_association) |
| 16 | + outputs: |
| 17 | + REQ_ID: ${{steps.sched_test.outputs.REQ_ID}} |
| 18 | + SHA: ${{steps.sha.outputs.SHA}} |
| 19 | + steps: |
| 20 | + - name: Get pull request number |
| 21 | + id: pr_nr |
| 22 | + run: | |
| 23 | + PR_URL="${{ github.event.comment.issue_url }}" |
| 24 | + echo "::set-output name=PR_NR::${PR_URL##*/}" |
| 25 | +
|
| 26 | + - name: Checkout repo |
| 27 | + uses: actions/checkout@v2 |
| 28 | + with: |
| 29 | + ref: "refs/pull/${{ steps.pr_nr.outputs.PR_NR }}/head" |
| 30 | + |
| 31 | + - name: Get sha |
| 32 | + id: sha |
| 33 | + run: | |
| 34 | + # Store SHA into outputs |
| 35 | + echo "::set-output name=SHA::$(git rev-parse HEAD)" |
| 36 | +
|
| 37 | + - name: Create status check to pending |
| 38 | + id: pending |
| 39 | + env: |
| 40 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + run: | |
| 42 | + # Create a JSON file for Testing Farm in order to schedule a CI testing job |
| 43 | + cat << EOF > pending.json |
| 44 | + { |
| 45 | + "sha": "${{steps.sha.outputs.SHA}}", |
| 46 | + "state": "pending", |
| 47 | + "context": "Testing Farm - RHEL7 - OpenShift 4", |
| 48 | + "target_url": "http://artifacts.osci.redhat.com/testing-farm/${{ steps.sched_test.outputs.req_id }}" |
| 49 | + } |
| 50 | + EOF |
| 51 | + echo "https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{steps.sha.outputs.SHA}}" |
| 52 | + # GITHUB_TOKEN is used for updating pull request status. |
| 53 | + # It is provided by GitHub https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret |
| 54 | + curl -X POST -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" \ |
| 55 | + https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{steps.sha.outputs.SHA}} \ |
| 56 | + --data @pending.json |
| 57 | + echo "::set-output name=GITHUB_REPOSITORY::$GITHUB_REPOSITORY" |
| 58 | +
|
| 59 | + - name: Schedule a test on Testing Farm forn RHEL7 - OpenShift 4 |
| 60 | + id: sched_test |
| 61 | + run: | |
| 62 | + # Update ubuntu-20.04 in order to install curl and jq |
| 63 | + apt update && apt -y install curl jq |
| 64 | + cat << EOF > request.json |
| 65 | + { |
| 66 | + "api_key": "${{ secrets.TF_INTERNAL_API_KEY }}", |
| 67 | + "test": {"fmf": { |
| 68 | + "url": "https://gitlab.cee.redhat.com/platform-eng-core-services/sclorg-tmt-plans", |
| 69 | + "ref": "master", |
| 70 | + "name": "rhel7-openshift-4" |
| 71 | + }}, |
| 72 | + "environments": [{ |
| 73 | + "arch": "x86_64", |
| 74 | + "os": {"compose": "RHEL-7.9-Released"}, |
| 75 | + "variables": { |
| 76 | + "REPO_URL": "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY", |
| 77 | + "REPO_NAME": "$GITHUB_REPOSITORY", |
| 78 | + "PR_NUMBER": "${{ steps.pr_nr.outputs.PR_NR }}", |
| 79 | + "OS": "rhel7", |
| 80 | + "TEST_NAME": "test-openshift-4" |
| 81 | + } |
| 82 | + }] |
| 83 | + } |
| 84 | + EOF |
| 85 | + curl ${{ secrets.TF_ENDPOINT }}/requests --data @request.json --header "Content-Type: application/json" --output response.json |
| 86 | + # Store REQ_ID into outputs for later on usage |
| 87 | + echo "::set-output name=REQ_ID::$(jq -r .id response.json)" |
| 88 | +
|
| 89 | + running: |
| 90 | + needs: build |
| 91 | + name: Check running tests on Testing Farm service |
| 92 | + runs-on: ubuntu-20.04 |
| 93 | + outputs: |
| 94 | + REQ_ID: ${{steps.req_sha.outputs.REQ_ID}} |
| 95 | + SHA: ${{steps.req_sha.outputs.SHA}} |
| 96 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 97 | + steps: |
| 98 | + - name: Check if REQ_ID and SHA exists |
| 99 | + id: req_sha |
| 100 | + run: | |
| 101 | + # Update ubuntu-20.04 in order to install curl and jq |
| 102 | + # each job is separate machine |
| 103 | + apt update && apt -y install curl jq |
| 104 | + # Propagate REQ_ID and SHA into the finish section |
| 105 | + echo "::set-output name=REQ_ID::${{needs.build.outputs.REQ_ID}}" |
| 106 | + echo "::set-output name=SHA::${{needs.build.outputs.SHA}}" |
| 107 | +
|
| 108 | + - name: Switch to running state of Testing Farm request |
| 109 | + id: running |
| 110 | + run: | |
| 111 | + # Create running.json file for query, whether job is finished or not. |
| 112 | + cat << EOF > running.json |
| 113 | + { |
| 114 | + "sha": "${{needs.build.outputs.SHA}}", |
| 115 | + "state": "pending", |
| 116 | + "context": "Testing Farm - RHEL7 - OpenShift 4", |
| 117 | + "description": "Build started", |
| 118 | + "target_url": "http://artifacts.osci.redhat.com/testing-farm/${{ needs.build.outputs.REQ_ID }}" |
| 119 | + } |
| 120 | + EOF |
| 121 | + # Update GitHub status description to 'Build started' |
| 122 | + curl -X POST -H "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" -H "Accept: application/vnd.github.v3+json" \ |
| 123 | + https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{needs.build.outputs.SHA}} \ |
| 124 | + --data @running.json |
| 125 | +
|
| 126 | + - name: Check test is still running |
| 127 | + id: still_running |
| 128 | + run: | |
| 129 | + CMD=${{ secrets.TF_ENDPOINT }}/requests/${{needs.build.outputs.REQ_ID}} |
| 130 | + curl $CMD > job.json |
| 131 | + state=$(jq -r .state job.json) |
| 132 | + # Wait till job is not finished. As soon as state is complete or failure then go to the finish action |
| 133 | + while [ "$state" == "running" ] || [ "$state" == "new" ] || [ "$state" == "pending" ] || [ "$state" == "queued" ]; do |
| 134 | + # Wait 30s. We do not need to query Testing Farm each second |
| 135 | + sleep 30 |
| 136 | + curl $CMD > job.json |
| 137 | + state=$(jq -r .state job.json) |
| 138 | + done |
| 139 | +
|
| 140 | + finish: |
| 141 | + needs: running |
| 142 | + name: Tests are finished - switching to proper state |
| 143 | + runs-on: ubuntu-20.04 |
| 144 | + steps: |
| 145 | + - name: Check if REQ_ID exists |
| 146 | + run: echo "${{ needs.running.outputs.REQ_ID }}" |
| 147 | + |
| 148 | + - name: Check if SHA exists |
| 149 | + run: echo "${{ needs.running.outputs.SHA }}" |
| 150 | + |
| 151 | + - name: Get final state of Testing Farm request |
| 152 | + id: final_state |
| 153 | + run: | |
| 154 | + # Update ubuntu-20.04 in order to install curl and jq |
| 155 | + # each job is separate machine |
| 156 | + apt update && apt -y install curl jq |
| 157 | + curl ${{ secrets.TF_ENDPOINT }}/requests/${{needs.running.outputs.REQ_ID}} > job.json |
| 158 | + cat job.json |
| 159 | + state=$(jq -r .state job.json) |
| 160 | + result=$(jq -r .result.overall job.json) |
| 161 | + new_state="success" |
| 162 | + infra_error=" " |
| 163 | + echo "State is $state and result is: $result" |
| 164 | + if [ "$state" == "complete" ]; then |
| 165 | + if [ "$result" != "passed" ]; then |
| 166 | + new_state="failure" |
| 167 | + fi |
| 168 | + else |
| 169 | + # Mark job in case of infrastructure issues. Report to Testing Farm team |
| 170 | + infra_error=" - Infra problems" |
| 171 | + new_state="failure" |
| 172 | + fi |
| 173 | + echo "New State is: $new_state" |
| 174 | + echo "Infra state is: $infra_error" |
| 175 | + echo "::set-output name=FINAL_STATE::$new_state" |
| 176 | + echo "::set-output name=INFRA_STATE::$infra_error" |
| 177 | +
|
| 178 | + - name: Switch to final state of Testing Farm request |
| 179 | + run: | |
| 180 | + cat << EOF > final.json |
| 181 | + { |
| 182 | + "sha": "${{needs.running.outputs.SHA}}", |
| 183 | + "state": "${{steps.final_state.outputs.FINAL_STATE}}", |
| 184 | + "context": "Testing Farm - RHEL7 - OpenShift 4", |
| 185 | + "description": "Build finished${{steps.final_state.outputs.INFRA_STATE}}", |
| 186 | + "target_url": "http://artifacts.osci.redhat.com/testing-farm/${{ needs.running.outputs.REQ_ID }}" |
| 187 | + } |
| 188 | + EOF |
| 189 | + cat final.json |
| 190 | + # Switch Github status to proper state |
| 191 | + curl -X POST -H "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" -H "Accept: application/vnd.github.v3+json" \ |
| 192 | + https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{needs.running.outputs.SHA}} \ |
| 193 | + --data @final.json |
0 commit comments