极狐gitlab runner 使用 cache 实例

极狐gitlab runner 使用 cache 实例

参考官方文档:

使用 cache 功能缓存项目依赖包,可以大大的减少极狐gitlab 中项目 ci 任务构建时间,本文以 兼容 s3 接口的 minio 为例。

1. 安装 minio

下载 minio server

wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
mv minio /usr/local/bin/minio
minio -v

minio version RELEASE.2022-02-26T02-54-46Z

下载 minio client

wget https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
mv mc /usr/local/bin/mc
mc -v

mc version RELEASE.2022-02-26T03-58-31Z

配置 minio 数据目录

mkdir -p /opt/minio/data
mkdir -p /opt/minio/logs

创建 start_minio_server.sh 启动脚本

#!/bin/bash
export MINIO_ROOT_USER=minio
export MINIO_ROOT_PASSWORD=minio123456
nohup minio server /opt/minio/data > /opt/minio/logs/minio.log 2>&1 &

启动 minio server

chmod +x start_minio_server.sh
./start_minio_server.sh
  • 默认对外服务端口 9000

为客户端设置别名

mc alias set myminio http://10.10.10.60:9000 minio minio123456

配置存储桶

mc mb myminio/gitlab-cache

2. 配置 runner

config.toml 增加以下 cache 配置:

concurrent = 10
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "docker runner"
  url = "https://gitlab.leffss.cn"
  token = "xdfVPzdySQQNPCVgBxK1"
  executor = "docker"
  [runners.cache]
    type = "s3"
    Shared = true
    [runners.cache.s3]
      ServerAddress = "10.10.10.60:9000"
      AccessKey = "minio"
      SecretKey = "minio123456"
      BucketName = "gitlab-cache"
      Insecure = true

重启 runner 生效

3. 使用 cache

3.1 python

.gitlab-ci.yml 参考

image: python:3.9.7

stages:
  - test

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  
cache:
  paths:
    - .cache/pip/
  # 以项目 id 区分 cache,如果不区分,就是全局 cache
  key: $CI_PROJECT_ID

job1:
  stage: test
  script:
    - pip install ansible==2.9.2

or

image: python:3.9.7

stages:
  - test

cache:
  paths:
    - pip-cache
  # 以项目 id 区分 cache,如果不区分,就是全局 cache
  key: $CI_PROJECT_ID

before_script:
  - export PIP_CACHE_DIR="pip-cache"
  - mkdir -p pip-cache

job1:
  stage: test
  script:
    - pip install ansible==2.9.2

3.2 nodejs

.gitlab-ci.yml 参考

variables:
  NPM_CONFIG_CACHE: npm_cache
  NPM_CONFIG_REGISTRY: https://registry.npm.taobao.org

default:
  cache:
    paths:
      - ${NPM_CONFIG_CACHE}

build:
  stage: build
  image: node:14-alpine
  script:
    - node -v
    - npm -v
    - npm ci
    - npm run build
  artifacts:
    name: "build-package"
    paths:
      - dist
    expire_in: 1 day

3.3 java maven

.gitlab-ci.yml 参考

build:
  image: maven:3.8.5-jdk-11
  stage: build
  variables:
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  cache:
    paths:
      - .m2/repository/
      - target/
    key: $CI_PROJECT_ID
  script:
    - mvn package
    - ls -l target/*

3.4 golang

.gitlab-ci.yml 参考

.go-cache:
  variables:
    GOPATH: $CI_PROJECT_DIR/.go
  before_script:
    - mkdir -p .go
  cache:
    paths:
      - .go/pkg/mod/

test:
  image: golang:1.17
  extends: .go-cache
  script:
    - go test ./... -v -short

3.5 ruby

.gitlab-ci.yml 参考

image: ruby:2.6

cache:
  key: $CI_PROJECT_ID
  paths:
    - vendor/ruby

before_script:
  - ruby -v
  - bundle config set --local path 'vendor/ruby'
  - bundle install -j $(nproc)

rspec:
  script:
    - rspec spec

3.6 php

php 是世界上最好的语言!!!

.gitlab-ci.yml 参考

image: php:7.2

cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - vendor/

before_script:
  - curl --show-error --silent "https://getcomposer.org/installer" | php
  - php composer.phar install

test:
  script:
    - vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
posted @ 2022-08-14 17:08  leffss  阅读(1496)  评论(0编辑  收藏  举报