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
- Click System Management -> Plugin Management -> Optional Plugins
Two plugins need to be installed
[NodeJS Plugin](用于构建前端项目使用)
[Publish Over SSH](用于把项目部署的远程服务器使用)
3. Plug-in configuration
1. [NodeJS Plugin]: Click System Management -> Global Tool Configuration
2. [Publish Over SSH]: Click System Management -> System Configuration
- 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_rsa
which is the private keyid_rsa.pub
and the public key - Copy
id_rsa.pub
the 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
2. Configure git, enter task configuration, and select source code management
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
4. Build, add build steps and choose Excute shell
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
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