Git + Jenkins Automation NGINX Release is easy to implement

This article was last updated on: February 7, 2024 pm

overview

Previously, a simple automated release of NGINX was implemented based on GitLab + Jenkins.
Specifically, it includes the following components:

  1. GitLab
    1. Includes GItLab’s WebHook;
  2. Jenkins and its plugins:
    1. Generic Webhook Trigger
    2. Publish Over SSH

🧠Doubt:

Why not use Ansible?
A: The reason why Ansible is not used here is because this environment does not have Ansible installed by default, and Publish Over SSH is enough, so Ansible is no longer used.

Detailed description

There are 3 automation jobs, as follows:

  1. Generic Webhook Trigger Used to link with GitLab to automatically trigger WebHooks
  2. Publish Over SSH Used to publish NGINX configurations via SSH

Illustrate:

disposition WebHook
in order to test-intranet-nginx Take an example to illustrate.

  1. Go to the project -> Set up -> integration. As shown in the following figure:
    gitlab webhook
  2. Fill in the URL: https://jenkins.example.com/generic-webhook-trigger/invoke?token=Jdy0bTQafyfUUBxJw33k(Assuming jenkins.example.com is the console domain name of Jenkins, the token can be in the corresponding Jenkins plugin.) Generic Webhook Trigger Here, the token is used to distinguish which job it is. )
  3. Trigger selection: Push events -> master.
  4. Uncheck the box as needed Enable SSL verification. Save. As follows:
    gitlab webhook 填入 jenkins url 和 token

Illustrate:

The URL can be found in the Jenkins plugin.
Trigger can be adjusted as needed.

In Jenkins System configurationInside -> Publish over SSH:

As shown in the following figure:
jenkins 插件 publish over ssh

Fill in:

  • Key
  • SSH Server:
    • name (username used to identify Jenkins users)
    • Hostname (IP address of the target machine)
    • Username (OS user of the target machine)
    • Remote Directory (the directory address of the target machine to which the file needs to be sent)

Illustrate:

In general, for NGINX, the directories are 2, which are: /etc/nginx(Put the configuration.) *.conf) and /usr/share/nginx/html(put static web file)

The following figure Job is an example:

Configure first Source code management, as shown below:

job 源码管理配置

Fill in:

  1. Repository URL
  2. Authentication information (if it is a public library, authentication information is not required)
  3. Branch: master(adjust as needed)
  4. Source repository information:
    1. In this example, the types are: gitlab
    2. URL
    3. Version of GitLab

Then configure Build the trigger, as shown below:

jenkins-webhook-trigger1.png

Illustrate:

For detailed use, please enter the URL in the image in the browser for further viewing.

  1. Variable (with default configuration)
  2. Expression (with default configuration)
  3. Token

jenkins-webhook-trigger2.png

  1. Expression (explained below: used for directory filtering, after filling in the regular, only the directory matches the regular and changes will trigger the build)

  1. Text (default configuration)

Finally, yes Post-build actions(The actual “build” process doesn’t do anything). As shown in the following figure:

配置 publish over ssh

Note:

If there are multiple nginx to send at the same time, write multiple SSH servers here at the same time.

  1. Name: Select the corresponding Name from the drop-down box
  2. Transfers
    1. Source files: Source file, located at: iaas_web_xxxx/conf/**/*
    2. Remove prefix: The prefix to be removed is: iaas_web_xxxx/. After removal, an example is: conf/nginx.conf
    3. Remote directory: If you do not fill in, you will fill in the directory before, generally /etc/nginx. The example is then published to: /etc/nginx + conf/nginx.confNamely: /etc/nginx/conf/nginx.conf
    4. Exec command: Commands that need to be executed after the file transfer has passed. For: nginx -t && nginx -s reload(or:sudo systemctl reload nginx). i.e., first -t Verify that the configuration has no syntax errors before you do so reload Publish. If there is a problem with the verification, the Jenkins pipeline will be abnormal, turning yellow or red.

Publishing process

  1. Users can modify NGINX Conf locally via IDE + Git, and eventually push or merge(Also triggered.)pushaction) to masterabove
  2. GitLab received push event, trigger the webhook call: https://example.com/generic-webhook-trigger/invoke?token=Jdy0bTQafyfUUBxJw33k
  3. Jenkins receives a webhook trigger. And combine with the expression of the filter to judge, confirm the match, and start to automatically start a job.
  4. The job process is:
    1. Pull the repository with the nginx configuration to Jenkins.
    2. Pass Publish over SSHto transfer the relevant directories and files to the specified directory of the SSH Server
    3. Execute the nginx command to publish.
  5. Come to an end.

Prompt:

If it is not automatically published due to other exceptions, you can also manually click on the Job page: Build now Perform a manual trigger

Can passFirst imageRSS subscriptions: The Atom feed failed, so that if the release fails, you will receive the email in time.


Git + Jenkins Automation NGINX Release is easy to implement
https://e-whisper.com/posts/6622/
Author
east4ming
Posted on
September 26, 2021
Licensed under