diff --git a/.circleci/config.yml b/.circleci/config.yml index a9d18f0..95a4405 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,7 +1,7 @@ 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: @@ -9,14 +9,6 @@ references: 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 @@ -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: @@ -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: @@ -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 @@ -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 diff --git a/Dockerfile b/Dockerfile index 8210444..afd15d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 "team@codacy.com" && git config --global user.name "Codacy" +RUN git config --global user.email "team@codacy.com" && 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="team@codacy.com" -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 diff --git a/Makefile b/Makefile index b97401e..b74edeb 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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 diff --git a/spec/git-version-spec.cr b/spec/git-version-spec.cr index 0e8e2a8..3ec3e7f 100644 --- a/spec/git-version-spec.cr +++ b/spec/git-version-spec.cr @@ -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 diff --git a/spec/utils.cr b/spec/utils.cr index f879473..ed95362 100644 --- a/spec/utils.cr +++ b/spec/utils.cr @@ -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) diff --git a/src/git-version.cr b/src/git-version.cr index 6706dc7..d49959b 100644 --- a/src/git-version.cr +++ b/src/git-version.cr @@ -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( diff --git a/src/main.cr b/src/main.cr index 8a9c685..6de97d0 100644 --- a/src/main.cr +++ b/src/main.cr @@ -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 }