Skip to content

Commit 8ae1542

Browse files
authored
feature: Truncate branch names to avoid breaking k8s deployments (#67)
* 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
1 parent 92c7bec commit 8ae1542

7 files changed

Lines changed: 43 additions & 63 deletions

File tree

.circleci/config.yml

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
version: 2.1
22

33
orbs:
4-
codacy: codacy/base@2.5.0
4+
codacy: codacy/base@9.3.1
55

66
# Re-usable blocks to reduce boilerplate in job definitions.
77
references:
88
default_machine_job: &default_machine_job
99
machine: true
1010
working_directory: ~/workdir
1111

12-
set_version_env_variable: &set_version_env_variable
13-
run:
14-
name: Define Environment Variable VERSION and ENV at Runtime
15-
command: |
16-
echo export VERSION=$(cat .version) >> $BASH_ENV
17-
source $BASH_ENV
18-
echo $VERSION
19-
2012
jobs:
2113
compile:
2214
<<: *default_machine_job
@@ -26,12 +18,12 @@ jobs:
2618
- run:
2719
name: Get version
2820
command: |
21+
curl -fsSL https://crystal-lang.org/install.sh | sudo bash
2922
sudo apt-get update
30-
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
31-
wget https://github.com/crystal-lang/crystal/releases/download/0.30.1/crystal_0.30.1-1_amd64.deb
32-
sudo dpkg -i crystal_0.30.1-1_amd64.deb
33-
sudo make clean test build
23+
sudo apt-get install crystal
24+
make clean test build
3425
./bin/git-version > .version
26+
cat .version
3527
- persist_to_workspace:
3628
root: ~/workdir
3729
paths:
@@ -44,28 +36,20 @@ jobs:
4436
steps:
4537
- attach_workspace:
4638
at: ~/workdir
47-
- *set_version_env_variable
4839
- run:
4940
name: Build Docker image
5041
command: make docker_build
51-
- run:
52-
name: Save docker to file
53-
command: |
54-
docker save --output docker-image.tar codacy/git-version:$VERSION
55-
- persist_to_workspace:
56-
root: ~/workdir
57-
paths:
58-
- docker-image.tar
5942

6043
build_static:
6144
<<: *default_machine_job
6245
steps:
6346
- attach_workspace:
6447
at: ~/workdir
65-
- *set_version_env_variable
6648
- run:
67-
name: Compile
68-
command: make clean buildStatic
49+
name: Build Docker and extract from image
50+
command: |
51+
make docker_build
52+
docker run --entrypoint cat codacy/git-version:$(cat .version) /bin/git-version > bin/git-version
6953
- persist_to_workspace:
7054
root: ~/workdir
7155
paths:
@@ -75,14 +59,9 @@ jobs:
7559

7660
publish_versioned:
7761
<<: *default_machine_job
78-
environment:
79-
PROJECT_NAME: $CIRCLE_PROJECT_REPONAME
8062
steps:
8163
- attach_workspace:
8264
at: ~/workdir
83-
- run:
84-
name: Load docker from file
85-
command: docker load --input docker-image.tar
8665
- run:
8766
name: Publish images to Docker Hub
8867
command: make push-docker-image
@@ -92,10 +71,6 @@ jobs:
9271
steps:
9372
- attach_workspace:
9473
at: ~/workdir
95-
- *set_version_env_variable
96-
- run:
97-
name: Load docker from file
98-
command: docker load --input docker-image.tar
9974
- run:
10075
name: Publish latest image to Docker Hub
10176
command: make push-latest-docker-image

Dockerfile

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
FROM codacy/ci-base:2.0.0 AS builder
1+
FROM crystallang/crystal:1.6-alpine AS builder
22

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

9-
RUN git config --global user.email "team@codacy.com" && git config --global user.name "Codacy"
5+
RUN git config --global user.email "team@codacy.com" && git config --global user.name "Codacy"
106

117
RUN mkdir -p /workspace
128

139
WORKDIR /workspace
1410
COPY ./ /workspace
1511

16-
ENV ALPINE=1
12+
RUN make test buildStatic
1713

18-
RUN make test build
19-
20-
21-
22-
FROM codacy/ci-base:2.0.0
14+
FROM alpine:3.15
2315

2416
LABEL maintainer="team@codacy.com"
2517

26-
RUN apk add --update --no-cache --force-overwrite gc-dev pcre-dev libevent-dev git
18+
RUN apk add --update --no-cache git
2719

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

Makefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CRYSTAL?=$(shell which crystal)
22
CRYSTAL_FLAGS=--release
3-
CRYSTAL_STATIC_FLAGS=--static --no-debug
3+
CRYSTAL_STATIC_FLAGS=--static
44

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

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

@@ -21,13 +21,10 @@ build: ## compiles from crystal sources
2121
.PHONY: buildStatic
2222
buildStatic: ## compiles from crystal sources into static binary
2323
mkdir -p bin
24-
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
25-
26-
.PHONY: docker
27-
docker: build docker_build ## compiles from sources and produce the docker image
24+
crystal build $(CRYSTAL_FLAGS) $(CRYSTAL_STATIC_FLAGS) src/main.cr -o bin/git-version
2825

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

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

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

4745
.PHONY: help

spec/git-version-spec.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,24 @@ describe GitVersion do
737737
tmp.cleanup
738738
end
739739
end
740+
it "should truncate long branch names in tags" do
741+
tmp = InTmp.new
742+
743+
begin
744+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir)
745+
746+
tmp.exec %(git init)
747+
tmp.exec %(git checkout -b very-very-very-very-long-branch-name-that-excedes-k8s-limits)
748+
tmp.exec %(git commit -m "commit" --allow-empty)
749+
tmp.exec %(git tag "100.100.100")
750+
751+
version = git.get_new_version
752+
hash = git.current_commit_hash
753+
version.should eq("100.100.101-veryveryveryverylongbranchname.0.#{hash}")
754+
ensure
755+
tmp.cleanup
756+
end
757+
end
740758
end
741759

742760
it "get previous version - first commit" do

spec/utils.cr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ module Utils
44
extend self
55

66
class InTmp
7-
# on Alpine last available crystal version is 0.26, this brings compatibility with 0.27
8-
TMP_DIR = {% if env("ALPINE") %} "/tmp" {% else %} Dir.tempdir {% end %}
9-
107
def initialize
118
folder = UUID.random.to_s
129

1310
puts "folder #{folder}"
1411

15-
@tmpdir = File.expand_path(folder, TMP_DIR)
12+
@tmpdir = File.expand_path(folder, Dir.tempdir)
1613

1714
FileUtils.rm_rf(@tmpdir)
1815
FileUtils.mkdir(@tmpdir)

src/git-version.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ module GitVersion
202202
nil
203203
)
204204
else
205-
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")
205+
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
206206
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
207207
previous_version =
208208
SemanticVersion.new(

src/main.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ log_paths = ""
1414

1515
folder = FileUtils.pwd
1616

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

0 commit comments

Comments
 (0)