This repository contains a Java Maven project that utilizes a Jenkins pipeline to automate various stages of the development lifecycle, including the generation of unit tests. Additionally, this project provides a docker-compose.yml and a Dockerfile to set up a Jenkins environment with all the necessary prerequisites.
- Clean Workspace and Checkout: This stage cleans the Jenkins workspace and checks out the code from the feature branch.
- Clean Workspace: This stage cleans the Maven build workspace.
- Analyze Changed Files: This stage identifies the files that have changed between the previous commit and the current commit.
- Generate Unit Test: A Python script (
generate_tests.py) is executed to generate unit tests using OpenAI. - Run Generated Unit Tests: The generated unit tests are executed. If any test fails, the pipeline will mark this stage as 'UNSTABLE'.
- Fix Errors and Re-run Tests: If the previous stage fails, this stage will be executed. A Python script (
fix_errors.py) checks for errors, fixes them, and updates the test code. The tests are then re-run. - Commit and Push Generated Test: The path to the generated test file is read from
generated_test_path.txt. The generated test is then committed and pushed to themainbranch of the repository.
OPENAI_API_KEY: This environment variable securely handles the OpenAI API key using Jenkins credentials.
- Jenkins with the necessary plugins installed.
- Python 3.x for executing the Python scripts.
- Git installed on the Jenkins agent.
- Maven installed on the Jenkins agent.
For those who prefer Docker, a docker-compose.yml and Dockerfile are provided to set up a Jenkins environment with all the prerequisites.
- Ensure Docker and Docker Compose are installed on your machine.
- Navigate to the project directory.
- Run
docker-compose up -dto start the Jenkins service. - Access Jenkins by navigating to
http://localhost:8080in your browser.
To automatically trigger the Jenkins pipeline upon code changes, you need to set up a webhook in your version control system (e.g., GitHub, Bitbucket).
- Go to your GitHub repository.
- Click on
Settings>Webhooks. - Click on
Add webhook. - Set the
Payload URLtohttp://<JENKINS_URL>/github-webhook/. - Choose content type as
application/json. - Select the events you want to trigger the webhook, typically
Just the push event. - Save the webhook.
- Go to your Bitbucket repository.
- Click on
Settings>Webhooks. - Click on
Add webhook. - Set the
URLtohttp://<JENKINS_URL>/bitbucket-hook/. - Choose
Repository pushas the trigger. - Save the webhook.
Remember to replace <JENKINS_URL> with your actual Jenkins server URL.
Credentials such as the OpenAI API key and GitHub credentials are securely handled using Jenkins credentials binding.