Table of Contents
Project NIF Validator
This project extends the Jenkins setup from the previous note by building and shipping a Docker container named nif-validator. The example walks through four stages: automating the Docker build and push from GitHub, triggering builds automatically with a webhook, migrating the job to a declarative Jenkinsfile pipeline, and finally hardening the pipeline with automated tests and code-quality checks.
Github
- Create repo “nif-validator”
Add ~/.ssh/config
Host github.com
HostName github.com
IdentityFile ~/.ssh/cfreire-key-20260904
User cfreire
IdentitiesOnly yes
Stage 1
Automate the Docker build and release. The goal is to let Jenkins build the Docker image from the repository, log in to Docker Hub, and push the image — so every code change produces a fresh, publishable image without manual steps.
- Create a GitHub Project — As in the previous demo, create a new GitHub repository to host the code.
- Upload code into repository — Commit and push both the application source and its
Dockerfile, so Jenkins has everything it needs to build the image from the repository alone. - Create a freestyle project — Create a Jenkins job of type Freestyle project and configure it to use this repository.
- Link to GitHub source — Set Source Code Management → Git and paste the repository URL, adding the appropriate credentials so Jenkins can clone it.
- Create a shell — Add an Execute shell build step that builds the image, authenticates with Docker Hub, and pushes it:
docker build -t <dockerid>/nif-validator . docker login -u <dockerid> -p <password> docker push <dockerid>/nif-validator
Verify execution — After the build succeeds, confirm the image runs correctly by launching a container locally. The -p 8000:9046 mapping publishes the container’s internal port 9046 to the host’s port 8000:
docker run -d --name nif-validator -p 8000:9046 <dockerid>/nif-validatorStage 2
Enable automatic builds with a GitHub Webhook. Rather than triggering builds manually, configure GitHub to notify Jenkins whenever code is pushed, so every commit triggers a build automatically.
Configure GitHub Webhook
Go to Your Repository Settings:
- Navigate to your GitHub repository.
- Click on Settings → Webhooks → Add webhook.
Set the Payload URL:
- Use the URL of your Jenkins server with
/github-webhook/appended, e.g.:
Set the Content Type — Select application/json (Jenkins expects JSON payloads from GitHub).
Select Events to Trigger the Webhook — Typically, select “Just the push event” so Jenkins runs only on code changes.
Save the Webhook — Ensure the webhook is enabled.
On Jenkins
- Activate
GitHub hook trigger for GITScm pollingunder Build triggers. This tells Jenkins to listen for webhook notifications from GitHub and start a build whenever one arrives.
Pipeline examples
For ready-made Jenkinsfile templates and patterns, see the official reference:
Stage 3
Convert the freestyle job into a reusable pipeline. The freestyle project is convenient for quick demos, but a pipeline encoded in a Jenkinsfile is version-controlled, reviewable, and portable.
- Change into a Pipeline — Change the job type to Pipeline (or create a new pipeline job).
- Create a Jenkinsfile — Write the pipeline definition in a
Jenkinsfileat the root of the repository, declaring stages such as Build and (later) Test. - Link to GitHub — Point the pipeline job at the repository’s
Jenkinsfile. Set Pipeline → Definition → Pipeline script from SCM, select Git, and reference the branch and script path so Jenkins loads the pipeline directly from the repository.
Stage 4
Add automated tests and code-quality gates. This stage hardens the pipeline by catching bugs and enforcing standards on every build. Each item adds a new stage (or step) to the Jenkinsfile.
- Add JUnit tests — Run unit tests and publish the results (e.g. with
junitin Jenkins) so failures and trends are visible in the build report. - Add Integration tests — Exercise the application against real dependencies (e.g. a database or another service) to catch issues that unit tests miss.
- Add UI Tests — Drive the application’s interface to verify user-facing behaviour end to end.
- Add coverage reports — Measure how much of the code the tests exercise and publish the coverage report to gate regressions.
- Add PEP8 Verification — If the project is Python, run a style check (such as
pycodestyle/flake8) to enforce PEP 8 formatting. - Add CC Analysis — Run Cyclomatic Complexity (or similar static analysis) to detect overly complex code that may be hard to test and maintain, failing the build when it exceeds a threshold.