如何通过Drone自定义更新GitHub仓库中各Job的状态?
Absolutely, you can replace the default GitHub PR status with custom ones and even add separate statuses for each Drone Job—this is totally achievable with a couple of straightforward approaches. Let me walk you through how to do it:
First: Disable Drone's Default GitHub Status
Before adding custom statuses, you’ll want to turn off Drone’s automatic default status update. This ensures your custom statuses are the only ones appearing in your PRs. You can do this via:
- Drone’s web UI: Navigate to your repository settings, find the "GitHub Status" toggle, and switch it off.
- Or, if you prefer config-as-code, set the
statusparameter tofalsein your repository’s Drone configuration (check your Drone instance’s docs for exact syntax if this is supported).
Option 1: Use Drone’s GitHub Status Plugin (Simplest Method)
Drone has an official plugin designed specifically for sending custom GitHub statuses. You can add this as a step in each job to report individual job statuses, or as a final step to report an overall pipeline status.
Here’s an example .drone.yml snippet that adds separate statuses for a build and test job:
kind: pipeline type: github name: default steps: - name: build-code image: golang:1.21 commands: - go mod download - go build -o myapp # Report build job status to GitHub - name: report-build-status image: drone/github-status settings: token: from_secret: github_token # Store your GitHub PAT as a Drone secret context: "ci/build" # This is the label that shows up in the PR status description: "Application build completed" state: success # Automatically uses the job's status if not hardcoded target_url: "${DRONE_BUILD_LINK}" # Links back to your Drone build logs when: status: [success, failure] - name: run-tests image: golang:1.21 commands: - go test ./... -v # Report test job status to GitHub - name: report-test-status image: drone/github-status settings: token: from_secret: github_token context: "ci/unit-tests" description: "Unit tests executed" state: success target_url: "${DRONE_BUILD_LINK}" when: status: [success, failure]
Each context value acts as a unique label for the status, so you’ll see separate entries like "ci/build" and "ci/unit-tests" in your PR’s status section.
Option 2: Manual curl Calls (Full Customization)
If you need more control over the status details (like dynamic descriptions or custom logic), you can directly call the GitHub Status API using curl in your pipeline steps.
First, create a GitHub Personal Access Token (PAT) with the repo:status scope (for private repos) or public_repo scope (for public repos), and store it as a secret in Drone.
Here’s how to add a custom success/failure status for a lint job:
steps: - name: lint-code image: node:18 commands: - npm install - npm run lint # Send success status if lint passes - | curl -X POST \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ https://api.github.com/repos/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/statuses/${DRONE_COMMIT} \ -d '{ "state": "success", "context": "ci/code-lint", "description": "Code linting passed with no issues", "target_url": "'"${DRONE_BUILD_LINK}"'" }' environment: GITHUB_TOKEN: from_secret: github_token when: status: success # Handle lint failure case - name: report-lint-failure image: alpine/curl commands: - | curl -X POST \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ https://api.github.com/repos/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/statuses/${DRONE_COMMIT} \ -d '{ "state": "failure", "context": "ci/code-lint", "description": "Code linting failed - fix errors", "target_url": "'"${DRONE_BUILD_LINK}"'" }' environment: GITHUB_TOKEN: from_secret: github_token when: status: failure
Quick Tips
- Unique Contexts: Make sure each custom status uses a unique
contextvalue—this prevents overwriting existing statuses and keeps your PR statuses organized. - Dynamic State: In the plugin approach, you can omit the
statesetting and Drone will automatically use the job’s actual success/failure status. - Token Security: Always store your GitHub PAT as a Drone secret (never hardcode it in your config file) to keep it secure.
That’s all you need! Either method will let you replace the default GitHub status with custom ones and add individual statuses for every Drone Job in your pipeline.
内容的提问来源于stack exchange,提问作者Paras Jain

