1. First install these three things on the server: jdk, git, maven , and configure global variables.
Enter to view the current environment variables, and remember the path, which needs to be used below :
export
2. Start the whole job and install jenkins
1. Pull the image to view the image
#View image package
docker search jenkins
#Install mirror package
docker pull jenkins
#Check if the installation is successful (check the image package)
docker images
2. Create a new folder to mount the docker directory
mkdir -pv /opt/docker/jenkins
3. Add modification permissions or fall into the pit
chown -R 1000 /opt/docker/jenkins
4. Start Jenkins, there are pits here
The paths of the maven and jdk environment variables you just checked are also placed here for file mounting
Otherwise, when starting the build, maven and jdk cannot be found
docker run -d -p 9595:8080 -p 50000:50000
#Let the container have root privileges to facilitate access to the container operation
--privileged=true
--restart=always
--name jenkins
#The container /var/jenkins_home path is mapped to the host /opt/docker/jenkins/
-v /opt/docker/jenkins:/var/jenkins_home
-v /etc/localtime:/etc/localtime
#jdk environment mount
-v /usr/local/src/jdk/jdk1.8:/usr/local/src/jdk/jdk1.8
#maven environment mount
-v /usr/local/src/maven/apache-maven-3.5.3:/usr/local/src/maven/apache-maven-3.5.3 jenkins/jenkins
5. View password:
Where is the host path just set, and check it there
cat opt/docker/jenkins/secrets/initialAdminPassword
http://ip:
6. Enter 9595 in the browser /
to access the jenkins home page
Enter the password you just checked
Next, go to the download plug-in page and install the recommended plug-in
7. You can create or use the admin user to continue, here select the admin account to continue
After entering, click here to slide down, find the option to change the password, and change the password
8. Download the plugin Publish Over SSH
After downloading and installing, it is recommended to restart Jenkins in the docker container
9. Global tool configuration
Configure maven here, that is, just start to view the system environment variable maven path
Install jdk, that is, start to view the system environment variable jdk path
Slide down, configure maven as above
10. System configuration - environment variables, if you don't configure it, you will fall into the pit
The configuration of the key here is the name of the server environment variable, and fill in whatever the name of the server environment variable is! ! !
11. After configuration, return to the server and enter the Jenkins container
docker exec -it jenkins bas
12. Configure SSH-KEY and execute in the container:
ssh-keygen
#Enter three times to generate ssh key
13. Check the public key and copy it
cat ~/.ssh/id_rsa.pub
14. Enter your github or gitee or gitlab to configure ssh key
I use the gitlab configuration here, and put the Ctrl+V just now here
15. Test it
Execute a command in the container, if this interface appears, the configuration is successful, enter yes and press Enter to indicate that the authentication is complete
git clone [email protected]:xxxx.git
16. Start the project
Create a pipeline project
17. Configuration
Select Parameterized Build Process - Select String Parameters
REPOSITORY_VERSION
#Represents the current code branch where the default test
18. Scroll down to find the pipeline and write the pipeline script
Refer to the script, REPOSITORY Fill in the ssh address of the project, REPOSITORY_VERSION is the build parameter just configured
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-test.git"
}
stages {
stage('拉取代码') {
steps {
echo "start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
}
}
19. Test pull code
Return to the home page, select the project you just created, and click the button to run
Click to start building
If the build is successful, it will appear like this
20. Build the code through maven, upload it to the specified folder through ssh, and execute the operation
First set up ssh
Click Advanced to enter username and password
Finally, you can click Test Conflguation to test. If Success pops up, it means the link is successful.
21. Modify the pipeline just now
(Here, by directly uploading to the specified docker jar package folder, updating and replacing the old version, executing the command to restart the container, and loading the latest jar package)
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-demo.git"
MODULE="testsystemserver"
}
stages {
stage('拉代码') {
steps {
echo "start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
stage('编译代码') {
steps {
echo "start compile"
sh "cd common && mvn -U clean install package"
echo "compile project ................................"
sh "cd systemserver && mvn -U clean install package"
echo "compile testsystemserver................................"
}
}
stage('构建部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '31', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'docker restart testsystemserver', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'opt/docker/jar', remoteDirectorySDF: false, removePrefix: 'systemserver/target/', sourceFiles: 'systemserver/target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
The ones under stage('build deployment') can be generated with reference to the pipeline syntax, otherwise there will be pits and pits! ! !
error:Jenkins SSH: Transferred 0 file(s)
Click Pipeline Syntax
example:
example
If the address of the console output build when the project is built in jenkins is
/var/lib/jenkins/workspace/test
and view the service deployment of jenkins. After building the corresponding project, the directory of the generated target package is:/var/lib/jenkins/workspace/test/test-demo/target
So Source files (indicating what files to match when transferring) should be filled in
test-demo/target/*.jar
Remove prefix (the operation is for the above source files directory) should be filled
test-demo/target/
Remote directory (this operation is based on the set server directory). Here my server configuration is /. So it should be written here./
Exec command (the operation is what command to execute after the file transfer is completed) I just restart the service here, so fill it in
docker restart testsystemserver
Then click Generate Pipeline Script to replace it;
Break up the meeting! ! ! !