Skip to content

Commit

Permalink
feature: Truncate branch names to avoid breaking k8s deployments (#67)
Browse files Browse the repository at this point in the history
* feature: Truncate branch names to avoid breaking k8s deployments

* fix: Update install crystal in CI

* fix: Update to newer crystal

* fix: Update build and Dockerfile, use static

* Fix Makefile

* Some cleanups

* Fix version

* Simplify docker push step

* Avoid sudo to run make

* Fix $(VERSION)

* Add again manual_approval and fix buildStatic
  • Loading branch information
lolgab authored Nov 21, 2022
1 parent 92c7bec commit 8ae1542
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 63 deletions.
43 changes: 9 additions & 34 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
version: 2.1

orbs:
codacy: codacy/base@2.5.0
codacy: codacy/base@9.3.1

# Re-usable blocks to reduce boilerplate in job definitions.
references:
default_machine_job: &default_machine_job
machine: true
working_directory: ~/workdir

set_version_env_variable: &set_version_env_variable
run:
name: Define Environment Variable VERSION and ENV at Runtime
command: |
echo export VERSION=$(cat .version) >> $BASH_ENV
source $BASH_ENV
echo $VERSION
jobs:
compile:
<<: *default_machine_job
Expand All @@ -26,12 +18,12 @@ jobs:
- run:
name: Get version
command: |
curl -fsSL https://crystal-lang.org/install.sh | sudo bash
sudo apt-get update
sudo apt-get install libevent-core-2.0-5 libevent-dev libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5 libgmp-dev libgmpxx4ldbl libssl-dev libxml2-dev libyaml-dev libreadline-dev automake libtool git llvm libpcre3-dev build-essential -y
wget https://github.com/crystal-lang/crystal/releases/download/0.30.1/crystal_0.30.1-1_amd64.deb
sudo dpkg -i crystal_0.30.1-1_amd64.deb
sudo make clean test build
sudo apt-get install crystal
make clean test build
./bin/git-version > .version
cat .version
- persist_to_workspace:
root: ~/workdir
paths:
Expand All @@ -44,28 +36,20 @@ jobs:
steps:
- attach_workspace:
at: ~/workdir
- *set_version_env_variable
- run:
name: Build Docker image
command: make docker_build
- run:
name: Save docker to file
command: |
docker save --output docker-image.tar codacy/git-version:$VERSION
- persist_to_workspace:
root: ~/workdir
paths:
- docker-image.tar

build_static:
<<: *default_machine_job
steps:
- attach_workspace:
at: ~/workdir
- *set_version_env_variable
- run:
name: Compile
command: make clean buildStatic
name: Build Docker and extract from image
command: |
make docker_build
docker run --entrypoint cat codacy/git-version:$(cat .version) /bin/git-version > bin/git-version
- persist_to_workspace:
root: ~/workdir
paths:
Expand All @@ -75,14 +59,9 @@ jobs:

publish_versioned:
<<: *default_machine_job
environment:
PROJECT_NAME: $CIRCLE_PROJECT_REPONAME
steps:
- attach_workspace:
at: ~/workdir
- run:
name: Load docker from file
command: docker load --input docker-image.tar
- run:
name: Publish images to Docker Hub
command: make push-docker-image
Expand All @@ -92,10 +71,6 @@ jobs:
steps:
- attach_workspace:
at: ~/workdir
- *set_version_env_variable
- run:
name: Load docker from file
command: docker load --input docker-image.tar
- run:
name: Publish latest image to Docker Hub
command: make push-latest-docker-image
Expand Down
20 changes: 6 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
FROM codacy/ci-base:2.0.0 AS builder
FROM crystallang/crystal:1.6-alpine AS builder

RUN apk add --update --no-cache --force-overwrite \
openssl openssl-dev crystal shards g++ gc-dev \
libc-dev libevent-dev libxml2-dev llvm llvm-dev \
llvm-static make pcre-dev readline-dev \
yaml-dev zlib-dev git
RUN apk add --update --no-cache --force-overwrite git

RUN git config --global user.email "[email protected]" && git config --global user.name "Codacy"
RUN git config --global user.email "[email protected]" && git config --global user.name "Codacy"

RUN mkdir -p /workspace

WORKDIR /workspace
COPY ./ /workspace

ENV ALPINE=1
RUN make test buildStatic

RUN make test build



FROM codacy/ci-base:2.0.0
FROM alpine:3.15

LABEL maintainer="[email protected]"

RUN apk add --update --no-cache --force-overwrite gc-dev pcre-dev libevent-dev git
RUN apk add --update --no-cache git

COPY --from=builder /workspace/bin/git-version /bin

Expand Down
16 changes: 7 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CRYSTAL?=$(shell which crystal)
CRYSTAL_FLAGS=--release
CRYSTAL_STATIC_FLAGS=--static --no-debug
CRYSTAL_STATIC_FLAGS=--static

VERSION?=$(shell ./bin/git-version)
VERSION?=$(shell cat .version)

all: fmt test build docker_build ## clean and produce target binary and docker image

Expand All @@ -21,13 +21,10 @@ build: ## compiles from crystal sources
.PHONY: buildStatic
buildStatic: ## compiles from crystal sources into static binary
mkdir -p bin
docker run --rm -it -v $(PWD):/app -w /app durosoft/crystal-alpine:latest crystal build $(CRYSTAL_FLAGS) $(CRYSTAL_STATIC_FLAGS) src/main.cr -o bin/git-version

.PHONY: docker
docker: build docker_build ## compiles from sources and produce the docker image
crystal build $(CRYSTAL_FLAGS) $(CRYSTAL_STATIC_FLAGS) src/main.cr -o bin/git-version

docker_build: ## build the docker image
docker build -t codacy/git-version:${VERSION} .
docker build -t codacy/git-version:$(VERSION) .

.PHONY: clean
clean: ## clean target directories
Expand All @@ -36,12 +33,13 @@ clean: ## clean target directories
.PHONY: push-docker-image
push-docker-image: ## push the docker image to the registry (DOCKER_USER and DOCKER_PASS mandatory)
@docker login -u $(DOCKER_USER) -p $(DOCKER_PASS) &&\
docker push codacy/git-version:${VERSION}
docker build -t codacy/git-version:$(VERSION) . &&\
docker push codacy/git-version:$(VERSION)

.PHONY: push-latest-docker-image
push-latest-docker-image: ## push the docker image with the "latest" tag to the registry (DOCKER_USER and DOCKER_PASS mandatory)
@docker login -u $(DOCKER_USER) -p $(DOCKER_PASS) &&\
docker tag codacy/git-version:${VERSION} codacy/git-version:latest &&\
docker build -t codacy/git-version:latest . &&\
docker push codacy/git-version:latest

.PHONY: help
Expand Down
18 changes: 18 additions & 0 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,24 @@ describe GitVersion do
tmp.cleanup
end
end
it "should truncate long branch names in tags" do
tmp = InTmp.new

begin
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir)

