Docker installs jenkins, deploys springcloud service and pit pit pit.

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

35169c9825a44e2c9fe96e818732fe27.png

 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

3e65415893e445bab7e66e8105543729.png

 Next, go to the download plug-in page and install the recommended plug-in

dffaa75cd79d885907e1e9998bf18ec6.png

 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

6b8299622b0e4f4d967cd637037e200f.png

 8. Download the plugin Publish Over SSH 

5ccc488ee31845c9933456b302378d33.png

 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

2f35bf4f96e240379f3f78c70cc6ee79.png

 Install jdk, that is, start to view the system environment variable jdk path66bd9b19b371483a812f07a8c02fdf2d.png

 Slide down, configure maven as abovebee8a19a45ed4f73a7281e0a6b8b7bf3.png

 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! ! !

4195bd2b9fcd4e78a64ccf73cfaf95e4.png

 af6ea9fe04944d28be6a5b94ad4503a9.png

 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 

c5017b6dadb048f5a24c24b3c92e33dd.png

 

13. Check the public key and copy it

cat ~/.ssh/id_rsa.pub

0172260416b74b0585139de1d3577ea9.png

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

4b5d855fe4844c1ea540eb21e0c62c19.png

 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

f0ece14e74fa43da8360097712e27a77.png

16. Start the project

Create a pipeline project

b38c0a45303f412a8c928bfc67aedbca.png

 17. Configuration

Select Parameterized Build Process - Select String Parameters

REPOSITORY_VERSION 

#Represents the current code branch where the default test

da1577f503d94b3a8438feda6eb7b3f0.png

 18. Scroll down to find the pipeline and write the pipeline script

e086396d43154fcd9e2e43bdc50ebc37.png

 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

708692c378a44230aeb78d1372ff6d8c.png

 

 Click to start building94836d5128fd4dbfb80731ecfb1f42f5.png

 If the build is successful, it will appear like this1bfa92284e414bdcb999a70e8dfbc541.png

 20. Build the code through maven, upload it to the specified folder through ssh, and execute the operation

First set up ssh

dbc87f1de8cc43c092da5a5c4525d16a.png

 4b4bedb0385b4192ac8580ea3cba10c2.png

 Click Advanced to enter username and password7891aed3c02745f9ac60a7fe198e8b9a.png

 Finally, you can click Test Conflguation to test. If Success pops up, it means the link is successful.dea28a89331a4c659e4ba14380d13320.png

 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

7eb6667d19d543e7bde788e11360073e.png

 ffff6991678b4c83bab1f887f37e4ff3.png4a8295fc6b6d49239363784ef17009c9.png

 

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! ! ! !

 

 

 

Guess you like

Origin blog.csdn.net/qq_42990433/article/details/130755080