본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

치환

원문 보기 위키 갱신

치환 (Substitution) — 환경 변수

Drone은 저장소와 빌드 메타데이터를 확장(expand)하거나 치환(substitute)하는 기능을 제공해서 동적인 파이프라인 설정을 가능하게 해요.

출처: Drone CI Docs

본문

Drone은 저장소와 빌드 메타데이터를 확장(expand)하거나 치환(substitute)하는 기능을 제공해서 동적인 파이프라인 설정을 가능하게 해요.

커밋 치환 예시:

kind: pipeline
type: docker
name: default

steps:
- name: publish
  image: plugins/docker
  settings:
    tags: ${DRONE_COMMIT}
    repo: octocat/hello-world

태그 치환 예시:

steps:
- name: publish
  image: plugins/docker
  settings:
    tags: ${DRONE_TAG}
    repo: octocat/hello-world

치환에 사용할 수 있는 파라미터 목록은 environment Reference를 참고하세요. 이 목록의 일부 파라미터는 치환에 사용할 수 없다는 점에 유의하세요. 스텝 이름, 스텝 번호, 그리고 상태와 타임스탬프를 저장하는 파라미터가 그 예예요.

문자열 연산 (String Operations)

Drone은 bash 문자열 연산을 부분적으로 흉내(emulation) 내요. 치환 전에 문자열 값을 조작하는 데 사용할 수 있어요.

부분 문자열(substring)을 사용한 변수 치환 예시:

steps:
- name: publish
  image: plugins/docker
  settings:
    tags: ${DRONE_COMMIT_SHA:0:8}
    repo: octocat/hello-world

v1.0.0에서 v 접두사를 떼어내는 변수 치환 예시:

steps:
- name: publish
  image: plugins/docker
  settings:
    tags: ${DRONE_TAG##v}
    repo: octocat/hello-world

/를 -로 교체하는 변수 치환 예시:

steps:
- name: publish
  image: plugins/docker
  settings:
    tags: ${DRONE_BRANCH/\//-}
    repo: octocat/hello-world

Drone은 아래 문자열 연산을 흉내 내요. Drone은 이 연산들을 최선을 다해 흉내 내지만 완벽한 흉내를 약속하지는 않아요.

${parameter^}
${parameter^^}
${parameter,}
${parameter,,}
${parameter:position}
${parameter:position:length}
${parameter#substring}
${parameter##substring}
${parameter%substring}
${parameter%%substring}
${parameter/substring/replacement}
${parameter//substring/replacement}
${parameter/#substring/replacement}
${parameter/%substring/replacement}
${#parameter}
${parameter=default}
${parameter:=default}
${parameter:-default}

이스케이프 (Escaping)

파라미터 표현식은 yaml이 파싱되기 전에 평가돼요. 시스템이 표현식을 평가하지 않게 하려면 이스케이프해야 해요.

kind: pipeline
type: docker
name: default

steps:
- name: build
  commands:
  - echo $GOOS
  - echo $${GOARCH}
  - go build
  - go test

흔한 문제 (Common Problems)

파라미터 치환은 yaml이 파싱되기 전에 일어나요. 치환 결과로 유효하지 않은 yaml 파일이 만들어지면 파싱 오류가 발생해요:

yaml: unmarshal errors:
cannot unmarshal !!map into string

특수 문자 / 예약 문자가 이스케이프되도록 파라미터를 따옴표로 감싸면 해결할 수 있어요:

kind: pipeline
type: docker
name: default

steps:
- name: notify
  image: plugins/slack
  settings:
    channel: team
    message: "${DRONE_COMMIT}"