Sha256: 6bd70b60556f584e5fd72ef7ce2fad7bda4a0bbdc6219ab53d78092f49311650

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require 'thor'
require 'thegarage/gitx'
require 'thegarage/gitx/cli/base_command'

module Thegarage
  module Gitx
    module Cli
      class BuildtagCommand < BaseCommand

        desc 'buildtag', 'create a tag for the current build and push it back to origin (supports Travis CI and Codeship)'
        def buildtag
          fail "Unknown branch. Environment variables TRAVIS_BRANCH or CI_BRANCH are required" unless branch_name
          fail "Branch must be one of the supported taggable branches: #{config.taggable_branches}" unless config.taggable_branch?(branch_name)

          label = "buildtag generated by build #{build_number}"
          create_build_tag(branch_name, label)
        end

        private

        # pull the current branch name from environment variables
        # supports Travis CI or Codeship variables
        # see https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
        def branch_name
          ENV['TRAVIS_BRANCH'] || ENV['CI_BRANCH']
        end

        def build_number
          ENV['TRAVIS_BUILD_NUMBER'] || ENV['CI_BUILD_NUMBER']
        end

        def create_build_tag(branch, label)
          timestamp = Time.now.utc.strftime '%Y-%m-%d-%H-%M-%S'
          git_tag = "build-#{branch}-#{timestamp}"
          run_cmd "git tag #{git_tag} -a -m '#{label}'"
          run_cmd "git push origin #{git_tag}"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thegarage-gitx-2.14.0 lib/thegarage/gitx/cli/buildtag_command.rb
thegarage-gitx-2.13.1 lib/thegarage/gitx/cli/buildtag_command.rb
thegarage-gitx-2.13.0 lib/thegarage/gitx/cli/buildtag_command.rb
thegarage-gitx-2.12.0 lib/thegarage/gitx/cli/buildtag_command.rb