Skip to content

Commit

Permalink
Improve Release pipeline (#8)
Browse files Browse the repository at this point in the history
* Fix minor drone issues

* Try building images from  scratch

* Ensure /tmp is creted

* Add compression to scratch

* Try goreleaser docker

* Add more configuration for goreleaser

* Disable drone docker tagging for now
  • Loading branch information
kakkoyun authored Feb 15, 2019
1 parent 165e528 commit 4d310ed
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 28 deletions.
24 changes: 14 additions & 10 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ branches:
- master

pipeline:
clone:
image: plugins/git
tags: true

restore-cache:
image: meltwater/drone-cache
pull: true
Expand Down Expand Up @@ -103,15 +107,15 @@ pipeline:
branch: [master]
event: [push]

docker-release-tag:
image: plugins/docker
repo: meltwater/drone-cache
tags: '${DRONE_TAG}'
secrets: [docker_username, docker_password]
when:
event: tag
status: success
ref: refs/tags/*
# docker-release-tag:
# image: plugins/docker
# repo: meltwater/drone-cache
# tags: '${DRONE_TAG}'
# secrets: [docker_username, docker_password]
# when:
# event: tag
# status: success
# ref: refs/tags/*

release:
image: golang:1.11 # golang:1.11-alpine
Expand All @@ -137,7 +141,7 @@ pipeline:
> {{build.message}} by <https://github.com/{{build.author}}|{{build.author}}>
when:
local: false
status: [changed, failure] # success
status: [success, failure]

services:
filestorage:
Expand Down
53 changes: 50 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ builds:
- darwin
- linux
- windows
- freebsd
goarch:
- amd64
- 386
- amd64
- arm
- arm64
goarm:
- 5
- 6
- 7
ldflags: >
-s
-w
Expand All @@ -39,13 +44,55 @@ changelog:
sort: asc
filters:
exclude:
- .*
- typo
- '^docs:'
- '^test:'
- Merge pull request
- Merge branch
release:
github:
owner: meltwater
name: drone-cache
prerelease: auto # in case there is an indicator for this in the tag e.g. v1.0.0-rc1
disable: false
dockers:
# - &docker
- image_templates:
- "meltwater/drone-cache:latest"
- "meltwater/drone-cache:{{ .Tag }}"
- "meltwater/drone-cache:{{ .Major }}.{{ .Minor }}"
- "meltwater/drone-cache:{{ .Major }}.{{ .Minor }}.{{ .Patch }}"
skip_push: false
binaries:
- drone-cache
dockerfile: Dockerfile.goreleaser
build_flag_templates:
- "--label=org.label-schema.schema-version=1.0"
- "--label=org.label-schema.version={{.Version}}"
- "--label=org.label-schema.name={{.ProjectName}}"
- "--label=repository=http://github.com/meltwater/drone-cache"
- "--label=homepage=http://github.com/meltwater"
- "--label=maintainer=Kemal Akkoyun <kakkoyun@gmail.com>"
extra_files:
- scripts/entrypoint.sh
# - <<: *docker
# goos: linux
# goarch: 386
# - <<: *docker
# goos: linux
# goarch: amd64
# - <<: *docker
# goos: linux
# goarch: arm
# goarm: 5
# - <<: *docker
# goos: linux
# goarch: arm
# goarm: 6
# - <<: *docker
# goos: linux
# goarch: arm
# goarm: 7
# - <<: *docker
# goos: linux
# goarch: arm64
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build stage
FROM golang:1.11.5-alpine AS builder
FROM golang:1.11-alpine AS builder
RUN apk add --update make git upx

ENV BUILD_DIR /build
Expand All @@ -24,6 +24,7 @@ RUN set -ex \
&& rm -rf /var/cache/apk/*

COPY scripts/entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/bin/drone-cache"]
21 changes: 21 additions & 0 deletions Dockerfile.goreleaser
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# build stage
FROM golang:1.11-alpine AS builder
RUN apk add --update upx ca-certificates tzdata && update-ca-certificates

RUN adduser -D -g '' appuser

COPY drone-cache /bin/
WORKDIR /bin
RUN upx drone-cache

# final stage
FROM scratch

COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /bin/drone-cache /bin/

USER appuser

ENTRYPOINT ["/bin/drone-cache"]
30 changes: 30 additions & 0 deletions Dockerfile.scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# build stage
FROM golang:1.11-alpine AS builder
RUN apk add --update make git upx ca-certificates tzdata \
&& update-ca-certificates

RUN adduser -D -g '' appuser

ENV BUILD_DIR /build

COPY go.* Makefile $BUILD_DIR/
WORKDIR $BUILD_DIR
RUN make fetch-dependencies

COPY . $BUILD_DIR

RUN make drone-cache
RUN make compress
RUN cp drone-cache /bin

# final stage
FROM scratch

COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /bin/drone-cache /bin/

USER appuser

ENTRYPOINT ["/bin/drone-cache"]
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ fetch-dependencies:
.PHONY: fetch-dependencies

compress: drone-cache
upx --brute drone-cache
upx drone-cache

.PHONY: compress

docker-build: Dockerfile
docker build -t meltwater/drone-cache:latest .

docker-build-scratch: Dockerfile.scratch
docker build -f Dockerfile.scratch -t meltwater/drone-cache:latest .

docker-push: docker-build
docker push meltwater/drone-cache:latest

Expand Down
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
- [x] Rename Repo
- [x] TEST!
- [x] MERGE !
- [X] Docker from scratch

- [ ] Docker from scratch
- [ ] Gorelease Docker multiple arc
- [ ] Improve Makefile

- [ ] Improve Makefile
- [ ] Improve static analyzers

## Before v1.0.0
Expand All @@ -48,11 +48,11 @@
- [ ] http://plugins.drone.io/drillster/drone-volume-cache/
- [ ] https://github.com/Drillster/drone-volume-cache/blob/master/cacher.sh
- [ ] Introduce mode: wrap_in_directory: true/false
- [ ] Add unit tests
- [ ] Checkout
- [ ] https://github.com/drone/drone-go
- [ ] https://github.com/drone-plugins/drone-s3
- [ ] New Drone Version
- [ ] Add unit tests

## Road to Open Source

Expand Down
19 changes: 10 additions & 9 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (c Cache) Upload(src, dst string) error {
log.Printf("archiving directory <%s>", src)

// 2. create a temporary file for the archive
ensureDir("/tmp")
dir, err := ioutil.TempDir("", "")
if err != nil {
return errors.Wrap(err, "could not create tmp folder for archive")
Expand Down Expand Up @@ -163,15 +164,6 @@ func archiveReader(r io.Reader, archiveFmt string) *tar.Reader {
}

func extractFilesFromArchive(tr *tar.Reader, dst string) error {
ensureDir := func(dirName string) error {
if _, err := os.Stat(dirName); err != nil {
if err := os.MkdirAll(dirName, os.FileMode(0755)); err != nil {
return errors.Wrap(err, fmt.Sprintf("could not create directory <%s>", dirName))
}
}
return nil
}

for {
h, err := tr.Next()
switch {
Expand Down Expand Up @@ -207,3 +199,12 @@ func extractFilesFromArchive(tr *tar.Reader, dst string) error {
f.Close()
}
}

func ensureDir(dirName string) error {
if _, err := os.Stat(dirName); err != nil {
if err := os.MkdirAll(dirName, os.FileMode(0755)); err != nil {
return errors.Wrap(err, fmt.Sprintf("could not create directory <%s>", dirName))
}
}
return nil
}

0 comments on commit 4d310ed

Please sign in to comment.