Bs the opening

I have the habit of buying cloud host since university, ali Cloud, Tencent cloud have used. The main reason for using cloud hosting is that I got tired of desktop Linux in college and went back to Windows and Mac. However, since the working environment is usually Linux, I still need a Linux environment so that I can continue to study and research in my spare time. I also tried to set up my own blog and so on. At the beginning, because of student discount, it cost less than a few dollars a year. After working in Tencent, I often use the company’s monthly welfare (Tencent cloud voucher) to renew the cloud host fee, but I don’t think it is expensive. After leaving Tencent, I still insist on using Tencent cloud and renew the fee every year. Until this year…

I found that the annual renewal fee is thousands of yuan, and this is only a very low allocation system. 1 core, 2 GB memory…… At first I wanted to upgrade to a multi-core configuration to test some parallel programs, but it turned out to be too expensive. I realized I didn’t really need a cloud host. Instead of spending thousands of dollars every year to “rent” a high cloud host, it is better to buy a high host at home.

Besides, I no longer try to set up an independent blog. It is enough to write something on zhihu and the official account. Once I also studied the public number, small program development, at that time does need a cloud host back-end to provide services, but later energy is not enough, I did not continue to study these.

After realizing that “cloud hosting” was a bogus requirement for me, the only thing that kept me from abandoning it entirely was this small requirement: I had a script of scheduled tasks on the cloud hosting to record something. For example, I have a regular task to record the daily changes in the number of fans and likes of Zhihu.

Github.com/guodongxiar…

This demand with their own computer is difficult to ensure that every day can run (after all, to shut down is not, sometimes at night standby can not perform scheduled tasks). So I wondered if I could find a replacement. I soon discovered Github’s Action feature.

Get to the point

Github Actions are probably a new feature added in the last few years that can be used to CI/CD projects. Originally, if you had a need for this, you might have used a third party site like Travis.

Create a directory and configuration file

Github Actions are configured in yamL format. Github /workflows/ Add the yamL configuration file to the workflows directory. Github refers to such tasks as “workflows” as you can see from the directory name.

Specify workflow name

Edit the YAML by specifying the name of the workflow:

name: xxxx
Copy the code

Configure events that are triggered

You need to first bind an event (such as Git push, merge, or submit pull Request) and then specify a series of actions (script commands). When the corresponding event is triggered, it will automatically execute your preset action.

Such as:

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
Copy the code

This configuration specifies that it is triggered when pushing code or submitting a pull request to the master branch.

Of course, you can not specify branches, such as:

on: [push]
Copy the code

Indicates that the workflow is triggered whenever there is a push event.

Of course. Github Action also supports direct configuration of scheduled tasks.

on:
  schedule:
    - cron: "0 2 * * *"
Copy the code

You can configure multiple scheduled times under schedule. The syntax is the same as that of crontab. This task is performed at 2:00 every day.

However, there is a point to note here that Github is a foreign product after all, and the time specified by the scheduled task here is different from our Time zone in China. Beijing time is eight hours ahead of Github’s time zone. For example, on July 1 at 23:00, github time is on July 1 at 15:00. So we can configure it to be executed at 15:00 or 16:00 (11pm or 12pm Beijing time).

After the event, let’s talk about how to configure the specific task content to be performed.

Configure the system environment for performing tasks

Tasks start with Jobs, and you can specify the system environment in which the task will run.

jobs:
  build:
    runs-on: ubuntu-latest
Copy the code

** Current (2021) ** Supported system environments are:

  • ubuntu-latest.Ubuntu 18.04 或 Ubuntu 16.04
  • windows-latest 或 windows-2019
  • macos-latest 或 Macos 10.15

Configure specific task steps

You can add steps under Jobs -> Build -> Steps. Steps can have multiple steps, such as:

Jobs: build: runs-on: Ubuntu -latest steps: -uses: actions/checkout@v2 - name: Set up Python 3.9 uses: Actions /setup-python@v2 with: python-version: 3.9-name: Install dependencies run: | python -m pip install --upgrade pip pip install requestsCopy the code

These are the two steps (or actions) that represent the Checkout code, configure the Python environment, and install the dependent libraries. My script relies on Requests, so I installed it here with the PIP command.

Each step also has a name: the name of the step. Behind the run: add your own to execute commands, if there are multiple commands, use | line feed. If you have only one line of command, you can write run: after it

Next up:

    - name: Update zhihu
      run: |
        cd zhihu
        sh run.sh
Copy the code

Go to my project directory and execute the run.sh script, which I won’t cover here. Basically, I get the number of my Zhihu fans and likes, and then update them to a data.csv file.

Continue to:

    - name: Commit
      run: |
        git config --global user.email [email protected]
        git config --global user.name guodongxiaren
        git add .
        git commit -m"action:`date`"
        git pull --rebase
Copy the code

The obvious step is to initialize your Git account and make an Add and commit to the files in your project. Because I modified the data.csv file after the execution of the previous step.

    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
Copy the code

This step is to push the commit to Github’s cloud repository. This way I can see how my data changes by checking data.csv in the repository on Github.

Of course you may be wondering what the github_token is for.

This is because we typically push changes to Github, either through a password or through a pre-configured SSH public key to commit confidentially. But as a public workflow profile, we obviously don’t want to disclose our password, and we can’t generate a public key for the workflow machine and automatically configure it into our Github account. Github has designed the token mechanism for this. In fact, there was a token mechanism before Github Action existed, which allows you to call some of Github’s open APIS. If you don’t have a token to generate one, the steps are as follows:

Go to the Settings page for your account:

Choose the Developer Settings:

choosePersonal access tokens:

Click Generate new token:

Set the name to GITHUB_TOKEN, then check permissions like repo, Admin :repo_hook, and Workflow, and click Generate Token.

${{secrets.github_token}} is the only variable that can be used. ${{}} are all variables.

Running effect

Once configured, you are ready to run. The first time you use the Action function, you might have a problem editing yamL. Instead of setting it to run once a day, you can set it to another trigger to test and verify.

The execution of each workflow can be viewed here:

Click Actions to jump to a new page:

To see the latest successful tasks, click on the previous successful workflow (green ✓).

Each of these steps can be expanded. That’s it, Github Action.

Nonsense ends

So once I had this timed task configured, all the obstacles that prevented me from abandoning cloud hosting were removed. I bought a mainframe in 618. The host came with Windows 10, then I installed Ubuntu using Windows 2, and I’ve been happily playing on my own host ever since. The configuration is perfect. CPU I5-10400 (6 cores and 12 threads), memory 16G, 256G SOLID-state disk + 1T mechanical disk. Also comes with wireless card + Bluetooth as laptop! The key is that it also comes with an RTX3060 graphics card!

I was going to recommend it, but it’s no longer available… It’s a shame.