mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-11 01:10:28 -04:00
107 lines
3.8 KiB
Markdown
107 lines
3.8 KiB
Markdown
---
|
|
title: "Jenkins: Basic Introduction and Project Usage Guide"
|
|
tags:
|
|
- jenkins
|
|
- devops
|
|
- cicd
|
|
- automation
|
|
- guide
|
|
created: 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:
|
|
|
|
```groovy
|
|
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
|
|
1. Open your Jenkins Dashboard.
|
|
2. Click **New Item**, enter a name, and select **Pipeline**.
|
|
3. Under the **Pipeline** section, set **Definition** to "Pipeline script from SCM".
|
|
4. Select **Git** and provide your repository URL and credentials.
|
|
5. Ensure the **Script Path** is set to `Jenkinsfile`.
|
|
6. Click **Save** and then **Build Now**.
|
|
|
|
## 3. Best Practices for Your Project
|
|
|
|
1. **Keep it in Git:** Always store your `Jenkinsfile` in your repository. This allows versioning and easy recovery.
|
|
2. **Fail Fast:** Put unit tests and linting in the earliest stages. If they fail, the pipeline stops immediately, saving time.
|
|
3. **Use Credentials:** Never hardcode passwords or API keys. Use Jenkins' **Credentials Provider** and access them via the `credentials()` helper in your script.
|
|
4. **Isolated Environments:** Use **Docker** agents to ensure your build environment is consistent and doesn't conflict with other projects.
|
|
5. **Stages are Logical:** Use stages to group related steps. This makes the Jenkins UI much easier to read and debug.
|