preface

This article, I wanted to write for a long time, but actually in the process of writing, I found that it involves two things:

  • Git commands, such as git log
  • Gitlab CI configuration and basic use of Gitlab-ci.yML

Git git git git git git git git git git git git git git git

By default, git logs and GitLab cI have been configured.

The opening

As a battle-tested student, you are bound to encounter the following scenario:

Project Manager/Lead: JB, what's with the BUG? JB: Let me see (check the BUG) (a few minutes later) JB: It's really buggy, let me see what's going on (in my mind, I had no problem yesterday, but suddenly there's a BUG again) This BUG was caused by the code submitted by JB2 last night, and it has been fixed now. JB:?? The code submitted last night? Why was the test acceptance not notified? The test didn't even know thatCopy the code

R&d: JB ah, I have an optimization, you verify it; JB: What did the optimization do, what did it change, and what impact did it have? R & D: this will optimize the web response speed, you can compare the response speed is not faster; It doesn't matter. If there is a problem, it will only affect the opening speed. JB: Ok, LET me verify that the speed is optimized and the web opening function is normal. (Some time later) JB:OK, that's all right. You can go online now. (Some time after launch) Leader: JB, why can't anything be displayed here? The morning was normal; Later confirmed, is the morning r & D said that the web page speed optimization, but the two have no relationship! Nima!!!Copy the code

In daily life, it is very common for the R & D to secretly use the code or give insufficient influence surface after the R & D modification, leading to online problems, especially in small companies.

Is there a solution to this problem? The reason comes down to my personal belief that tests know too little; People are not consciously, if blindly rely on the information provided by r & D, if one day R & D can not say clearly, or did not say at all, and then directly online, how to break?

Some students may jump out and say that this solution is feasible through process constraints, but this time I want to introduce the use of tools to solve this problem;

So the idea came up:

Obtain the record of every r&d submission and notify the corresponding students ~ through the nail/emailCopy the code

Will this prevent the above scenario?

The answer is definitely no, but did this, at least test students have more information input, and is no longer a three-know-nothing, how to use these information, will depend on the specific students ~

rendering

XX pushed to the XX branch of the project name XX- submit record SHA value + submit content information

The style is: JB pushes to the XX branch of the project name JB-submit record SHA value + submit content information

Function description: Click the project name can jump to the warehouse address, click the branch name can jump to the warehouse branch address; Click on the SHA value to jump to the corresponding submission record

The task was disassembled and divided into 2 pieces:

1. Obtain data. 2Copy the code

Nailing notice

Access to the nail, there are documents on the official website, click to view;

There is a link to how to build a new robot on the nail, which is not introduced here; As you can see, nails support the following types:

Text Type Link Type MarkDown Type Overall forward ActionCard Type Independent Forward ActionCard Type FeedCard typeCopy the code

Choose a different type depending on the result you want. For example, in this article, we chose the markdown type.

Json request import json headers = {' content-type ': 'application/json'} String_textMsg = { "msgtype": "markdown", "markdown": { "title" : "jbtest", "text": "jb is here" } } requests.packages.urllib3.disable_warnings() String_textMsg = json.dumps(String_textMsg) requests.post(dingdingurl, data=String_textMsg, headers=headers, verify=False)Copy the code

Here the dingdingURL needs to be modified into your robot Webhook, as for how to get webhook, look at the above official website document, there is an introduction, here is not detailed;

The code above looks like this:

The title corresponds to the title shown in Figure 1, and the text is the content after clicking in. Ok, the nail access is so simple ~ code, should not need to explain, is according to the requirements of the nail, build the request header, and then fill in the corresponding information, and initiate the POST request. But there’s a question here, what does this code do?

requests.packages.urllib3.disable_warnings()
Copy the code

You can try it out and see what happens if you comment it out:

InsecureRequestWarning: Unverified HTTPS request is being made. 
Adding certificate verification is strongly advised.
Copy the code

This is a security warning because at the time of the request, verify=False, which means HTTPS is not verified; In this case, if you remove this, it will still work, but most things that involve HTTPS will be checked, and for debugging purposes, verify=False; This is such a reason ~

Git log Obtains commit records

Git log: git log: git log: git log: git log:

Git log --pretty=format:\"%an-%h-%s-% h \Copy the code

%an %h %s %h %h

options instructions
%an Name of the author
%h A short hash string of the submitted object
%s Submit instructions
%H The full hash string of the commit object

The final result is as follows:

One might ask, why are we using these parameters? Because the result we’re showing is that we want these fields;

Author repository name and URL branch name as well as a short hash substring and URL submission description of the URL submission objectCopy the code

Git log has obtained 3 parameters, so there are still 5 parameters, which can be obtained by other methods

