Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Makefile and Dockerfile #47

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ARG ALPINE_VERSION=3.8
ARG GO_VERSION=1.11.4

# build image
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as build
# base image
FROM golang:${GO_VERSION} as build

ARG DOCKERCLI_VERSION=18.03.1-ce
ARG DOCKERCLI_CHANNEL=edge
Expand All @@ -12,35 +12,34 @@ ARG BUILDTIME
ARG COMMIT
ARG TAG

RUN apk add --no-cache \
RUN apt-get update && apt-get install -y \
bash \
make \
git \
curl \
util-linux \
coreutils \
build-base
build-essential

# Fetch docker cli to run a registry container for e2e tests
RUN curl -Ls https://download.docker.com/linux/static/$DOCKERCLI_CHANNEL/x86_64/docker-$DOCKERCLI_VERSION.tgz | tar -xz

# Fetch docker-app to build a CNAB from an application template
RUN curl -Ls https://github.com/docker/app/releases/download/$DOCKER_APP_VERSION/docker-app-linux.tar.gz | tar -xz
RUN git clone https://github.com/docker/app
RUN curl -Ls https://github.com/docker/app/releases/download/$DOCKER_APP_VERSION/docker-app-linux.tar.gz | tar -xz && cp /go/docker-app-linux /usr/bin/docker-app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should bump the version we use to the lastest release of Docker App. I see above that it's the cnab-preview one

RUN git clone https://github.com/docker/app && cp -r /go/app/examples /examples

WORKDIR /go/src/github.com/docker/cnab-to-oci
COPY . .
RUN make BUILDTIME=$BUILDTIME COMMIT=$COMMIT TAG=$TAG bin/cnab-to-oci &&\
make BUILDTIME=$BUILDTIME COMMIT=$COMMIT TAG=$TAG build-e2e-test

# e2e image
RUN make BUILDTIME=$BUILDTIME COMMIT=$COMMIT TAG=$TAG cross build-e2e-test

FROM alpine:${ALPINE_VERSION} as e2e

# copy all the elements needed for e2e tests from build image
COPY --from=build /go/docker/docker /usr/bin/docker
COPY --from=build /go/docker-app-linux /usr/bin/docker-app
COPY --from=build /go/app/examples /examples
COPY --from=build /go/src/github.com/docker/cnab-to-oci/bin/cnab-to-oci /usr/bin/cnab-to-oci
COPY --from=build /go/src/github.com/docker/cnab-to-oci/bin/cnab-to-oci-linux /usr/bin/cnab-to-oci
COPY --from=build /go/src/github.com/docker/cnab-to-oci/e2e /e2e
COPY --from=build /go/src/github.com/docker/cnab-to-oci/e2e.test /e2e/e2e.test

Expand Down
83 changes: 83 additions & 0 deletions Jenkinsfile.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
properties([buildDiscarder(logRotator(numToKeepStr: '20'))])

