|
| 1 | +name: Trigger amd-debug Buildbot Build |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + pull_request: |
| 5 | + branches: [amd-debug] |
| 6 | + types: [opened, reopened, synchronize, ready_for_review] |
| 7 | + |
| 8 | + |
| 9 | +jobs: |
| 10 | + trigger-build: |
| 11 | + if: github.event.pull_request.draft == false |
| 12 | + runs-on: self-hosted |
| 13 | + env: |
| 14 | + PR_SHA: ${{ github.event.pull_request.head.sha != '' && github.event.pull_request.head.sha || github.sha }} |
| 15 | + PR_NUMBER: ${{ github.event.pull_request.number != '' && github.event.pull_request.number || 0 }} |
| 16 | + PR_URL: ${{ github.event.pull_request.html_url != '' && github.event.pull_request.html_url || '' }} |
| 17 | + PR_TITLE: ${{ github.event.pull_request.title != '' && github.event.pull_request.title || '' }} |
| 18 | + BASE_BRANCH: ${{ github.event.pull_request.base.ref != '' && github.event.pull_request.base.ref || '' }} |
| 19 | + GITHUB_TOKEN: ${{secrets.CI_GITHUB_TOKEN}} |
| 20 | + |
| 21 | + steps: |
| 22 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 23 | + - name: Set environment variable for container image |
| 24 | + run: | |
| 25 | + echo "CONTAINER_IMAGE=${{ secrets.BUILDBOT_DOCKER_IMAGE }}" >> $GITHUB_ENV |
| 26 | + echo "CONTAINER_NAME=my_container_${{ github.run_id }}" >> $GITHUB_ENV |
| 27 | + |
| 28 | + - name: Pull container image |
| 29 | + run: docker pull "${{env.CONTAINER_IMAGE}}" |
| 30 | + |
| 31 | + - name: Run container |
| 32 | + run: | |
| 33 | + docker run -d --name "${{env.CONTAINER_NAME}}" $CONTAINER_IMAGE sleep infinity |
| 34 | + docker exec "${{env.CONTAINER_NAME}}" /bin/bash -c "echo 'Running commands inside the container'" |
| 35 | +
|
| 36 | + - name: Escape pull request title |
| 37 | + run: | |
| 38 | + import json |
| 39 | + import os |
| 40 | + import shlex |
| 41 | + with open('${{ github.event_path }}') as fh: |
| 42 | + event = json.load(fh) |
| 43 | + escaped = event['pull_request']['title'] |
| 44 | + with open(os.environ['GITHUB_ENV'], 'a') as fh: |
| 45 | + print(f'PR_TITLE={escaped}', file=fh) |
| 46 | + shell: python3 {0} |
| 47 | + |
| 48 | + - name: Trigger Buildbot Build |
| 49 | + run: | |
| 50 | + echo "${{ secrets.BUILDBOT_HOST }}:${{ secrets.BUILDBOT_WORKER_PORT }}" |
| 51 | + docker exec -e PR_TITLE="$PR_TITLE" "${{env.CONTAINER_NAME}}" /bin/bash -c 'buildbot sendchange -W ${{ secrets.BUILDBOT_USER }} -a ${{secrets.BUILDBOT_USER}}:${{secrets.BUILDBOT_PWD}} --master="${{ secrets.BUILDBOT_HOST }}:${{ secrets.BUILDBOT_WORKER_PORT }}" --branch=${{ env.BASE_BRANCH }} --revision=${{ env.PR_SHA }} -p PR_NUMBER:${{ env.PR_NUMBER }} -p PR_TITLE:"$PR_TITLE" -p PR_URL:${{ env.PR_URL }} -p SHA:${{ env.PR_SHA }}' |
| 52 | + |
| 53 | + - name: Set Initial Status to Pending |
| 54 | + run: | |
| 55 | + docker exec -e PR_SHA=$PR_SHA -e GITHUB_TOKEN=$GITHUB_TOKEN "${{env.CONTAINER_NAME}}" /bin/bash -c "python3 -c \" |
| 56 | + import os |
| 57 | + import requests |
| 58 | + GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') |
| 59 | + TARGET_SHA = os.getenv('PR_SHA') |
| 60 | + print('debug', TARGET_SHA) |
| 61 | + api_url = f'https://api.github.com/repos/AMD-Lightning-Internal/llvm-project/statuses/{TARGET_SHA}' |
| 62 | + headers = { |
| 63 | + 'Authorization': f'token {GITHUB_TOKEN}', |
| 64 | + 'Content-Type': 'application/json' |
| 65 | + } |
| 66 | + payload = { |
| 67 | + 'state': 'pending', |
| 68 | + 'context': 'buildbot', |
| 69 | + 'description': 'Build is in queue' |
| 70 | + } |
| 71 | + response = requests.post(api_url, json=payload, headers=headers) |
| 72 | + if response.status_code == 201: |
| 73 | + print('Status set to pending successfully.') |
| 74 | + else: |
| 75 | + print(f'Failed to set status: {response.status_code} {response.text}') |
| 76 | + \"" |
| 77 | +
|
| 78 | + - name: Poll Buildbot build status |
| 79 | + run: | |
| 80 | + python3 -c " |
| 81 | + import os |
| 82 | + import time |
| 83 | + import requests |
| 84 | + GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') |
| 85 | + BUILD_URL = 'http://${{ secrets.BUILDBOT_HOST }}:${{ secrets.BUILDBOT_MASTER_PORT }}/api/v2/builds' |
| 86 | + TARGET_SHA = os.getenv('PR_SHA') |
| 87 | + print('debug', TARGET_SHA) |
| 88 | + MAX_RETRIES = 10 |
| 89 | + RETRY_INTERVAL = 30 # seconds |
| 90 | +
|
| 91 | + def get_build_properties(build_id): |
| 92 | + build_properties_url = f'http://${{ secrets.BUILDBOT_HOST }}:${{ secrets.BUILDBOT_MASTER_PORT }}/api/v2/builds/{build_id}/properties' |
| 93 | + response = requests.get(build_properties_url, headers={'Accept': 'application/json', 'Authorization': f'token {GITHUB_TOKEN}'}) |
| 94 | + return response.json() |
| 95 | +
|
| 96 | + for i in range(MAX_RETRIES): |
| 97 | + response = requests.get(BUILD_URL, headers={'Accept': 'application/json'}) |
| 98 | + response_json = response.json() |
| 99 | + print(f'Attempt {i + 1}: Buildbot response:', response_json) |
| 100 | + |
| 101 | + # Check if any build has the target SHA |
| 102 | + builds = response_json.get('builds', []) |
| 103 | + print (builds) |
| 104 | + build_with_sha = None |
| 105 | + for build in builds: |
| 106 | + build_id = build['buildid'] |
| 107 | + properties = get_build_properties(build_id) |
| 108 | + #print(properties) |
| 109 | + #prop = properties.get('revision', []) |
| 110 | + |
| 111 | + if 'properties' in properties: |
| 112 | + print (properties['properties']) |
| 113 | + if 'revision' in properties['properties'][0]: |
| 114 | + print(properties['properties'][0]) |
| 115 | + if 'revision' in properties['properties'][0] and properties['properties'][0]['revision'] [0] == TARGET_SHA: |
| 116 | + build_with_sha = build |
| 117 | + break |
| 118 | + |
| 119 | + if build_with_sha: |
| 120 | + print('Build started successfully for SHA:', TARGET_SHA) |
| 121 | + break |
| 122 | + else: |
| 123 | + print('Build for SHA not started yet, retrying in', RETRY_INTERVAL, 'seconds') |
| 124 | + time.sleep(RETRY_INTERVAL) |
| 125 | + else: |
| 126 | + print('Build did not start for SHA:', TARGET_SHA, 'after maximum retries') |
| 127 | + exit(1) |
| 128 | + " |
| 129 | +
|
| 130 | + - name: Stop and remove container |
| 131 | + if: always() |
| 132 | + run: | |
| 133 | + docker stop "${{env.CONTAINER_NAME}}" |
| 134 | + docker rm "${{env.CONTAINER_NAME}}" |
0 commit comments