.gitlab-ci.yml

By default, the runner is configured. If the runner is still configured, check the gitlab CI article at the top

Create a file on the project front page as required:

.gitlab-ci.yml
Copy the code

After creating a new file and editing it, it is not difficult to write the following code:

stages:
  - test

job_test:
  stage: test
  variables: 
      branch_url: $CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME
      commit_url: $CI_PROJECT_URL/commit/$CI_COMMIT_SHA
  script:
    - python getcommitlog.py $CI_PROJECT_URL $branch_url $CI_PROJECT_NAME $commit_url $CI_COMMIT_REF_NAME
  only:
    - master
Copy the code

The above code is written according to JB’s git journey –.gitlab-ci.yml introduction.

Here’s another explanation:

Define a stage containing a stage called test. Then define a job_test task, which corresponds to a stage called test; Then create two variables, branch_URL and commit_URL. The specific values are the concatenation of different variables. After creating the variables, execute the getCommitlog. py script, pass a bunch of arguments, and only allow the master branch to execute.Copy the code

Several system variables are involved when defining variables and executing script parameter passing. The meanings are as follows:

variable describe
CI_PROJECT_URL Access the HTTP address of the project
CI_COMMIT_REF_NAME The branch or tag name for the build project
CI_COMMIT_SHA Build the commit SHA value of the project
CI_PROJECT_NAME The name of the project currently being built (actually the project folder name)

For more information about system variables, see the git journey to JB. Gitlab-ci.yml.

So what a custom variable ultimately means is:

  • Branch_url: branch address, of course, such as http://xxxx/jb/jbtest/tree/master
  • Commit_url: submit record addresses, such as http://xxxx/jb/jbtest/commit/ SHA

The other four parameters are explained as follows: project address, project name, commit branch name, and SHA value of commit

getcommitlog.py

Introduced. Gitlab – ci. Yml, that is to introduce the call getcommitlog. Py script The script logic is very simple, 1) for the git log information and processing the information, 2) nailing post, the code is as follows:

import re import os import requests import json import sys def getContent(): # submit records information content = (OS popen (" git log - pretty = format: \ "% the an - h - & & % % s \" 1 "), read ()). The split (" & ") return the content def postDingDing(content): print(sys.argv) project_url = sys.argv[1] branch_url = sys.argv[2] project_name = sys.argv[3] commit_url = sys.argv[4] branch_name = sys.argv[5] name,shortcodenum,explain = content dingdingurl = Your robot access_token "[" https://oapi.dingtalk.com/robot/send?access_token=] headers = {' the content-type: 'application/json'} String_textMsg = { "msgtype": "markdown", "markdown": { "title" : "jbtest", "text": "# # # #" + name + "push to []" + project_name + "(" + project_url +") "+" the [" + + "branch_name] (" + branch_url +") branch \ n > "+ name +" -["+shortcodenum+"]("+commit_url+")"+" "+explain } } requests.packages.urllib3.disable_warnings() String_textMsg = json.dumps(String_textMsg) for i in dingdingurl: requests.post(i, data=String_textMsg, headers=headers, verify=False) print(name,shortcodenum,explain,project_url,branch_url,project_name,commit_url,branch_name) if __name__ ==  "__main__": content = getContent() postDingDing(content)Copy the code

The script is very simple: 1) get git log value, 2) get parameter, 3) string concatenation, 4) pin post notification, this part will not be introduced, very simple code;

Yml to the root directory of the repository, getCommitLog. py custom directory, except that.gitlab-ci.yml also needs to change the path, then commit the two files, then commit the code in the master branch, If you do not want to specify the master branch, remove the only master branch;

In addition to the spikes can receive notifications, gitlab CI can also see the running status, in the warehouse-CI/cd-jobs, you can see all. Gitlab-ci running status

digression

Some students may ask, if there is a script, such as the one above, that wants all warehouses to be used low, wouldn’t each warehouse have to upload these two files? Gitlab-ci. yml is a script that is required by each repository, but can be shared. Use the specified execution path in. Gitlab-ci. yml.

For example, warehouse A has script A, and if you want warehouse B to use script A, you can execute script A of warehouse A in the. Gitlab-ci. yml file of warehouse B, using the absolute path, if there is no permission, just give permission.

For example, if the gitRunner installation directory is on /home, there will be A directory on the Runner server when executing repository A:

13c1a0f2

From now on, you can see what r&d has submitted on the nail, click on the warehouse address and see what r&d has written (if you have access to the warehouse)

summary

This paper mainly introduces how to obtain submission records through Gitlab CI, which involves the syntax of Git log and Gitlab ci.yml and the use of pin interface push. These are basic knowledge points.

Thank you.