From 09d2bafa37bc8357ca5525f6da43a9e36743553e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 10 Oct 2019 01:00:43 +0800 Subject: [PATCH] chore: upgrade docker build. --- .drone.jsonnet | 16 + .drone.yml | 480 +++++++++++++----- Dockerfile.i386 | 19 - Dockerfile => docker/Dockerfile.linux.amd64 | 9 +- Dockerfile.arm => docker/Dockerfile.linux.arm | 9 +- .../Dockerfile.linux.arm64 | 11 +- docker/manifest.tmpl | 25 + pipeline.libsonnet | 258 ++++++++++ 8 files changed, 676 insertions(+), 151 deletions(-) create mode 100644 .drone.jsonnet delete mode 100644 Dockerfile.i386 rename Dockerfile => docker/Dockerfile.linux.amd64 (75%) rename Dockerfile.arm => docker/Dockerfile.linux.arm (73%) rename Dockerfile.arm64 => docker/Dockerfile.linux.arm64 (70%) create mode 100644 docker/manifest.tmpl create mode 100644 pipeline.libsonnet diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..385485f --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,16 @@ +local pipeline = import 'pipeline.libsonnet'; +local name = 'drone-packer'; + +[ + pipeline.test, + pipeline.build(name, 'linux', 'amd64'), + pipeline.build(name, 'linux', 'arm64'), + pipeline.build(name, 'linux', 'arm'), + pipeline.release, + pipeline.notifications(depends_on=[ + 'linux-amd64', + 'linux-arm64', + 'linux-arm', + 'release-binary', + ]), +] diff --git a/.drone.yml b/.drone.yml index ed86282..97dc91e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,114 +1,368 @@ -workspace: - base: /go/src - path: github.com/appleboy/drone-packer - -clone: - git: - image: plugins/git - depth: 50 - tags: true - -pipeline: - lint: - image: golang:1.11 - pull: true - commands: - - make vet - - make revive - - make test-vendor - - make misspell-check - - make test - - codecov: - image: robertstettner/drone-codecov - secrets: [ codecov_token ] - files: - - coverage.txt - when: - event: [ push, pull_request ] - - build_linux_amd64: - image: golang:1.11 - pull: true - group: build - commands: - - make build_linux_amd64 - - build_linux_i386: - image: golang:1.11 - pull: true - group: build - commands: - - make build_linux_i386 - - build_linux_arm64: - image: golang:1.11 - pull: true - group: build - commands: - - make build_linux_arm64 - - build_linux_arm: - image: golang:1.11 - pull: true - group: build - commands: - - make build_linux_arm - - publish_linux_amd64: - image: plugins/docker:17.05 - pull: true - secrets: [ docker_username, docker_password ] - group: release - repo: ${DRONE_REPO} +--- +kind: pipeline +name: testing + +platform: + os: linux + arch: amd64 + +steps: +- name: vet + pull: always + image: golang:1.13 + commands: + - make vet + volumes: + - name: gopath + path: /go + +- name: lint + pull: always + image: golang:1.13 + commands: + - make lint + volumes: + - name: gopath + path: /go + +- name: misspell + pull: always + image: golang:1.13 + commands: + - make misspell-check + volumes: + - name: gopath + path: /go + +- name: test + pull: always + image: golang:1.13 + commands: + - make test + - make coverage + environment: + WEBHOOK_ID: + from_secret: webhook_id + WEBHOOK_TOKEN: + from_secret: webhook_token + volumes: + - name: gopath + path: /go + +- name: codecov + pull: always + image: robertstettner/drone-codecov + settings: + token: + from_secret: codecov_token + +volumes: +- name: gopath + temp: {} + +--- +kind: pipeline +name: linux-amd64 + +platform: + os: linux + arch: amd64 + +steps: +- name: build-push + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/amd64/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + exclude: + - tag + +- name: build-tag + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/amd64/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + - tag + +- name: executable + pull: always + image: golang:1.13 + commands: + - ./release/linux/amd64/drone-packer --help + +- name: dryrun + pull: always + image: plugins/docker:linux-amd64 + settings: + cache_from: appleboy/drone-packer + dockerfile: docker/Dockerfile.linux.amd64 + dry_run: true + repo: appleboy/drone-packer + tags: linux-amd64 + when: + event: + - pull_request + +- name: publish + pull: always + image: plugins/docker:linux-amd64 + settings: + auto_tag: true + auto_tag_suffix: linux-amd64 + cache_from: appleboy/drone-packer + daemon_off: false + dockerfile: docker/Dockerfile.linux.amd64 + password: + from_secret: docker_password + repo: appleboy/drone-packer + username: + from_secret: docker_username + when: + event: + exclude: + - pull_request + +trigger: + ref: + - refs/heads/master + - refs/pull/** + - refs/tags/** + +depends_on: +- testing + +--- +kind: pipeline +name: linux-arm64 + +platform: + os: linux + arch: arm64 + +steps: +- name: build-push + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm64/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + exclude: + - tag + +- name: build-tag + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm64/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + - tag + +- name: executable + pull: always + image: golang:1.13 + commands: + - ./release/linux/arm64/drone-packer --help + +- name: dryrun + pull: always + image: plugins/docker:linux-arm64 + settings: + cache_from: appleboy/drone-packer + dockerfile: docker/Dockerfile.linux.arm64 + dry_run: true + repo: appleboy/drone-packer + tags: linux-arm64 + when: + event: + - pull_request + +- name: publish + pull: always + image: plugins/docker:linux-arm64 + settings: auto_tag: true - dockerfile: Dockerfile - when: - event: [ push, tag ] - local: false - - # publish_linux_arm: - # image: plugins/docker:17.05 - # pull: true - # secrets: [ docker_username, docker_password ] - # group: release - # repo: ${DRONE_REPO} - # auto_tag: true - # auto_tag_suffix: arm - # dockerfile: Dockerfile.arm - # when: - # event: [ push, tag ] - # local: false - - # publish_linux_arm64: - # image: plugins/docker:17.05 - # pull: true - # secrets: [ docker_username, docker_password ] - # group: release - # repo: ${DRONE_REPO} - # auto_tag: true - # auto_tag_suffix: arm64 - # dockerfile: Dockerfile.arm64 - # when: - # event: [ push, tag ] - # local: false - - # publish_linux_i386: - # image: plugins/docker:17.05 - # pull: true - # secrets: [ docker_username, docker_password ] - # group: release - # repo: ${DRONE_REPO} - # auto_tag: true - # auto_tag_suffix: i386 - # dockerfile: Dockerfile.i386 - # when: - # event: [ push, tag ] - # local: false - - discord: - image: appleboy/drone-discord - pull: true - secrets: [ discord_webhook_id, discord_webhook_token ] - when: - status: [ success, failure ] + auto_tag_suffix: linux-arm64 + cache_from: appleboy/drone-packer + daemon_off: false + dockerfile: docker/Dockerfile.linux.arm64 + password: + from_secret: docker_password + repo: appleboy/drone-packer + username: + from_secret: docker_username + when: + event: + exclude: + - pull_request + +trigger: + ref: + - refs/heads/master + - refs/pull/** + - refs/tags/** + +depends_on: +- testing + +--- +kind: pipeline +name: linux-arm + +platform: + os: linux + arch: arm + +steps: +- name: build-push + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + exclude: + - tag + +- name: build-tag + pull: always + image: golang:1.13 + commands: + - go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm/drone-packer + environment: + CGO_ENABLED: 0 + when: + event: + - tag + +- name: executable + pull: always + image: golang:1.13 + commands: + - ./release/linux/arm/drone-packer --help + +- name: dryrun + pull: always + image: plugins/docker:linux-arm + settings: + cache_from: appleboy/drone-packer + dockerfile: docker/Dockerfile.linux.arm + dry_run: true + repo: appleboy/drone-packer + tags: linux-arm + when: + event: + - pull_request + +- name: publish + pull: always + image: plugins/docker:linux-arm + settings: + auto_tag: true + auto_tag_suffix: linux-arm + cache_from: appleboy/drone-packer + daemon_off: false + dockerfile: docker/Dockerfile.linux.arm + password: + from_secret: docker_password + repo: appleboy/drone-packer + username: + from_secret: docker_username + when: + event: + exclude: + - pull_request + +trigger: + ref: + - refs/heads/master + - refs/pull/** + - refs/tags/** + +depends_on: +- testing + +--- +kind: pipeline +name: release-binary + +platform: + os: linux + arch: amd64 + +steps: +- name: build-all-binary + pull: always + image: golang:1.13 + commands: + - make release + when: + event: + - tag + +- name: deploy-all-binary + pull: always + image: plugins/github-release + settings: + api_key: + from_secret: github_release_api_key + files: + - dist/release/* + when: + event: + - tag + +trigger: + ref: + - refs/tags/** + +depends_on: +- testing + +--- +kind: pipeline +name: notifications + +platform: + os: linux + arch: amd64 + +steps: +- name: manifest + pull: always + image: plugins/manifest + settings: + ignore_missing: true + password: + from_secret: docker_password + spec: docker/manifest.tmpl + username: + from_secret: docker_username + +trigger: + ref: + - refs/heads/master + - refs/tags/** + +depends_on: +- linux-amd64 +- linux-arm64 +- linux-arm +- release-binary + +... diff --git a/Dockerfile.i386 b/Dockerfile.i386 deleted file mode 100644 index 239fae2..0000000 --- a/Dockerfile.i386 +++ /dev/null @@ -1,19 +0,0 @@ -FROM plugins/base:multiarch - -LABEL maintainer="Bo-Yi Wu " \ - org.label-schema.name="Drone Packer" \ - org.label-schema.vendor="Bo-Yi Wu" \ - org.label-schema.schema-version="1.0" - -RUN apk add --no-cache ca-certificates \ - wget && \ - rm -rf /var/cache/apk/* - -ENV PACKER_VERSION 1.4.4 -ENV PACKER_ARCH 386 -RUN wget -q https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_${PACKER_ARCH}.zip -O packer.zip && \ - unzip packer.zip -d /bin && \ - rm -f packer.zip - -ADD release/linux/i${PACKER_ARCH}/drone-packer /bin/ -ENTRYPOINT ["/bin/drone-packer"] diff --git a/Dockerfile b/docker/Dockerfile.linux.amd64 similarity index 75% rename from Dockerfile rename to docker/Dockerfile.linux.amd64 index 0ae7b7d..eb6e8c1 100644 --- a/Dockerfile +++ b/docker/Dockerfile.linux.amd64 @@ -1,19 +1,16 @@ -FROM plugins/base:amd64 +FROM plugins/base:linux-amd64 LABEL maintainer="Bo-Yi Wu " \ org.label-schema.name="Drone Packer" \ org.label-schema.vendor="Bo-Yi Wu" \ org.label-schema.schema-version="1.0" -RUN apk add --no-cache ca-certificates \ - wget && \ - rm -rf /var/cache/apk/* - ENV PACKER_VERSION 1.4.4 ENV PACKER_ARCH amd64 RUN wget -q https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_${PACKER_ARCH}.zip -O packer.zip && \ unzip packer.zip -d /bin && \ rm -f packer.zip -ADD release/linux/amd64/drone-packer /bin/ +COPY release/linux/amd64/drone-packer /bin/ + ENTRYPOINT ["/bin/drone-packer"] diff --git a/Dockerfile.arm b/docker/Dockerfile.linux.arm similarity index 73% rename from Dockerfile.arm rename to docker/Dockerfile.linux.arm index 66c3ed3..aa6921f 100644 --- a/Dockerfile.arm +++ b/docker/Dockerfile.linux.arm @@ -1,19 +1,16 @@ -FROM plugins/base:multiarch +FROM plugins/base:linux-arm LABEL maintainer="Bo-Yi Wu " \ org.label-schema.name="Drone Packer" \ org.label-schema.vendor="Bo-Yi Wu" \ org.label-schema.schema-version="1.0" -RUN apk add --no-cache ca-certificates \ - wget && \ - rm -rf /var/cache/apk/* - ENV PACKER_VERSION 1.4.4 ENV PACKER_ARCH arm RUN wget -q https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_${PACKER_ARCH}.zip -O packer.zip && \ unzip packer.zip -d /bin && \ rm -f packer.zip -ADD release/linux/${PACKER_ARCH}/drone-packer /bin/ +COPY release/linux/arm/drone-packer /bin/ + ENTRYPOINT ["/bin/drone-packer"] diff --git a/Dockerfile.arm64 b/docker/Dockerfile.linux.arm64 similarity index 70% rename from Dockerfile.arm64 rename to docker/Dockerfile.linux.arm64 index 8d64a4b..34f88b3 100644 --- a/Dockerfile.arm64 +++ b/docker/Dockerfile.linux.arm64 @@ -1,19 +1,16 @@ -FROM plugins/base:multiarch +FROM plugins/base:linux-arm64 LABEL maintainer="Bo-Yi Wu " \ org.label-schema.name="Drone Packer" \ org.label-schema.vendor="Bo-Yi Wu" \ org.label-schema.schema-version="1.0" -RUN apk add --no-cache ca-certificates \ - wget && \ - rm -rf /var/cache/apk/* - ENV PACKER_VERSION 1.4.4 ENV PACKER_ARCH arm64 RUN wget -q https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_${PACKER_ARCH}.zip -O packer.zip && \ unzip packer.zip -d /bin && \ - rm -f packer.zip + rm -f packer.zi + +COPY release/linux/arm64/drone-packer /bin/ -ADD release/linux/${PACKER_ARCH}/drone-packer /bin/ ENTRYPOINT ["/bin/drone-packer"] diff --git a/docker/manifest.tmpl b/docker/manifest.tmpl new file mode 100644 index 0000000..e99cb17 --- /dev/null +++ b/docker/manifest.tmpl @@ -0,0 +1,25 @@ +image: appleboy/drone-packer:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} +{{#if build.tags}} +tags: +{{#each build.tags}} + - {{this}} +{{/each}} +{{/if}} +manifests: + - + image: appleboy/drone-packer:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + platform: + architecture: amd64 + os: linux + - + image: appleboy/drone-packer:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 + platform: + architecture: arm64 + os: linux + variant: v8 + - + image: appleboy/drone-packer:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + platform: + architecture: arm + os: linux + variant: v7 diff --git a/pipeline.libsonnet b/pipeline.libsonnet new file mode 100644 index 0000000..3b4eb30 --- /dev/null +++ b/pipeline.libsonnet @@ -0,0 +1,258 @@ +{ + test:: { + kind: 'pipeline', + name: 'testing', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'vet', + image: 'golang:1.13', + pull: 'always', + commands: [ + 'make vet', + ], + volumes: [ + { + name: 'gopath', + path: '/go', + }, + ], + }, + { + name: 'lint', + image: 'golang:1.13', + pull: 'always', + commands: [ + 'make lint', + ], + volumes: [ + { + name: 'gopath', + path: '/go', + }, + ], + }, + { + name: 'misspell', + image: 'golang:1.13', + pull: 'always', + commands: [ + 'make misspell-check', + ], + volumes: [ + { + name: 'gopath', + path: '/go', + }, + ], + }, + { + name: 'test', + image: 'golang:1.13', + pull: 'always', + environment: { + WEBHOOK_ID: { 'from_secret': 'webhook_id' }, + WEBHOOK_TOKEN: { 'from_secret': 'webhook_token' }, + }, + commands: [ + 'make test', + 'make coverage', + ], + volumes: [ + { + name: 'gopath', + path: '/go', + }, + ], + }, + { + name: 'codecov', + image: 'robertstettner/drone-codecov', + pull: 'always', + settings: { + token: { 'from_secret': 'codecov_token' }, + }, + }, + ], + volumes: [ + { + name: 'gopath', + temp: {}, + }, + ], + }, + + build(name, os='linux', arch='amd64'):: { + kind: 'pipeline', + name: os + '-' + arch, + platform: { + os: os, + arch: arch, + }, + steps: [ + { + name: 'build-push', + image: 'golang:1.13', + pull: 'always', + environment: { + CGO_ENABLED: '0', + }, + commands: [ + 'go build -v -ldflags \'-X main.build=${DRONE_BUILD_NUMBER}\' -a -o release/' + os + '/' + arch + '/' + name, + ], + when: { + event: { + exclude: [ 'tag' ], + }, + }, + }, + { + name: 'build-tag', + image: 'golang:1.13', + pull: 'always', + environment: { + CGO_ENABLED: '0', + }, + commands: [ + 'go build -v -ldflags \'-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}\' -a -o release/' + os + '/' + arch + '/' + name, + ], + when: { + event: [ 'tag' ], + }, + }, + { + name: 'executable', + image: 'golang:1.13', + pull: 'always', + commands: [ + './release/' + os + '/' + arch + '/' + name + ' --help', + ], + }, + { + name: 'dryrun', + image: 'plugins/docker:' + os + '-' + arch, + pull: 'always', + settings: { + daemon_off: false, + dry_run: true, + tags: os + '-' + arch, + dockerfile: 'docker/Dockerfile.' + os + '.' + arch, + repo: 'appleboy/' + name, + cache_from: 'appleboy/' + name, + }, + when: { + event: [ 'pull_request' ], + }, + }, + { + name: 'publish', + image: 'plugins/docker:' + os + '-' + arch, + pull: 'always', + settings: { + daemon_off: 'false', + auto_tag: true, + auto_tag_suffix: os + '-' + arch, + dockerfile: 'docker/Dockerfile.' + os + '.' + arch, + repo: 'appleboy/' + name, + cache_from: 'appleboy/' + name, + username: { 'from_secret': 'docker_username' }, + password: { 'from_secret': 'docker_password' }, + }, + when: { + event: { + exclude: [ 'pull_request' ], + }, + }, + }, + ], + depends_on: [ + 'testing', + ], + trigger: { + ref: [ + 'refs/heads/master', + 'refs/pull/**', + 'refs/tags/**', + ], + }, + }, + + release:: { + kind: 'pipeline', + name: 'release-binary', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'build-all-binary', + image: 'golang:1.13', + pull: 'always', + commands: [ + 'make release' + ], + when: { + event: [ 'tag' ], + }, + }, + { + name: 'deploy-all-binary', + image: 'plugins/github-release', + pull: 'always', + settings: { + files: [ 'dist/release/*' ], + api_key: { 'from_secret': 'github_release_api_key' }, + }, + when: { + event: [ 'tag' ], + }, + }, + ], + depends_on: [ + 'testing', + ], + trigger: { + ref: [ + 'refs/tags/**', + ], + }, + }, + + notifications(os='linux', arch='amd64', depends_on=[]):: { + kind: 'pipeline', + name: 'notifications', + platform: { + os: os, + arch: arch, + }, + steps: [ + { + name: 'manifest', + image: 'plugins/manifest', + pull: 'always', + settings: { + username: { from_secret: 'docker_username' }, + password: { from_secret: 'docker_password' }, + spec: 'docker/manifest.tmpl', + ignore_missing: true, + }, + }, + ], + depends_on: depends_on, + trigger: { + ref: [ + 'refs/heads/master', + 'refs/tags/**', + ], + }, + }, + + signature(key):: { + kind: 'signature', + hmac: key, + } +}