Gitea Actions实现简单CI

2,109 阅读3分钟

已有项目部署在Gitea上,为实现CI/CD,通过Gitea Actions实现自动拉取Gitea仓库代码到Docker容器构建的自动化全过程。

1. Gitea Actions相关环境准备

1.1 启用仓库粒度Actions

管理员权限下,选择需要开启Actions的库,设置-仓库-勾选启用Actions

image.png

image.png

如果Gitea版本1.21.0之前需要先手动开启Gitea Actions

image.png

1.2 配置Runner

Gitea Actions 需要runner来运⾏作业,可通过docker或二进制文件方式安装runner,这里选择docker进行安装。

  • 获取令牌

获取令牌可通过命令行或页面方式,这里选择通过页面获得令牌。在启用仓库Actions后,设置-Actions-Runner右上角可复制令牌。 image.png

  • docker-compose编译Runner Docker

编写docker-compose构建Runner Docker容器。

services:
  customer_runner:
    image: gitea/act_runner:nightly  # 使用最新的夜间构建版本 

    container_name: customer_runner
    environment:
      - CONFIG_FILE=/customer/config_customer.yaml  # 指定配置文件路径
      - GITEA_INSTANCE_URL=http://xxx:3000  # 替换为你的 Gitea 实例地址
      - GITEA_RUNNER_REGISTRATION_TOKEN=xxx-TOKEN  # 替换为你的 Runner 注册 Token
      - GITEA_RUNNER_NAME=customer  # Runner 名称
      - GITEA_RUNNER_LABELS=ubuntu-latest  # 标签

    volumes:
      - ./config_customer.yaml:/customer/config_customer.yaml  # 将本地的 config.yaml 挂载到容器中
      - ./runner_data_customer:/customer/runner_data_customer  # 持久化 Runner 数据
      - /var/run/docker.sock:/var/run/docker.sock  # 允许 Runner 访问 Docker Socket
  • Runner配置文件
# Example configuration file, it's safe to copy this as the default config file without any modification.

# You don't have to copy this file to your instance,
# just run `./act_runner generate-config > config.yaml` to generate a config file.

log:
  # The level of logging, can be trace, debug, info, warn, error, fatal
  level: info

runner:
  # Where to store the registration result.
  file: .runner
  # Execute how many tasks concurrently at the same time.
  capacity: 1
  # Extra environment variables to run jobs.
  envs:
    RUNNER_TOOL_CACHE: /customer/toolcache
    A_TEST_ENV_NAME_1: a_test_env_value_1
    A_TEST_ENV_NAME_2: a_test_env_value_2
  # Extra environment variables to run jobs from a file.
  # It will be ignored if it's empty or the file doesn't exist.
  env_file: .env
  # The timeout for a job to be finished.
  # Please note that the Gitea instance also has a timeout (3h by default) for the job.
  # So the job could be stopped by the Gitea instance if it's timeout is shorter than this.
  timeout: 3h
  # The timeout for the runner to wait for running jobs to finish when shutting down.
  # Any running jobs that haven't finished after this timeout will be cancelled.
  shutdown_timeout: 0s
  # Whether skip verifying the TLS certificate of the Gitea instance.
  insecure: false
  # The timeout for fetching the job from the Gitea instance.
  fetch_timeout: 5s
  # The interval for fetching the job from the Gitea instance.
  fetch_interval: 2s
  # The labels of a runner are used to determine which jobs the runner can run, and how to run them.
  # Like: "macos-arm64:host" or "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest"
  # Find more images provided by Gitea at https://gitea.com/gitea/runner-images .
  # If it's empty when registering, it will ask for inputting labels.
  # If it's empty when execute `daemon`, will use labels in `.runner` file.
  labels:
   #  - "windows-latest"
    # - "node-20" 
    - "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest"
    - "ubuntu-22.04:docker://gitea/runner-images:ubuntu-22.04"
    - "ubuntu-20.04:docker://gitea/runner-images:ubuntu-20.04"
 #envs:
    
