Git + Jenkins Automation NGINX Release is easy to implement
This article was last updated on: July 24, 2024 am
overview
Previously, a simple automated release of NGINX was implemented based on GitLab + Jenkins.
Specifically, it includes the following components:
- GitLab
- Includes GItLab’s WebHook;
- Jenkins and its plugins:
- Generic Webhook Trigger
- 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:
- Generic Webhook Trigger Used to link with GitLab to automatically trigger WebHooks
- Publish Over SSH Used to publish NGINX configurations via SSH
Illustrate:
disposition WebHook
in order totest-intranet-nginx
Take an example to illustrate.
- Go to the project -> Set up -> integration. As shown in the following figure:
- 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. ) - Trigger selection: Push events -> master.
- Uncheck the box as needed Enable SSL verification. Save. As follows:
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:
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:
Fill in:
- Repository URL
- Authentication information (if it is a public library, authentication information is not required)
- Branch:
master
(adjust as needed) - Source repository information:
- In this example, the types are:
gitlab
- URL
- Version of GitLab
- In this example, the types are:
Then configure Build the trigger, as shown below:
Illustrate:
For detailed use, please enter the URL in the image in the browser for further viewing.
- Variable (with default configuration)
- Expression (with default configuration)
- Token
- Expression (explained below: used for directory filtering, after filling in the regular, only the directory matches the regular and changes will trigger the build)
- Text (default configuration)
Finally, yes Post-build actions(The actual “build” process doesn’t do anything). As shown in the following figure:
Note:
If there are multiple nginx to send at the same time, write multiple SSH servers here at the same time.
- Name: Select the corresponding Name from the drop-down box
- Transfers
- Source files: Source file, located at:
iaas_web_xxxx/conf/**/*
- Remove prefix: The prefix to be removed is:
iaas_web_xxxx/
. After removal, an example is:conf/nginx.conf
- 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.conf
Namely:/etc/nginx/conf/nginx.conf
- 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 soreload
Publish. If there is a problem with the verification, the Jenkins pipeline will be abnormal, turning yellow or red.
- Source files: Source file, located at:
Publishing process
- Users can modify NGINX Conf locally via IDE + Git, and eventually
push
ormerge
(Also triggered.)push
action) tomaster
above - GitLab received
push
event, trigger the webhook call:https://example.com/generic-webhook-trigger/invoke?token=Jdy0bTQafyfUUBxJw33k
- 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.
- The job process is:
- Pull the repository with the nginx configuration to Jenkins.
- Pass Publish over SSHto transfer the relevant directories and files to the specified directory of the SSH Server
- Execute the nginx command to publish.
- 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.