-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
107 lines (96 loc) · 4.25 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// this is a sample file written in groovy
// components: agent > environment > stages > post
// stages: checkout code > build > test > archive artifacts > deploy
// Jenkinsfile: A declarative pipeline example for a simple CI/CD workflow
// The 'pipeline' block is the entry point for Jenkins' declarative pipeline.
pipeline {
// Define the agent where the pipeline should run. 'any' means it can run on any available agent.
agent any
// The 'environment' block defines environment variables that will be available throughout the pipeline.
environment {
// Define any necessary environment variables here.
// Example: Set JAVA_HOME for Java builds
JAVA_HOME = '/usr/lib/jvm/java-11-openjdk'
// Another variable that can be used across steps
BUILD_VERSION = '1.0.0'
}
// Stages are logical steps in your build process.
stages {
// First stage: Checkout code from version control (like Git).
stage('Checkout Code') {
steps {
// Checkout the source code from the Git repository.
// You can replace the URL and branch with your project's specifics.
git branch: 'main', url: 'https://github.com/your-repo/your-project.git'
}
}
// Second stage: Compile/Build the project (typically for Java, Node.js, etc.).
stage('Build') {
steps {
// Echo a message to the console output to signal the start of the build process.
echo 'Building the application...'
// Run the build command for the project.
// Replace this with the actual build command (e.g., `mvn clean install` for a Maven project).
sh './gradlew build'
}
}
// Third stage: Run unit tests to validate code.
stage('Test') {
steps {
// Run test commands, such as unit tests.
// You can replace this with your testing framework's command (e.g., `npm test`, `pytest`, etc.).
echo 'Running tests...'
sh './gradlew test'
}
// 'post' conditions to determine what to do based on test outcomes.
post {
// If tests fail, mark the build as unstable or failed.
always {
// Archive the test results (e.g., JUnit XML results) and display them in Jenkins.
junit 'build/test-results/**/*.xml'
}
failure {
// Perform actions on failure, like notifying the team.
echo 'Tests failed. Investigate the issue!'
}
}
}
// Fourth stage: Archive build artifacts for later retrieval.
stage('Archive Artifacts') {
steps {
// Archive the build artifacts (e.g., .jar, .war files, etc.).
// This will store the artifact files for future use.
archiveArtifacts artifacts: 'build/libs/**/*.jar', allowEmptyArchive: true
}
}
// Fifth stage: Deploy the application (optional, for CD pipelines).
// This might include copying files to a server, deploying to cloud infrastructure, etc.
stage('Deploy') {
when {
// Deploy only for main branch or a specific environment.
branch 'main'
}
steps {
echo 'Deploying to production server...'
// Run deployment scripts or commands.
// For example, you might run `scp` to copy files, or use a cloud CLI to trigger deployments.
sh 'scp build/libs/myapp.jar user@production-server:/path/to/deploy/'
}
}
}
// Post section defines what to do after the pipeline completes.
post {
always {
// Clean up the workspace, if necessary.
cleanWs()
}
success {
// Send notifications on successful builds, e.g., Slack, email, etc.
echo 'Build and deployment successful!'
}
failure {
// Notify team of failure.
echo 'Build failed! Sending notifications...'
}
}
}