This guide explains automating pull request creation in GitHub using Jenkins and GitHubs REST API.
In this guide, you’ll learn how to automate raising a pull request (PR) in GitHub by integrating the process into a Jenkins pipeline. Instead of manually clicking on “New Pull Request” in the GitHub UI, this approach uses GitHub’s REST API to automate the creation of a PR.Below, find a step-by-step explanation, enriched with diagrams and code snippets.
GitHub’s REST API allows you to perform almost any action available in the GitHub UI. To discover the available endpoints, navigate to the API section (usually at the bottom right of your repository page) where you can view a Swagger-based specification.
Open your repository’s API interface and search for “pull”.
You will find that the endpoint for creating a pull request uses the POST method. Expanding this endpoint reveals the required parameters including repository owner, repository name, base branch, head branch, title, body, labels, and milestones.
Next, open the API interface for creating a pull request and fill in the required fields (for example, owner: “Dasher-org” and repository: “Solar System GitOps Agostini”).
Click “Try it out” and enter the repository name along with a JSON body containing the PR details.
After clicking “Execute,” a cURL command is generated that mirrors the endpoint, headers, and payload provided.
If you encounter a 404 error, verify that the endpoint URL and parameters are correctly formatted (e.g., no extra spaces).
A missing token will result in a 401 Unauthorized response.
For instance, a missing token response might look like:
The API documentation specifies that the token can be passed as either a header or a query parameter. In our example, we use the Authorization header.Another example with token authorization:
Integrating the cURL Command into a Jenkins Pipeline
To integrate the automated PR creation, add a new stage in your Jenkins file titled “K8S - Raise PR”. This stage should follow the image tag update stage. Below is an excerpt from a sample Jenkins file illustrating the available stages:
Within the K8S - Raise PR stage, use a shell script to execute the tested cURL command. This command makes a POST request to the API endpoint with the proper headers and payload, utilizing an environment variable (e.g., GITEA_TOKEN) for authorization.Below is the cURL command used in the Jenkins pipeline:
The “assignee” and “assignees” are set to the receiving admin (e.g., gitea-admin).
The “base” refers to the main branch.
The “head” dynamically uses the Jenkins BUILD_ID to create a feature branch.
Both the title and the body of the pull request are provided.
Upon executing this stage, the feature branch (e.g., feature-2) will be created and a new PR will automatically appear in GitHub with the specified details. After the commit, a new Jenkins pipeline triggers on the PR branch, runs subsequent builds, and updates the PR status with direct build details. Additionally, you can enforce branch protection rules that require passing status checks before merging.
Post merge, the repository shows the updated commit ID and the Argo CD dashboard reflects the health and sync status of your application.
On your Kubernetes cluster (within the solar-system namespace), verify the creation of the pods, service, deployment, and replica set:
Copy
Ask AI
k -n solar-system get allpod/solar-system-889855cc4-qcpwt 1/1 Running 0 31spod/solar-system-889855cc4-vh75q 1/1 Running 0 37sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/solar-system NodePort 10.245.223.95 <none> 3000:30000/TCP 37sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/solar-system 2/2 2 2 37sNAME DESIRED CURRENT READY AGEreplicaset.apps/solar-system-889855cc4 2 2 2 37s
Access the application on port 30000 using the appropriate NodePort endpoint. This verifies both HTTP accessibility and the connection to your MongoDB backend via Kubernetes secrets.
The application secret (e.g., mongo-db-creds) is managed using Bitnami Sealed Secrets. When the Sealed Secrets controller decrypts the sealed secret, it converts it into a standard Kubernetes secret.Below is an example of the Kubernetes secret after decryption:
By integrating the steps above into your Jenkins pipeline, you can fully automate updating Kubernetes deployments using GitOps. The pipeline not only updates the Docker image tag in your repository but also automates the PR creation via a cURL command. Once merged, Argo CD synchronizes the changes in your Kubernetes cluster, ensuring that your deployments are efficient, consistent, and secure.Happy automating!