如何让我的自托管 GitHub 运行器在 docker 容器而不是本机 shell 和操作系统中运行作业?

问题描述 投票:0回答:2

我将 Raspberry Pi 4 注册为 GitHub 的自托管运行程序。它可以工作,但所有作业都在本机系统本身上运行。如何让 GitHub 也在运行器上创建 Docker 容器,以便底层系统保持干净?它是 GitLab 的默认行为,我很惊讶它不适用于 GiHub,或者也许我错过了一些东西,请帮助我。

# This is a basic workflow to help you get started with Actions

name: CI Rasp

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: self-hosted

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run blinky for 3 times
        run: /home/jbron/blinky.py
docker github-actions github-actions-self-hosted-runners
2个回答
2
投票

我正在尝试使用

container
上下文来实现相同的目标。

我相信它看起来像这样:

# This is a basic workflow to help you get started with Actions

name: CI Rasp

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: self-hosted
    container:
      # Runner docker image
      image: ubuntu:22.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run blinky for 3 times
        run: /home/jbron/blinky.py

查看此链接以获取更多信息:

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idcontainer


0
投票

使用@Ger指出的容器方法。请注意,文档说它将使用来自 docker 注册表的图像。
如果您要在自托管运行器上运行自定义 Docker 映像,则必须使用公共 docker 注册表(如 GitHub Docker 容器注册表),或者可以在运行器上托管您自己的 docker 注册表,以避免将映像上传/下载到公共服务器。

在您的自托管运行器上,您必须安装 docker,然后启动注册表(这是一个 docker 映像),标记您的映像并将其推送到注册表:

docker run -d -p 5000:5000 --restart=always --name registry -v /home/my-runner/docker-registry:/var/lib/registry registry:2
docker tag my-docker-image:my-runner localhost:5000/my-docker-image
docker push localhost:5000/my-docker-image

现在您可以使用工作流程中的标签名称在 my-docker-image 中运行命令:

name: Run my-docker-image
on:
  push:
    branches: 
      - '**'

jobs:
  init:
    runs-on: [ self-hosted, Linux ]  
    steps:
    - name: 'Run on the self-hosted runner host'
      run: |
        (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)

  run_docker:
    needs: [ init ]
    runs-on: [ self-hosted, Linux ]
    container:
      image: localhost:5000/my-docker-image
      volumes:
        - /data/downloads:/data/downloads
    steps:
      - name: 'Run inside container'
        run: |
          (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)
© www.soinside.com 2019 - 2024. All rights reserved.