pipeline {
agent {
label 'ubuntu-1804'
}

options {
skipDefaultCheckout(true)
}

environment {
TAG = tag()
BUILD_TAG = tag()
}

stages {
stage('Build') {
parallel {
stage("Validate") {
agent {
label 'ubuntu-1804'
}
steps {
dir('src/github.com/docker/cnab-to-oci') {
checkout scm
ansiColor('xterm') {
sh 'make -f docker.Makefile lint'
}
}
}
post {
always {
sh 'make -f docker.Makefile clean-images'
deleteDir()
jcsirot marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
stage("Binaries") {
agent {
label 'ubuntu-1804'
}
steps {
dir('src/github.com/docker/cnab-to-oci') {
checkout scm
ansiColor('xterm') {
sh 'make -f docker.Makefile build test-unit'
}
dir('bin') {
stash name: 'binaries'
}
}
}
post {
always {
sh 'make -f docker.Makefile clean-images'
deleteDir()
}
}
}
}
}
stage('Test') {
agent {
label 'ubuntu-1804'
}
steps {
dir('src/github.com/docker/cnab-to-oci') {
checkout scm
ansiColor('xterm') {
sh 'make -f docker.Makefile test-e2e'
}
}
}
post {
always {
sh 'make -f docker.Makefile clean-images'
deleteDir()
}
}
}
}
}
47 changes: 9 additions & 38 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
include vars.mk

.DEFAULT_GOAL := all
SHELL:=/bin/bash

PKG_NAME := github.com/docker/cnab-to-oci

EXEC_EXT :=
ifeq ($(OS),Windows_NT)
EXEC_EXT := .exe
endif

ifeq ($(TAG),)
TAG := $(shell git describe --always --dirty 2> /dev/null)
endif
ifeq ($(COMMIT),)
COMMIT := $(shell git rev-parse --short HEAD 2> /dev/null)
endif

ifeq ($(BUILDTIME),)
BUILDTIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" 2> /dev/null)
endif
ifeq ($(BUILDTIME),)
BUILDTIME := unknown
$(warning unable to set BUILDTIME. Set the value manually)
endif

LDFLAGS := "-s -w \
-X $(PKG_NAME)/internal.GitCommit=$(COMMIT) \
-X $(PKG_NAME)/internal.Version=$(TAG) \
-X $(PKG_NAME)/internal.BuildTime=$(BUILDTIME)"

BUILD_ARGS := \
--build-arg BUILDTIME=$(BUILDTIME) \
--build-arg COMMIT=$(COMMIT) \
--build-arg TAG=$(TAG)

GO_BUILD := CGO_ENABLED=0 go build -ldflags=$(LDFLAGS)
GO_TEST := CGO_ENABLED=0 go test -ldflags=$(LDFLAGS) -failfast
GO_TEST_RACE := go test -ldflags=$(LDFLAGS) -failfast -race

all: build test

all-ci: lint all
Expand All @@ -51,11 +17,16 @@ get-tools:
gometalinter --install

# Default build
build: bin/cnab-to-oci
build: bin/$(BIN_NAME)

cross: bin/$(BIN_NAME)-linux bin/$(BIN_NAME)-darwin bin/$(BIN_NAME)-windows.exe

bin/%: cmd/% check_go_env
bin/$(BIN_NAME): cmd/$(BIN_NAME) check_go_env
$(GO_BUILD) -o $@$(EXEC_EXT) ./$<

bin/$(BIN_NAME)-%.exe bin/$(BIN_NAME)-%: cmd/$(BIN_NAME) check_go_env
GOOS=$* $(GO_BUILD) -o $@ ./$<

install:
pushd cmd/cnab-to-oci && go install && popd

Expand Down
52 changes: 52 additions & 0 deletions docker.Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
include vars.mk

LINT_IMAGE_NAME := $(BIN_NAME)-lint:$(TAG)
DEV_IMAGE_NAME := $(BIN_NAME)-dev:$(TAG)
E2E_IMAGE_NAME := $(BIN_NAME)-e2e:$(TAG)

BIN_CTNR_NAME := $(BIN_NAME)-bin-$(TAG)

.DEFAULT: all
all: build test

create_bin:
@$(call mkdir,bin)

build_dev_image:
docker build $(BUILD_ARGS) --target=build -t $(DEV_IMAGE_NAME) .

build_e2e_image:
docker build $(BUILD_ARGS) --target=e2e -t $(E2E_IMAGE_NAME) .

build: create_bin build_dev_image
docker create --name $(BIN_CTNR_NAME) $(DEV_IMAGE_NAME) noop
docker cp $(BIN_CTNR_NAME):$(PKG_PATH)/bin/$(BIN_NAME)-linux bin/$(BIN_NAME)-linux
docker cp $(BIN_CTNR_NAME):$(PKG_PATH)/bin/$(BIN_NAME)-darwin bin/$(BIN_NAME)-darwin
docker cp $(BIN_CTNR_NAME):$(PKG_PATH)/bin/$(BIN_NAME)-windows.exe bin/$(BIN_NAME)-windows.exe
docker rm $(BIN_CTNR_NAME)
@$(call chmod,+x,bin/$(BIN_NAME)-linux)
@$(call chmod,+x,bin/$(BIN_NAME)-darwin)
@$(call chmod,+x,bin/$(BIN_NAME)-windows.exe)

shell: build_dev_image ## run a shell in the docker build image
docker run -ti --rm $(DEV_IMAGE_NAME) bash

test: test-unit test-e2e ## run all tests

test-unit: build_dev_image ## run unit tests
docker run --rm $(DEV_IMAGE_NAME) make test-unit

test-e2e: build_e2e_image ## run e2e tests
docker run --rm -v /var/run:/var/run:ro --network="host" $(E2E_IMAGE_NAME)

lint: ## run linter(s)
$(info Linting...)
docker build -t $(LINT_IMAGE_NAME) -f lint.Dockerfile .
docker run --rm $(LINT_IMAGE_NAME) gometalinter --config=gometalinter.json ./...

clean-images: ## Delete images
docker image rm -f $(DEV_IMAGE_NAME)
docker image rm -f $(E2E_IMAGE_NAME)
docker image rm -f $(LINT_IMAGE_NAME)

.PHONY: lint test-e2e test-unit test shell build gradle-test shell build_e2e_image build_dev_image create_bin
17 changes: 17 additions & 0 deletions lint.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ARG ALPINE_VERSION=3.8
ARG GO_VERSION=1.11.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These versions are quite old, would be good to bump them


# base image
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as lint

RUN apk add --no-cache \
curl \
git \
make \
coreutils
RUN go get github.com/alecthomas/gometalinter && gometalinter --install

WORKDIR /go/src/github.com/docker/cnab-to-oci
ENV CGO_ENABLED=0

COPY . .
64 changes: 64 additions & 0 deletions vars.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
PKG_NAME := github.com/docker/cnab-to-oci
PKG_PATH := /go/src/$(PKG_NAME)
BIN_NAME ?= cnab-to-oci
E2E_NAME := $(BIN_NAME)-e2e

EXEC_EXT :=
ifeq ($(OS),Windows_NT)
EXEC_EXT := .exe
endif

# Failing to resolve sh.exe to a full path denotes a windows vanilla shell.
# Although 'simple' commands are still exec'ed, 'complex' ones are batch'ed instead of sh'ed.
ifeq ($(SHELL),sh.exe)
mkdir = mkdir $(subst /,\,$(1)) > nul 2>&1 || (exit 0)
rm = del /F /Q $(subst /,\,$(1)) > nul 2>&1 || (exit 0)
rmdir = rmdir /S /Q $(subst /,\,$(1)) > nul 2>&1 || (exit 0)
chmod =
BUILDTIME ?= unknown
NULL := nul
else
# The no-op redirection forces make to shell out the commands instead of spawning a process as
# the latter can fail on windows running cmd or powershell while having a unix style shell in the path.
mkdir = mkdir -p $(1) 1>&1
rm = rm -rf $(1) 1>&1
rmdir = rm -rf $(1) 1>&1
chmod = chmod $(1) $(2) 1>&1
NULL := /dev/null
endif

ifeq ($(BUILD_TAG),)
BUILD_TAG := $(shell git describe --always --dirty --abbrev=10 2> $(NULL))
endif
ifeq ($(TAG),)
ifeq ($(TAG_NAME),)
TAG := $(BUILD_TAG)
else
TAG := $(TAG_NAME)
endif
endif
ifeq ($(COMMIT),)
COMMIT := $(shell git rev-parse --short HEAD 2> $(NULL))
endif

ifeq ($(BUILDTIME),)
BUILDTIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" 2> $(NULL))
endif
ifeq ($(BUILDTIME),)
BUILDTIME := unknown
$(warning unable to set BUILDTIME. Set the value manually)
endif

LDFLAGS := "-s -w \
-X $(PKG_NAME)/internal.GitCommit=$(COMMIT) \
-X $(PKG_NAME)/internal.Version=$(TAG) \
-X $(PKG_NAME)/internal.BuildTime=$(BUILDTIME)"

BUILD_ARGS := \
--build-arg BUILDTIME=$(BUILDTIME) \
--build-arg COMMIT=$(COMMIT) \
--build-arg TAG=$(TAG)

GO_BUILD := CGO_ENABLED=0 go build -ldflags=$(LDFLAGS)
GO_TEST := CGO_ENABLED=0 go test -ldflags=$(LDFLAGS) -failfast
GO_TEST_RACE := go test -ldflags=$(LDFLAGS) -failfast -race