The previous blogs were all recorded in Jianshu, and occasionally some scattered articles would be sent to Denver gold and CSDN. Although the writing experience of these platforms is quite good, and there are some traffic without considering promotion, but jianshu rectified a wave recently, and some articles were locked innocently, and I felt very annoyed. If you think about all the pages on these writing platforms that are not personalized enough, you might as well set up your own personal blog so that you don’t have to worry about collateral damage from censorship or anything else, and you can add a little fun to your life.

Personal needs

Carefully considered the next personal needs, in fact, relatively simple, is to force the case must be high. So the theme has to find their favorite ~ so the individual domain name must be to ~ so must not be finished to knock a few lines of command others to see ~ so comments ah, the number of statistics ah, the number of words statistics ah, the some absolutely can not be less ~ of course must be fast enough.

Now all kinds of public account vlog and other forms of “we media” fly up, compared to blog seems a little Old School. However, the advantage of this is that the technical solutions are basically mature. If you look on the Internet, you will find that the mature technologies are good. There are directly solutions that can meet most of your needs

  • Ali’s language platform for writing
  • Webhook triggers Travis – CI automatic build
  • Download the wordfinch article to generate an MD file
  • Hexo generates blogs
  • Github – Pages is hosted as a page

Building work

I have never done front-end work before, so it took some effort to build the whole environment. However, as for the whole construction work, I can just do it according to the online article Step by step. Here, I only recorded my careless places and wasted a lot of time

The first is the naming of the Hexo theme

This is a relatively low-level error, but it did hold me up for half an hour. When I downloaded the theme, I directly downloaded the zip and unzipped it. The folder name was branched, which did not correspond to the theme name in _config.yml, so it could not generate a normal page

Then there is the question of Travis-CI SSH

  • Pay attention to.travis/ssh_configThe path of the RSA file must be correct. Since I have multiple Git repositories configured, I cannot miss any of them
Host github.com
    User git
    StrictHostKeyChecking no
    IdentityFile ~/.ssh/id_rsa_ci
    IdentitiesOnly yes
Host gitee.com
    User git
    StrictHostKeyChecking no
    IdentityFile ~/.ssh/id_rsa_ci
    IdentitiesOnly yes
Host git.dev.tencent.com
    User git
    StrictHostKeyChecking no
    IdentityFile ~/.ssh/id_rsa_ci
    IdentitiesOnly yes
Copy the code

yuque-hexoThe sparrow token

It is stated in the documentation that the public library can not use token, but trying to expose the library itself still needs token, and token should define environment variables in Travis – CI, which is safer. However, it takes time to rely on Travis – CI for each debugging. Tokens can be defined in the yuqueConfig of package.json, see #30

Continue to optimize

The blog should load up, import a few templates and test it. Everything goes well, but how can a programmer who likes to mess around be satisfied

  • Does not have a own domain name, hangs ingithub.ioThere’s not enough pressure down there
  • Github’s domestic access has been shaky
  • Wordbird’s articles are just static pages pushed to Github, and each time they are pushed, there is no history, and the original MD file is not backed up

Domain name to apply for

Because of poverty, I applied for a free domain name on Freenom, sunhapper.tk, and it was easy to bind to Github

  • Set the CNAME point of a subdomain in freenom’s DNS administrationsunhapper.github.ioI set it toBLOG
  • A subdomain that was registered before the github hosted blog page was boundblog.sunhapper.tk

You can now redirect to sunhapper.github. IO by visiting blog.sunhapper.tk

Many articles on the Internet say that it is unnecessary to set A record to point to github’S IP address. The setting of A record is to bind the domain name and IP when you have your own public IP address. Github’S IP is not actually maintained by you, so binding is meaningless

Improved access stability

dns

Bind domain name, test, personal feeling is ok, but let other friends try to find some problems

Therefore, the first step of access optimization is to change the DNS server, and apply for DNspod as the DNS server, because the company used DNspod before, and the personal blog is also free, the switching process is relatively smooth, after dnspod is set, change freenom’S DNS server to a customized one

cdn

As github access in China is also not very stable, so WE considered using CDN for acceleration, but finally did not use, mainly because

  • Domestic CDN all need to record the domain name, but a personal blog is not very want to toss about this
  • I tried Cloudflare. Although there is no need to record, its free solution nodes should be in foreign countries, and the acceleration effect is very mediocre

For the above reasons, instead of using CDN, a domestic Git repository is used for page hosting

Domestic Git hosting

