Use jenkins for project deployment

foreword

Since I recently took over the front-end project, when the project is packaged and deployed, it is very cumbersome to manually operate the steps of building, packaging and deploying, so I try to use jenkins to help myself solve this trouble. It worked before, but just used it. This time I took the opportunity to build and configure it myself. I thought it was very simple, but I encountered a few pitfalls more or less in the process of using it. Next, this article will take the deployment of front-end projects as an example to explain.

1. Install jenkins

You can check my previous article: jenkins detailed installation tutorial

Second, according to the necessary plug-ins

  1. Click System Management -> Plugin Management -> Optional Plugins
    Two plugins need to be installed
[NodeJS Plugin](用于构建前端项目使用)
[Publish Over SSH](用于把项目部署的远程服务器使用)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-260a0Nvh-1614666306788)(/img/bVcO6Sz)]

3. Plug-in configuration

1. [NodeJS Plugin]: Click System Management -> Global Tool Configuration
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-biLoq6Pi-1614666306790)(/img/bVcO6S2)]

2. [Publish Over SSH]: Click System Management -> System Configuration
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ArMKKZmA-1614666306792)(/img/bVcO6TV)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-h5lxUTVp-1614666306794)(/img/bVcO6T4)]

  • Enter the jenkins server to configure the public key and private key
ssh-keygen -t rsa
  • Entering all the way will generate the ssh file in the current user directory, id_rsawhich is the private key id_rsa.puband the public key
  • Copy id_rsa.pubthe contents of the public key to ~.ssh/.ssh/authorized_keys of the remote server (for example: 47.104.36.36)
vi .ssh/authorized_keys

4. Create a new task

1. Fill in the description
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-phocyrfR-1614666306796)(/img/bVcO6UJ)]

2. Configure git, enter task configuration, and select source code management
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-jFmFwevF-1614666306797)(/img/bVcO6UT)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-04D4kiOX-1614666306798)(/img/bVcO6Vd)]

3. Set up the build environment, select Provide Node & npm bin/ folder to PATH and then select the node version configured when installing the plugin before
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-pw0uRYoC-1614666306799)(/img/bVcO6Ve)]

4. Build, add build steps and choose Excute shell
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-8I8sMATn-1614666306801)(/img/bVcO6VK)]

node -v
npm install
npm run build
tar -czvf dist.tar.gz dist

5. Post-build operation, add post-build operation steps and select Send build artifacts over SSH
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-mnqDihSi-1614666306802)(/img/bVcO6VQ)]

cd /data/www/wwwroot/test
tar -zxvf dist.tar.gz
rm -rf dist.tar.gz

6. After saving, click "Build Now" to check whether the task is built

A few pits encountered

  • After configuring the secret key, connect and click Test Connection to report an error and an error message
jenkins.plugins.publish_over.BapPublisherException: Failed to add SSH key. Message [invalid privatekey: [B@60373f7]

Because the version of openssh we generated the key is too high, the solution is as follows: re-execute the following command to generate a new key, and use the newly generated key

ssh-keygen -m PEM -t rsa -b 4096
  • After copying the ssh public key to authorized_keys, the solution is still unable to log in without password.
    Solution:
分别修改修改.ssh目录的权限以及authorized_keys 的权限
chmod 644 ~/.ssh/authorized_keys

chmod 700 ~/.ssh

Guess you like

Origin blog.csdn.net/m0_37572422/article/details/114284418