This is a Jenkins shared library for building, testing, creating Docker images, and pushing the images to a registry for Node.js projects. It supports automatic detection of new releases using GitHub tags and allows configuration flexibility for different projects.
nodejs-pipeline-library/
├── vars/
│ └── buildNodeApp.groovy
├── src/
│ └── org/
│ └── example/
│ └── BuildUtils.groovy
└── resources/
└── org/
└── example/
└── config/
└── defaultConfig.yml
- Go to Jenkins > Manage Jenkins > Configure System.
- Under Global Pipeline Libraries, add a new library:
- Name:
nodejs-pipeline-library
- Default Version:
master
(or the branch you prefer) - Retrieval Method: Modern SCM
- Source Code Management: Select the appropriate SCM and provide the repository URL
- Name:
In each project that will use the shared library, create a Jenkinsfile
with the following content:
@Library('nodejs-pipeline-library') _
buildNodeApp(
repoUrl: 'https://github.com/my-org/my-node-app.git',
branch: 'develop',
dockerImageName: 'my-node-app'
)
The default configuration is defined in resources/org/example/config/defaultConfig.yml
. You can override these defaults by providing a custom configuration in the Jenkinsfile
.
Example defaultConfig.yml
:
branch: "main"
dockerCredentialsId: "docker-credentials"
dockerRegistryUrl: "https://registry.example.com"
dockerImageName: "example-app"
To automatically build and push images on new releases, set up a webhook in your GitHub repository:
- Go to your GitHub repository settings > Webhooks.
- Add a new webhook with the following settings:
- Payload URL:
http://your-jenkins-server/github-webhook/
- Content type:
application/json
- Events:
Release
- Payload URL:
The shared library defines the following stages in the pipeline:
- Checkout: Clones the repository.
- Build: Installs dependencies and builds the application.
- Test: Runs tests.
- Docker Build: Builds the Docker image.
- Docker Push: Pushes the Docker image to the registry.
- Release: Tags and pushes the release image if a Git tag is detected.
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.