Hexo-Anatolo主题添加gitalk评论系统

一开始准备使用gitment作为评论系统,但是搭完发现已经不可用,随后找到了差不多的gitalk
gitalk已经有很多主题集成,但是很遗憾我使用的Anatolo还没有,尝试了下还算简单,现在把大概步骤写一下

相关地址

Anatolo主题:https://github.com/Ben02/hexo-theme-Anatole
gitalk评论:https://github.com/gitalk/gitalk
md5:https://github.com/blueimp/JavaScript-MD5 (issue label name 不允许超过50字符,所以使用md5生成id)

使用

准备工作

选择一个公共github存储库(已存在或创建一个新的github存储库)用于存储评论
创建GitHub Application,Authorization callback URL填写博客地址(点此申请
生成Client Id及Client Secret,复制保留
2022/02/20220218105941

主题更改

定位到Anatolo主题文件夹内,打开{Anatolo Path}/layout/partial/comments.pug文件
最下方添加此段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if theme.gitalk
if theme.gitalk.enable == true
a#comments
#vcomments(style="margin:0 30px;")
link(rel='stylesheet' href='https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css')
script(src='https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js')
script(src='https://cdn.jsdelivr.net/npm/blueimp-md5@2.19.0/js/md5.min.js')
script.
var gitalk = new Gitalk({
clientID: '#{theme.gitalk.client_id}',
clientSecret: '#{theme.gitalk.client_secret}',
owner: '#{theme.gitalk.owner}',
repo: '#{theme.gitalk.repo}',
admin: ['#{theme.gitalk.admin}'],
id: md5(location.pathname), // Ensure uniqueness and length less than 50
distractionFreeMode: false // Facebook-like distraction free mode
})
gitalk.render('vcomments')

添加配置

在主题文件夹下_config.yml或根目录下_config.anatolo.yml添加gitalk配置

1
2
3
4
5
6
7
gitalk:
enable: true
owner: #owner
repo: #repo name
admin: #admin name
client_id: #client_id
client_secret: #client_secret

效果

2022/02/20220218132427

webhook触发jenkins进行sonar检测

目的

jenkins仅需创建一个job,git推送后自动进行sonar代码检测并上传到sonarqube

jenkins插件

已按社区推荐安装基本插件

  • Generic Webhook Trigger
  • SonarQube Scanner

准备工作

  1. sonar生成令牌

    • 登录后右上角点击”头像 > 我的账号 > 安全

    • 生成令牌并复制保存

      image-20220214140155465

  2. 将令牌添加到jenkins凭证中 “系统管理 > 管理凭证 > 添加凭证”

    • 类型选择Secret text

    • 令牌粘贴到Secret框中,ID及描述可自行设置成想要的

      image-20220214141136238

  3. jenkins添加SonarQube Server “系统管理 > 系统配置 > SonarQube servers”

    • 定义name及sonarqube地址,选中刚创建的凭证,点击保存(这里name我命名‘SonarQube’)

      20220217095143

  4. jenkins添加Sonar Scanner “系统管理 > 全局工具配置 > SonarQube Scanner”

    • 定义name,勾选自动安装,版本选最新即可,点击保存

      image-20220214141806632

创建jenkins job

  1. 新建job,选择流水线

  2. “构建触发器”选中”Generic Webhook Trigger”

    variable expression expressionType defaultValue regexpFilter
    project_name $.repository.name JSONPath
    ssh_url $.repository.ssh_url JSONPath
    ref $.ref JSONPath
    • gitlab如下
    variable expression expressionType defaultValue regexpFilter
    project_name $.project.name JSONPath
    ssh_url $.project.git_ssh_url JSONPath
    ref $.ref JSONPath
    • 定义Token,不同token可触发不同job(若token相同则触发多个job)
  3. 定义流水线

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    pipeline {
    agent any

    stages {
    stage('Check Out!') {
    steps {
    script {
    //解析分支
    arr = "${env.ref}".split("refs/heads/") as List
    currentBranch = arr[1]
    }
    git branch: currentBranch, credentialsId: '343aa12a-92a3-4797-a9ea-a8facb4db380', url: '${ssh_url}' //credentialsId为ssh私钥,用于拉git代码
    }
    }

    stage('Sonar Scan!') {
    steps {
    script {
    sonarqubeScannerHome = tool name: 'SonarQube Scanner' //准备工作第4步的name
    withSonarQubeEnv('SonarQube') { //准备工作第3步的name,只有一个时可省略
    sh """
    ${sonarqubeScannerHome}/bin/sonar-scanner \
    -Dsonar.projectKey=${project_name} \
    -Dsonar.projectName=${project_name} \
    -Dsonar.sources=./
    """
    }
    }
    }
    }

    }
    }

定义webhook(github为例)

  1. 打开项目”Settings > Webhooks”

  2. 写入url及创建job第二步的token(注意token在url后)

    20220217094616

验证

  • 触发push事件后,可看到jenkins开始构建,完成后sonarqube平台也可看到相关报告

php getenv无法获取环境变量

现象

php在nginx+fpm模式下getenv()方法获取到的内容与cli模式下不同

举例

  • shell下输出HOSTNAME

    1
    2
    ➜  ~ echo $HOSTNAME
    php-nginx
  • nginx+fpm无法获取HOSTNAME环境变量

    1
    2
    3
    ➜  ~ cat test.php
    <?php
    var_dump(getenv("HOSTNAME"));
  • php cli模式下输出

    1
    2
    ➜  ~ php -r 'echo getenv("HOSTNAME"), PHP_EOL;'
    php-nginx

原因

nginx+fpm下环境变量通过nginx的fastcgi传递,所有环境变量需要在nginx配置文件下的fastcgi_params文件指定

解决

增加fastcgi_params参数,并重启nginx

1
2
3
echo "fastcgi_param  HOSTNAME       php-nginx;" >> /etc/nginx/fastcgi_params

nginx -s reload

rabbitmq-channel断网后没有断开情况记录

现象

队列中一直有unacked数据

复现

监听队列,使用不自动应答模式,拿到消息不应答,断开网络,关闭进程
rabbitmq中channel一直存在,state为idle状态
只能强制手动关闭连接

疑问

  • rabbitmq对于tcp连接断开的心跳时间?
    • 默认30分钟,根据rabbitmq.conf中consumer_timeout确定
  • 如何处理网络异常断开的unacked消息?
    • 等待连接超时,或强制断开channel

解答

来源:Consumers — RabbitMQ

If a consumer does not ack its delivery for more than the timeout value (30 minutes by default), its channel will be closed with a PRECONDITION_FAILED channel exception. The error will be logged by the node that the consumer was connected to.
The timeout value is configurable in [rabbitmq.conf] (in milliseconds):

1
2
# 30 minutes in milliseconds
consumer_timeout = 1800000

mac pecl安装swoole失败:fatal error: 'openssl/ssl.h' file not found

mac下使用brew安装arm版php7.2,pecl命令安装swoole
命令:pecl install swoole
发生如下报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/private/tmp/pear/temp/swoole/ext-src/php_swoole_private.h:84:2: error: "Enable openssl support, require openssl library"
#error "Enable openssl support, require openssl library"
^
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole.cc:16:
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole_cxx.h:20:
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole_coroutine.h:22:
In file included from /private/tmp/pear/temp/swoole/include/swoole_coroutine.h:22:
In file included from /private/tmp/pear/temp/swoole/include/swoole_socket.h:33:
/private/tmp/pear/temp/swoole/include/swoole_ssl.h:27:10: fatal error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>
^~~~~~~~~~~~~~~
2 errors generated.
make: *** [ext-src/php_swoole.lo] Error 1
ERROR: `make' failed

openssl本机已安装,所以只需要提示是否启用openssl指定路径即可(以下路径为brew安装openssl路径)

1
2
3
4
5
6
➜  ~ pecl install swoole

...
enable sockets supports? [no] : yes
enable openssl support? [no] : yes --with-openssl-dir=/opt/homebrew/opt/openssl
...