Github Action实践练习3_矩阵策略与生产环境复审

矩阵策略练习

应用上线前,需要在多个版本或多个平台测试兼容性。编写workflow,在多个python版本测试。

在./github/workflows里创建matrix-remote.yaml文件。

 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
name: Matrix

on:
  push:
    branches: [ "main" ]

jobs:
  # 矩阵策略练习
  test_matrix:
    name: Test on Python ${{ matrix.python-version }}
    runs-on: ubuntu-24.04
    strategy:
      # 矩阵配置:定义测试变量
      matrix:
        python-version: ["3.9", "3.10", "3.11"]
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}

      - name: Run a simple test
        run: |
          python -c "print('Testing on Python ${{ matrix.python-version }}')"
          # 模拟测试通过
          echo "Matrix tests passed!"          

安全密钥前置工作

在服务器或本地虚拟机创建ssh密钥用于GitHub仓库远程连接:

1
2
3
4
5
6
ssh-keygen -t ed25519 -C "github_actions_key"

cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys

# 查看私钥
cat ~/.ssh/id_ed25519

配置Github仓库密钥

  1. 在Github仓库打开setting -> Secrets and variables -> Actions,
  2. 点击New repository secret,名称(Name)填入SSH_PRIVATE_KEY,
  3. 值(Value)填入刚刚查看的私钥完整内容(包括—–BEGIN OPENSSH PRIVATE KEY—–开头结尾)

配置仓库的环境(Environment)

注:这个功能必须仓库是public才能开启,在Settings划到最底下的Danger ZoneChange repository visibility里可以调整仓库为public或private

  1. 进入Settings -> Environments,
  2. 点击New environment,取名为production
  3. 在Deployment branches选择All branches(或者限制为主分支), 勾选Required reviewers(必须审批人), 在搜索框填入自己的Github用户名,点击Save protection rules。

这样,代码提交到生产(production)环境后,就需要指定审批人通过才能部署

本地虚拟机安装Github Action自托管Runner模拟远程服务器

用的服务器的话就可以跳过这一步。Github并不推荐用本地Runner,因为如果有人fork了你的仓库,可能会在本地机器上运行恶意代码。

在虚拟机安装并注册Runner。在仓库点击Settings -> Actions -> Runnerss -> New self-hosted runner。

按提示运行配置脚本。提示会让你给本地runner取名,一路enter后默认的虚拟机名是[self-hosted, linux, X64]

从GitHub下载tar包这一步可能下不下来,建议本地下载了再传到虚拟机上。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 下载
#   创建并进入文件夹
mkdir actions-runner && cd actions-runner
#   下载runner压缩包
curl -o actions-runner-linux-x64-2.333.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.333.1/actions-runner-linux-x64-2.333.1.tar.gz
#   校验压缩包hash值
echo "18f8f68ed1892854ff2ab1bab4fcaa2f5abeedc98093b6cb13638991725cab74  actions-runner-linux-x64-2.333.1.tar.gz" | shasum -a 256 -c

# 解压
tar xzf ./actions-runner-linux-x64-2.333.1.tar.gz

# 配置
# Create the runner and start the configuration experience
./config.sh--url https://github.com/你的用户名/仓库名 --token YOUR_TOKEN

# 运行
$ ./run.sh

# 在workflows的yaml文件里使用本地runner
runs-on: self-hosted

安全密钥练习

在.github/workflows文件夹下创建safety-local.yaml文件。

正经流程应该是先多版本/平台测试后,再部署上线。也即是在安全密钥job之前应该有矩阵测试的job。不过矩阵测试的actions需要下载checkout actions和安装python。这里示例就拆分成了两个练习。

 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
34
name: Security 

on:
  push:
    branches: [ "main" ]

jobs:
  # SSH 连接与生产环境复审,部署到本地虚拟机
  deploy_to_local:
    name: Deploy to Local VM (Production)
    runs-on: [self-hosted, linux, X64]
    environment: production
    
    steps:

      # 安全技巧:安装 SSH 私钥
      # 注意:为了防止泄露,使用echo而不是cat
      - name: Setup SSH Key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519          

      - name: Connect and Execute
        run: |
          echo "Attempting to connect to local VM via ngrok..."
          # 使用 SSH 连接,执行 echo 命令
          ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 rust@localhost "
            echo '===================================='
            echo 'Hello from GitHub Actions!'
            echo 'This message is running on 本地虚拟机$(hostname). User is $(whoami)'
            echo 'Current Time:' $(date)
            echo '===================================='
          "          
网站总访客数:Loading
网站总访问量:Loading
使用 Hugo 构建
主题 StackJimmy 设计