Using global environment variables in Woodpecker CI

Viewed 194

I'm migrating from GitLab CI to Woodpecker CI. The current pipeline is defined as:

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

I'm trying to find a way to declare MAVEN_CONFIG and MAVEN_OPTS in the same way they are exposed (read: globally for all steps) in GitLab CI, but I so far I haven't found a solution. I'm basically declaring them all over again in subsequent steps.

Is this functionality available and I missed it, or isn't yet implemented?

1 Answers

You need to create a .woodpecker.yml file defining a pipeline with steps. See, Woodpecker CI: Intro.

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

To set environment variables, use the environment key. See, 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

Environment variables can be set globally for all builds via the WOODPECKER_ENVIRONMENT variable. See, Woodpecker CI: Global Env Vars.

You should be able to share commands between steps with yaml anchors and aliases, as in 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
Related