cache:
  # Enable cache server to use actions/cache.
  enabled: true
  # The directory to store the cache data.
  # If it's empty, the cache data will be stored in $HOME/.cache/actcache.
  dir: ""
  # The host of the cache server.
  # It's not for the address to listen, but the address to connect from job containers.
  # So 0.0.0.0 is a bad choice, leave it empty to detect automatically.
  host: "xxx"
  # The port of the cache server.
  # 0 means to use a random available port.
  port: xxx
  # The external cache server URL. Valid only when enable is true.
  # If it's specified, act_runner will use this URL as the ACTIONS_CACHE_URL rather than start a server by itself.
  # The URL should generally end with "/".
  external_server: ""
# Gitea 实例地址
gitea_instance_url: "http://xxx:3000"

# Runner 的注册 Token
gitea_runner_registration_token: ""

# Runner 的名称
gitea_runner_name: "customer_runner"
container:
  # Specifies the network to which the container will connect.
  # Could be host, bridge or the name of a custom network.
  # If it's empty, act_runner will create a network automatically.
  network: ""
  # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker).
  privileged: false
  # And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway).
#image: "node:16"  # 默认容器镜像
 # options:
  #  - "run: npm install && npm test"  # 默认运行的命令
  options:
  # The parent directory of a job's working directory.
  # NOTE: There is no need to add the first '/' of the path as act_runner will add it automatically. 
  # If the path starts with '/', the '/' will be trimmed.
  # For example, if the parent directory is /path/to/my/dir, workdir_parent should be path/to/my/dir
  # If it's empty, /workspace will be used.
  workdir_parent:
  # Volumes (including bind mounts) can be mounted to containers. Glob syntax is supported, see https://github.com/gobwas/glob
  # You can specify multiple volumes. If the sequence is empty, no volumes can be mounted.
  # For example, if you only allow containers to mount the `data` volume and all the json files in `/src`, you should change the config to:
  # valid_volumes:
  #   - data
  #   - /src/*.json
  # If you want to allow any volume, please use the following configuration:
  # valid_volumes:
  #   - '**'
  valid_volumes: []
  # overrides the docker client host with the specified one.
  # If it's empty, act_runner will find an available docker host automatically.
  # If it's "-", act_runner will find an available docker host automatically, but the docker host won't be mounted to the job containers and service containers.
  # If it's not empty or "-", the specified docker host will be used. An error will be returned if it doesn't work.
  docker_host: ""
  # Pull docker image(s) even if already present
  force_pull: false
  # Rebuild docker image(s) even if already present
  force_rebuild: false

host:
  # The parent directory of a job's working directory.
  # If it's empty, $HOME/.cache/act/ will be used.
  workdir_parent:
  • 运行Docker

成功运行后,可以在仓库的设置-Actions-Runner下看到该Runner的信息。 image.png

2. Gitea工作流配置

在需要配置Actions的代码下,创建.gitea/workflow/xx.yaml,如docker.yaml,build自己的Dockerfile。

name: Build and Push Docker Image
on: [push]

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    env:
      MYREGISTRY : 私有仓库
      MYIMAGE : 镜像名
      MYCONTAINER : 容器名
      MYPORT: 端口号
    steps:
      - name: Checkout repository code
        uses: https://gitea.com/actions/checkout@v4

      - name: Build Docker image
        run: docker build --no-cache -t $MYIMAGE .

      - name: Tag and push Docker image
        run: |
          docker tag $MYIMAGE $MYREGISTRY/$MYIMAGE
          docker push $MYREGISTRY/$MYIMAGE

      - name: Check if container exists
        id: check-container
        run: |
          if docker ps -a --format '{{.Names}}' | grep -q $MYCONTAINER; then
            echo "Container $MYCONTAINER exists."
            echo "container_exists=true" >> $GITHUB_ENV
          else
            echo "Container $MYCONTAINER does not exist."
            echo "container_exists=false" >> $GITHUB_ENV
          fi

      - name: Stop and remove container if exists
        if: ${{ env.container_exists == 'true' }}
        run: |
          docker stop $MYCONTAINER
          docker rm $MYCONTAINER
          echo "Container $MYCONTAINER has been stopped and removed."

      - name: Run container if not exists
        run: |
          docker run -d -p $MYPORT --network mynetwork  --name $MYCONTAINER $MYREGISTRY/$MYIMAGE
          echo "Container $MYCONTAINER has been started."

3. 取得成功

push代码后,可在仓库的Actions下看到执行结果。 image.png