In China, git repositories that provide pages functions include coding and Gitee. Gitee was widely used in the past, but only the charging version of Gitee can automatically publish and customize domain names. The static page is no different from Github. On dnspod, set the CNAME pointing to coding and set it as default, github. IO is set to foreign access because the SSL certificate site of coding is in foreign countries. Therefore, if github. IO is set as the CNAME of foreign routes, HTTPS will be affected. Therefore, in order to HTTPS, we have to reluctantly abandon multi-line support and change all access to be directed to coding

The backup

Because synchronous finches articles, generate a static page, to the custody warehouse deployment page are conducted on Travis, resulting in the blog source warehouse won’t have any change, and static page hosting warehouse push is mandatory, so there is no historical record, this is very difficult to make version rollback, and completely dependent on language finches platform, language sparrow one thousand hang up, It becomes more difficult to modify and update blog posts

Therefore, after performing the syncing operation in Travis -ci, we will add a submission and push it to Github. To be safe, we will set up a mirror repository in Gitee and also backup it. After all, MYZ Github has some action recently

This backup operation is smooth, after all, several storehouses have SSH keys and full read and write permissions, but it takes a long time to generate new commit_message

Serverless function

Looking back at the Serverless function in Hexo’s blog end-play, this function converts the webhook calls of the sparrow into the form of the Travis – CI API, with the modified filename placed as a message in the Body request field

 $post_data = json_encode(array(
        "request"= >array(
            "message"=>$message,
            "branch"=>$branch
        )
    ));
Copy the code

According to the travis-CI documentation, this message should override the TRAVIS_COMMIT_MESSAGE environment variable, but it is not actually seen at github.com/travis-ci/t… Each time the TRAVIS_COMMIT_MESSAGE is the latest committed message, there is no basis for me to create a new commit

After checking the documentation, I found that environment variables can be added to the env field of config in Couldchange builds with API V3, so add an environment variable as commit-message

    $post_data = json_encode(array(
        "request"= >array(
            "message"=>$message,
            "branch"=>$branch,
            "config"= >array(
                "env"= >array(
                    "DESC"=>preg_replace("/\t/"."",$message)
                )
            )
        )
    ));
Copy the code

.travis.yml

Modify. Travis. Yml

- NEW_MESSAGE=${DESC:-$TRAVIS_COMMIT_MESSAGE} // Use DESC if there is an environment variable, or TRAVIS_COMMIT_MESSAGE if there is no environment variable
- echo ${NEW_MESSAGE}
Copy the code

If there is a space in the middle of the environment variable, Travis -ci will select only the string before the space

The pit is exhausted, and every time the text is updated, a COMMIT is made on Git containing the modification file

todo

Dry two days, finally got a custom domain name, domestic and foreign branch line access, automatic deployment of the personal blog, of course, some things are not done, mark the memo

Figure bed problem

At present, the address of md image is automatically generated by copying directly to the page of mouse editing, but this address can only be seen when md is generated. It is troublesome to use it in other places, such as the cover, so we need to consider other graphics

Image backup

Seeing the cracked pictures on various blogs on the Internet, I naturally feel insecure about the picture bed, so I can use the picture bed for picture display, but I should also need a backup in git warehouse, so that when the picture bed is hung, I can timely migrate a line of code to extract the pictures in markdown files

Format the MD file generated by the finch

There are a lot of HTML tags in the MD file generated by the parser. For example, the newline of the paragraph is

, which has different effects in different MD parsers. Therefore, it is necessary to format it into a standard format, which is convenient for other places to publish

sed -E -i "s/

/ \n/g"
The $1 ## delete sed -E -i "s/<a\sname=\"\w*\"><\/a>//g" The $1 ## Delete the tag sed -E -i "s/\xc2\xa0/ /g" The $1 ## Remove HTML space special characters Copy the code

HTML special characters are a bit of a problem, and front-matter is a problem. See How to deal with NBSPs ina terminal for a solution

Multi-platform publishing

Every post is my own effort, and I want to make it available to as many people as possible. I also want to use Travis automatic deployment, so if you can support automatic distribution on other platforms besides Git hosting, you will be on the top of your life

The last

The first article of this blog is finally completed, and the subsequent articles from other platforms will be gradually migrated over, and the technical blog output will continue, please pay attention

A wave of advertising

Sunhapper’s blog sunhapper’s Github

Thank you

Hexo Yuque-hexo hexo-theme- Matery thanks for the open source tools and platform that helped me build this blog