在 Woodpecker 中使用全局环境变量 CI

Using global environment variables in Woodpecker CI

我正在从 GitLab CI 迁移到 Woodpecker CI。当前管道定义为:

default:
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
      - $CI_PROJECT_DIR/.m2/repository/
  image: docker.io/library/eclipse-temurin:8-jdk-focal

stages:
  - compile

variables:
  MAVEN_CONFIG: --batch-mode --no-transfer-progress
  MAVEN_OPTS: -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository/

compile:
  stage: compile
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_MERGE_REQUEST_ID
  script:
    - ./mvnw compile $MAVEN_EXTRA_OPTS
  variables:
    MAVEN_EXTRA_OPTS: -DskipTests=true

我正在尝试找到一种方法来声明 MAVEN_CONFIGMAVEN_OPTS,就像它们在 GitLab CI 中公开的方式一样(读取:所有步骤全局),但我到目前为止我还没有找到解决办法。我基本上是在后续步骤中再次声明它们。

此功能是否可用但我错过了,或者尚未实现?

您需要创建一个 .woodpecker.yml 文件来定义包含步骤的管道。看,Woodpecker CI: Intro

# .woodpecker.yml
pipeline:
  build:
    image: debian
    commands:
      - echo "This is the build step"
  a-test-step:
    image: debian
    commands:
      - echo "Testing.."

要设置环境变量,请使用 environment 键。看,Woodpecker CI: Environment Variables

pipeline:
  build:
    image: golang
    environment:
      # set environment variables
      - CGO=0
      - GOOS=linux
      - GOARCH=amd64
    commands:
      # expand/resolve environment variables by exporting
      - export PATH=$PATH:/go
      - go build
      - go test

可以通过 WOODPECKER_ENVIRONMENT 变量为所有构建全局设置环境变量。看,Woodpecker CI: Global Env Vars

应该 能够使用 yaml 锚点和别名在步骤之间共享命令,如 this example.

.set-maven-env-vars: &set-maven-env-vars
  - export MAVEN_CONFIG="--batch-mode --no-transfer-progress"
  - export MAVEN_OPTS="-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository/"

pipeline:
  build:
    image: golang
    commands:
      - *set-maven-env-vars
      - go build
      - go test