mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 03:53:37 +08:00
175 lines
5.3 KiB
YAML
175 lines
5.3 KiB
YAML
name: 'Setup Build Environment'
|
|
description: '配置构建发布环境,包括 Docker、kubectl 和基础工具'
|
|
author: 'Your Organization'
|
|
|
|
branding:
|
|
icon: 'settings'
|
|
color: 'blue'
|
|
|
|
inputs:
|
|
docker-registry:
|
|
description: 'Docker 私有仓库地址'
|
|
required: false
|
|
default: 'docker-registry.bjxgj.com'
|
|
|
|
docker-username:
|
|
description: 'Docker 仓库用户名'
|
|
required: false
|
|
default: 'ci-action'
|
|
|
|
docker-password:
|
|
description: 'Docker 仓库密码'
|
|
required: true
|
|
|
|
kube-config:
|
|
description: 'Base64 编码的 kubectl 配置文件'
|
|
required: false
|
|
default: ''
|
|
|
|
cache-key:
|
|
description: '缓存键值,用于依赖缓存'
|
|
required: false
|
|
default: 'setup-env'
|
|
|
|
use-aliyun-mirror:
|
|
description: '是否使用阿里云镜像源 (true/false)'
|
|
required: false
|
|
default: 'true'
|
|
|
|
git-user-name:
|
|
description: 'Git 用户名'
|
|
required: false
|
|
default: 'GiteaActions'
|
|
|
|
git-user-email:
|
|
description: 'Git 用户邮箱'
|
|
required: false
|
|
default: 'actions@gitea.com'
|
|
|
|
skip-kubectl:
|
|
description: '跳过 kubectl 安装和配置 (true/false)'
|
|
required: false
|
|
default: 'false'
|
|
|
|
skip-docker-login:
|
|
description: '跳过 Docker 登录 (true/false)'
|
|
required: false
|
|
default: 'false'
|
|
|
|
outputs:
|
|
cache-hit:
|
|
description: '缓存是否命中'
|
|
value: ${{ steps.cache-setup.outputs.cache-hit }}
|
|
|
|
kubectl-version:
|
|
description: '安装的 kubectl 版本'
|
|
value: ${{ steps.kubectl-version.outputs.version }}
|
|
|
|
docker-version:
|
|
description: '安装的 Docker 版本'
|
|
value: ${{ steps.docker-version.outputs.version }}
|
|
|
|
runs:
|
|
using: 'composite'
|
|
steps:
|
|
- name: 配置Git
|
|
shell: bash
|
|
run: |
|
|
echo "🔧 配置 Git 用户信息..."
|
|
git config --global user.name "${{ inputs.git-user-name }}"
|
|
git config --global user.email "${{ inputs.git-user-email }}"
|
|
echo "✅ Git 配置完成"
|
|
|
|
- name: 缓存依赖与配置环境
|
|
id: cache-setup
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: |
|
|
/var/cache/apt/archives
|
|
/var/lib/apt/lists
|
|
/usr/bin/curl
|
|
/usr/bin/gpg
|
|
/usr/bin/base64
|
|
/usr/share/ca-certificates
|
|
/etc/apt/keyrings/docker.gpg
|
|
/etc/apt/keyrings/kubernetes-apt-keyring.gpg
|
|
/etc/apt/sources.list.d/kubernetes.list
|
|
/etc/apt/sources.list.d/docker.list
|
|
/usr/bin/docker
|
|
/usr/bin/buildx
|
|
/usr/bin/docker-compose
|
|
/usr/libexec/docker/cli-plugins/docker-buildx
|
|
/usr/libexec/docker/cli-plugins/docker-compose
|
|
/usr/bin/kubectl
|
|
key: ${{ runner.os }}-${{ inputs.cache-key }}-${{ hashFiles('**/action.yml') }}-v2
|
|
restore-keys: |
|
|
${{ runner.os }}-${{ inputs.cache-key }}-
|
|
|
|
- name: 配置环境(如果未命中缓存)
|
|
if: steps.cache-setup.outputs.cache-hit != 'true'
|
|
shell: bash
|
|
run: ${{ github.action_path }}/scripts/setup-environment.sh
|
|
env:
|
|
USE_ALIYUN_MIRROR: ${{ inputs.use-aliyun-mirror }}
|
|
SKIP_KUBECTL: ${{ inputs.skip-kubectl }}
|
|
|
|
- name: 获取 Docker 版本
|
|
id: docker-version
|
|
shell: bash
|
|
run: |
|
|
if command -v docker &> /dev/null; then
|
|
VERSION=$(docker --version | cut -d' ' -f3 | cut -d',' -f1)
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "🐳 Docker 版本: $VERSION"
|
|
else
|
|
echo "version=not-installed" >> $GITHUB_OUTPUT
|
|
echo "⚠️ Docker 未安装"
|
|
fi
|
|
|
|
- name: 获取 kubectl 版本
|
|
id: kubectl-version
|
|
if: ${{ inputs.skip-kubectl != 'true' }}
|
|
shell: bash
|
|
run: |
|
|
if command -v kubectl &> /dev/null; then
|
|
VERSION=$(kubectl version --client --short 2>/dev/null | cut -d' ' -f3 || echo "unknown")
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "☸️ kubectl 版本: $VERSION"
|
|
else
|
|
echo "version=not-installed" >> $GITHUB_OUTPUT
|
|
echo "⚠️ kubectl 未安装"
|
|
fi
|
|
|
|
- name: 配置 kubectl
|
|
if: ${{ inputs.kube-config != '' && inputs.skip-kubectl != 'true' }}
|
|
shell: bash
|
|
run: ${{ github.action_path }}/scripts/setup-kubectl.sh
|
|
env:
|
|
KUBE_CONFIG_BASE64: ${{ inputs.kube-config }}
|
|
|
|
- name: 验证 kubectl 配置
|
|
if: ${{ inputs.kube-config != '' && inputs.skip-kubectl != 'true' }}
|
|
shell: bash
|
|
run: ${{ github.action_path }}/scripts/verify-kubectl.sh
|
|
|
|
- name: 登录私有 Docker 仓库
|
|
if: ${{ inputs.skip-docker-login != 'true' }}
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ inputs.docker-registry }}
|
|
username: ${{ inputs.docker-username }}
|
|
password: ${{ inputs.docker-password }}
|
|
|
|
- name: 环境配置完成
|
|
shell: bash
|
|
run: |
|
|
echo "🎉 环境配置完成!"
|
|
echo "📋 已安装的工具:"
|
|
if command -v docker &> /dev/null; then
|
|
echo " - Docker: $(docker --version | cut -d' ' -f3 | cut -d',' -f1)"
|
|
fi
|
|
if command -v kubectl &> /dev/null && [[ "${{ inputs.skip-kubectl }}" != "true" ]]; then
|
|
echo " - kubectl: $(kubectl version --client --short 2>/dev/null | cut -d' ' -f3 || echo "已安装")"
|
|
fi
|
|
echo " - Git: $(git --version | cut -d' ' -f3)"
|