tmp.exec %(git init)
tmp.exec %(git checkout -b very-very-very-very-long-branch-name-that-excedes-k8s-limits)
tmp.exec %(git commit -m "commit" --allow-empty)
tmp.exec %(git tag "100.100.100")

version = git.get_new_version
hash = git.current_commit_hash
version.should eq("100.100.101-veryveryveryverylongbranchname.0.#{hash}")
ensure
tmp.cleanup
end
end
end

it "get previous version - first commit" do
Expand Down
5 changes: 1 addition & 4 deletions spec/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ module Utils
extend self

class InTmp
# on Alpine last available crystal version is 0.26, this brings compatibility with 0.27
TMP_DIR = {% if env("ALPINE") %} "/tmp" {% else %} Dir.tempdir {% end %}

def initialize
folder = UUID.random.to_s

puts "folder #{folder}"

@tmpdir = File.expand_path(folder, TMP_DIR)
@tmpdir = File.expand_path(folder, Dir.tempdir)

FileUtils.rm_rf(@tmpdir)
FileUtils.mkdir(@tmpdir)
Expand Down
2 changes: 1 addition & 1 deletion src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ module GitVersion
nil
)
else
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
Expand Down
2 changes: 1 addition & 1 deletion src/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ log_paths = ""

folder = FileUtils.pwd

OptionParser.parse! do |parser|
OptionParser.parse do |parser|
parser.banner = "Usage: git-version [arguments]"
parser.on("-f FOLDER", "--folder=FOLDER", "Execute the command in the defined folder") { |f| folder = f }
parser.on("-b BRANCH", "--dev-branch=BRANCH", "Specifies the development branch") { |branch| dev_branch = branch }
Expand Down

0 comments on commit 8ae1542

Please sign in to comment.