Install Jenkins

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

sudo yum upgrade

sudo yum install epel-release

sudo yum install java-11-openjdk-devel

sudo yum install jenkins

sudo systemctl daemon-reload

sudo systemctl status jenkins
Copy the code

Modify Jenkins default port and user

# change user name to root; Change the port to 8989 vim /etc/sysconfig/jenkinsCopy the code

Start the Jenkins

sudo systemctl start jenkins
Copy the code

Check Jenkins status

sudo systemctl status jenkins
Copy the code

Updating the firewall

sudo firewall-cmd --zone=public --permanent --add-port=8989/tcp
sudo firewall-cmd --reload
Copy the code

Visit Jenkins

The browser input: http://192.168.2.240:8989

Installing recommended plug-ins

Create the first administrator user

Start using Jenkins

Installing a plug-in

  1. GitLab

  1. Publish Over SSH

The premise to prepare

Configure certificates (for pulling code from GitLab)

Dashboard -> Manage Credentials -> Jenkins(global) -> Global Credentials -> Add Credentials

Install Git on Jenkins’ distribution machine

yum install git
Copy the code

Install PHP7 on Jenkins’ distribution machine

sudo yum install epel-release sudo yum install yum-utils sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm sudo yum-config-manager --enable remi-php74 sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd # ext yum install php-xml yum install php-intl yum # yum install php-pecl-zip PHP systemctl restart HTTPDCopy the code

Install Node on Jenkins’ distribution machine

curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -

sudo yum install nodejs

node --version

npm --version
Copy the code

Install Composer on Jenkins’ publishing machine

# download composer install PHP script - r "copy (' https://getcomposer.org/installer ', 'composer - setup. PHP);" Verify download integrity, Signature matching will output the Installer verified HASH = "$(wget - q - O - https://composer.github.io/installer.sig)" PHP - r "the if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" PHP --install-dir=/usr/local/bin --filename=composerCopy the code

Configuring the SSH Server

  1. You first need to transfer the public key from the publisher machine to the production machine.

Send /root/.ssh/id_rsa.pub on the publisher to.ssh/ on the production machine.

Rename id_rsa.pub to authorized_keys

Test the IP address of the SSH production machine without password

# ssh-keygen -t rsa-b 2048 Jenkins does not support ssh-keygen -m PEM -t rsa-b 4096 # transfer to SCP /root/.ssh/id_rsa.pub [email protected]: / root /. SSH/authorized_keysCopy the code
  1. Configure SSH Servers

Dashboard -> Manage Jenkins -> Configure System

Locate the Publish over SSH bar on the page and configure SSH Servers

Remember to check Use Password authentication,or Use a different key

  1. Test whether the configuration is successful

Create Jenkins project configuration

Create a Pipeline-style project

Parametric construction

Dashboard -> oa-pipeline -> Configure -> General Remember to select String Parameter

pull code

pipeline { agent any stages { stage('pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git repository}]]])}}}}Copy the code

If you pull code from a tag, this configuration is required

pipeline { agent any stages { stage('pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: 'refs/tags/${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git repository}]]])}}}}Copy the code

build project

pipeline { agent any stages { stage('pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: 'refs/tags/${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: The ${} git repository address]]])}} stage (' build project ') {steps {sh '/ usr/local/oa/oa_build. Sh'}}}}Copy the code

The script content is as follows:

#! /bin/bash cd /var/lib/jenkins/workspace/php-oa && /usr/local/bin/composer install cd /var/lib/jenkins/workspace/php-oa/slimvue-oa && npm install cd /var/lib/jenkins/workspace/php-oa/slimvue-oa && npm run publishCopy the code

Save and exit. And change the file permissions to executable:

chmod 764 /usr/local/oa/oa_build.sh
Copy the code

post build

Write the script on the target host

#! /bin/bash cp /data/projects/oa_new/resource/config.yml /data/projects/oa_new/oa/config/config.yml rm -rf /data/projects/oa_new/oa/cache/*Copy the code

Save and exit. And change the file permissions to executable:

chmod 764 deploy.sh
Copy the code

The final Pipeline configuration is as follows:

pipeline { agent any stages { stage('pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: 'refs/tags/${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git repository}]]])}} stage('build project') {steps{sh' /usr/local/oa_build. sh'}} stage('post build'){steps{ sshPublisher(publishers: [sshPublisherDesc(configName: '98', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /data/projects/oa_new sh deploy.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/data/projects/oa_new/oa', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }Copy the code

Start the deployment

Build with Parameters