有效率的用 jsonnet 撰寫 Drone CI/CD 設定檔

Jsonnet + Drone

Drone 在 1.0 版本推出了用 jsonnet 來撰寫 YAML 設定檔,方便開發者可以維護多個專案設定。不知道大家有無遇過在啟動新的專案後,需要從舊的專案複製設定到新專案,或者是在 .drone.yml 內有非常多重複性的設定,假設 Go 語言的開源專案需要將執行檔包成 ARM64 及 AMD64 的映像檔,並且上傳到 Docker Hub,底下是 AMD64 的設定檔範例。剛好在 Udemy 課程內有學員詢問到相關問題

---
kind: pipeline
name: linux-arm64

platform:
  os: linux
  arch: arm64

steps:
- name: build-push
  pull: always
  image: golang:1.11
  commands:
  - "go build -v -ldflags \"-X main.build=${DRONE_BUILD_NUMBER}\" -a -o release/linux/arm64/drone-discord"
  environment:
    CGO_ENABLED: 0
    GO111MODULE: on
  when:
    event:
    - push
    - pull_request

- name: build-tag
  pull: always
  image: golang:1.11
  commands:
  - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}\" -a -o release/linux/arm64/drone-discord"
  environment:
    CGO_ENABLED: 0
    GO111MODULE: on
  when:
    event:
    - tag

- name: executable
  pull: always
  image: golang:1.11
  commands:
  - ./release/linux/arm64/drone-discord --help

- name: dryrun
  pull: always
  image: plugins/docker:linux-arm64
  settings:
    dockerfile: docker/Dockerfile.linux.arm64
    dry_run: true
    password:
      from_secret: docker_password
    repo: appleboy/drone-discord
    tags: linux-arm64
    username:
      from_secret: docker_username
  when:
    event:
    - pull_request

- name: publish
  pull: always
  image: plugins/docker:linux-arm64
  settings:
    auto_tag: true
    auto_tag_suffix: linux-arm64
    dockerfile: docker/Dockerfile.linux.arm64
    password:
      from_secret: docker_password
    repo: appleboy/drone-discord
    username:
      from_secret: docker_username
  when:
    event:
    - push
    - tag

trigger:
  branch:
  - master
Continue reading “有效率的用 jsonnet 撰寫 Drone CI/CD 設定檔”