mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-10 23:40:30 -04:00
3.8 KiB
3.8 KiB
title, tags, created
| title | tags | created | |||||
|---|---|---|---|---|---|---|---|
| Jenkins: Basic Introduction and Project Usage Guide |
|
2026-06-03 |
Jenkins: Basic Introduction and Project Usage Guide
Jenkins is the world's most popular open-source automation server. It helps developers automate the parts of software development related to building, testing, and deploying, facilitating Continuous Integration (CI) and Continuous Delivery (CD).
1. Core Concepts
- Controller (Master): The central brain that handles the UI, job scheduling, and configuration.
- Agent (Node): Machines that do the actual work (running builds/tests). Best Practice: Always run builds on agents, not the controller.
- Pipeline: A user-defined model of a CD pipeline. It defines your entire build process, which typically includes stages for building, testing, and delivering.
- Jenkinsfile: A text file that contains the definition of a Jenkins Pipeline and is checked into source control (Git).
2. How to Use Jenkins for Your Project
The modern way to use Jenkins is Pipeline as Code using a Jenkinsfile.
Step 1: Create a Jenkinsfile
In the root of your project repository, create a file named Jenkinsfile (no extension).
Step 2: Define Your Pipeline
Here is a standard Declarative Pipeline template you can adapt:
pipeline {
agent any // Tells Jenkins to run this pipeline on any available agent
environment {
// Define environment variables here
APP_NAME = "my-awesome-project"
}
stages {
stage('Checkout') {
steps {
// Jenkins automatically checks out code, but you can add custom logic
echo "Checking out code for ${APP_NAME}..."
}
}
stage('Build') {
steps {
echo 'Building...'
// Example for a Node.js project:
// sh 'npm install'
// Example for a Java/Maven project:
// sh './mvnw clean install'
sh 'echo "Insert build commands here"'
}
}
stage('Test') {
steps {
echo 'Testing...'
// sh 'npm test'
sh 'echo "Insert test commands here"'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
// sh './deploy.sh'
sh 'echo "Insert deployment commands here"'
}
}
}
post {
always {
echo 'Pipeline finished. Cleaning up...'
}
success {
echo 'Build Succeeded!'
}
failure {
echo 'Build Failed! Check logs.'
}
}
}
Step 3: Configure Jenkins
- Open your Jenkins Dashboard.
- Click New Item, enter a name, and select Pipeline.
- Under the Pipeline section, set Definition to "Pipeline script from SCM".
- Select Git and provide your repository URL and credentials.
- Ensure the Script Path is set to
Jenkinsfile. - Click Save and then Build Now.
3. Best Practices for Your Project
- Keep it in Git: Always store your
Jenkinsfilein your repository. This allows versioning and easy recovery. - Fail Fast: Put unit tests and linting in the earliest stages. If they fail, the pipeline stops immediately, saving time.
- Use Credentials: Never hardcode passwords or API keys. Use Jenkins' Credentials Provider and access them via the
credentials()helper in your script. - Isolated Environments: Use Docker agents to ensure your build environment is consistent and doesn't conflict with other projects.
- Stages are Logical: Use stages to group related steps. This makes the Jenkins UI much easier to read and debug.