GithubHelp home page GithubHelp logo

isabella232 / octoversion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from octopusdeploy/octoversion

0.0 0.0 0.0 240 KB

.NET tool for determining version number based on the Git revision graph

License: Apache License 2.0

Shell 2.15% C# 94.86% PowerShell 2.79% Batchfile 0.20%

octoversion's Introduction

feedz.io

Welcome to OctoVersion

What is OctoVersion?

OctoVersion is a dotnet tool for deriving a SemVer 2.0 version number based on a Git revision graph.

Getting started

Install the tool into the global tool registry:

dotnet tool install --global Octopus.OctoVersion.Tool

or into the local project's tool manifest:

dotnet tool install Octopus.OctoVersion.Tool

Run it against a local Git repository:

cd ~/code/my-app
octoversion --CurrentBranch=main # if installed globally
dotnet tool run octoversion --CurrentBranch=main # if installed locally

You should see output that looks something like this:

[22:16:32 INF] Version is 0.0.5

Integrating with build tools

Nuke

Add a reference to OctoVersion.Tool in your Nuke _build project. This can be done using the nuke command:

nuke :add-package Octopus.OctoVersion.Tool --version X.X.X

Add a field marked with [OctoVersion] and it will be auto-populated:

using Nuke.Common
using Nuke.Common.Git;
using Nuke.Common.Tools.OctoVersion;
using Serilog;

class Build : NukeBuild
{
     [GitRepository]
     readonly GitRepository GitRepository;

     string Branch => GitRepository.Branch;

     // The Required Attribute will automatically throw an exception if the 
     // OctoVersionInfo parameter is not set due to an error or misconfiguration in Nuke.
     [Required]
     // 'Framework = "net6.0"' is only required for net6.0 apps.
     [OctoVersion(UpdateBuildNumber = true, 
                  BranchParameter = nameof(Branch),
                  Framework = "net6.0")]
     readonly OctoVersionInfo OctoVersionInfo;
     
     Target PrintVersion => _ => _
     .Executes(() => {
                Log.Information($"The version is {OctoVersion.MajorMinorBuild}");
     });
}

Take a look at the Nuke OctoVersionAttribute for all available attribute properties.

Cake

#module nuget:?package=Cake.DotNetTool.Module&version=0.4.0
#addin "nuget:?package=Cake.OctoVersion&version=0.3.63"

// The list below is to manually resolve NuGet dependencies to work around a bug in Cake's dependency loader.
// Our intention is to remove this list again once the Cake bug is fixed.
//
// What we want:
// #addin "nuget:?package=Cake.OctoVersion&version=0.0.138&loaddependencies=true"
// (Note the loaddependencies=true parameter.)
//
// Our workaround:
#addin "nuget:?package=LibGit2Sharp&version=0.26.2"
#addin "nuget:?package=Serilog&version=2.8.0.0"
#addin "nuget:?package=Serilog.Settings.Configuration&version=3.1.0.0"
#addin "nuget:?package=Serilog.Sinks.Console&version=3.0.1.0"
#addin "nuget:?package=Serilog.Sinks.Literate&version=3.0.0.0"
#addin "nuget:?package=SerilogMetrics&version=2.1.0.0"
#addin "nuget:?package=Octopus.OctoVersion.Core&version=0.3.63"
#addin "nuget:?package=Cake.OctoVersion&version=0.0.138"
#addin "nuget:?package=Microsoft.Extensions.Primitives&version=3.1.7"
#addin "nuget:?package=Microsoft.Extensions.Configuration&version=3.1.7.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.Abstractions&version=3.1.7.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.Binder&version=3.1.7.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.CommandLine&version=3.1.7.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.EnvironmentVariables&version=3.1.7.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.FileExtensions&version=3.1.0.0"
#addin "nuget:?package=Microsoft.Extensions.Configuration.Json&version=3.1.7"
#addin "nuget:?package=Microsoft.Extensions.DependencyModel&version=2.0.4.0"
#addin "nuget:?package=Microsoft.Extensions.FileProviders.Abstractions&version=3.1.0.0"
#addin "nuget:?package=Microsoft.Extensions.FileProviders.Physical&version=3.1.0.0"

using Path = System.IO.Path;
using IO = System.IO;
using Cake.Common.Tools;

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");

if (!BuildSystem.IsRunningOnTeamCity) OctoVersionDiscoverLocalGitBranch(out _);
OctoVersion(out var versionInfo);

Task("Default")
    .IsDependentOn("Build");

Task("Build")
    .IsDependentOn("Restore")
    .Does(() =>
    {
        DotNetCoreBuild("./source", new DotNetCoreBuildSettings
        {
            Configuration = configuration,
            NoRestore = true,
            ArgumentCustomization = args => args.Append($"/p:Version={versionInfo.FullSemVer} /p:InformationalVersion={versionInfo.FullSemVer}"),
            MSBuildSettings = new DotNetCoreMSBuildSettings
            {
                ArgumentCustomization = args => args.Append($"/p:Version={versionInfo.FullSemVer} /p:InformationalVersion={versionInfo.FullSemVer}")
            }
        });
    });

RunTarget(target);

TeamCity

octoversion --CurrentBranch=main --OutputFormats:0=TeamCity
##teamcity[buildNumber '0.0.12']
##teamcity[setParameter name='env.OCTOVERSION_Major' value='0']
##teamcity[setParameter name='OctoVersion.Major' value='0']
##teamcity[setParameter name='env.OCTOVERSION_Minor' value='0']
##teamcity[setParameter name='OctoVersion.Minor' value='0']
##teamcity[setParameter name='env.OCTOVERSION_Patch' value='12']
##teamcity[setParameter name='OctoVersion.Patch' value='12']
##teamcity[setParameter name='env.OCTOVERSION_MajorMinorPatch' value='0.0.12']
##teamcity[setParameter name='OctoVersion.MajorMinorPatch' value='0.0.12']
##teamcity[setParameter name='env.OCTOVERSION_PreReleaseTag' value='']
##teamcity[setParameter name='OctoVersion.PreReleaseTag' value='']
##teamcity[setParameter name='env.OCTOVERSION_PreReleaseTagWithDash' value='']
##teamcity[setParameter name='OctoVersion.PreReleaseTagWithDash' value='']
##teamcity[setParameter name='env.OCTOVERSION_BuildMetadata' value='']
##teamcity[setParameter name='OctoVersion.BuildMetadata' value='']
##teamcity[setParameter name='env.OCTOVERSION_BuildMetadataWithPlus' value='']
##teamcity[setParameter name='OctoVersion.BuildMetadataWithPlus' value='']
##teamcity[setParameter name='env.OCTOVERSION_FullSemVer' value='0.0.12']
##teamcity[setParameter name='OctoVersion.FullSemVer' value='0.0.12']```

Environment variables

octoversion --CurrentBranch=main --OutputFormats:0=Environment
OCTOVERSION_Major=0
OCTOVERSION_Minor=0
OCTOVERSION_Patch=5
OCTOVERSION_MajorMinorPatch=0.0.5
OCTOVERSION_PreReleaseTag=
OCTOVERSION_PreReleaseTagWithDash=
OCTOVERSION_BuildMetadata=
OCTOVERSION_BuildMetadataWithPlus=
OCTOVERSION_FullSemVer=0.0.5

JSON

octoversion --CurrentBranch=main --OutputFormats:0=Json
{
  "Major": 0,
  "Minor": 0,
  "Patch": 5,
  "MajorMinorPatch": "0.0.5",
  "PreReleaseTag": "",
  "PreReleaseTagWithDash": "",
  "BuildMetadata": "",
  "BuildMetadataWithPlus": "",
  "FullSemVer": "0.0.5"
}

Configuration

OctoVersion sources configuration from:

  1. The first, if any, octoversion.json file (lower case!) it finds walking up from the current directory.
  2. Environment variables prefixed with the OCTOVERSION_ prefix.
  3. Command-line parameters.

with the later sources overriding the earlier ones. The configuration syntax for each is as per the standard .NET Core standard configuration providers:

FAQ

Can't you figure out the branch I'm on? Why do I have to specify it?

This way, madness lies.

It's important to remember that branches in Git are a lie. Branches are nothing more than pointers to a commit. It's entirely possible (and very likely) that a commit will be reachable from many branches - or no branches, if you've checked out a commit and have a detached head.

Rather than attempting to guess (and sneakily getting the answer incorrect), OctoVersion requires you to tell it which branch you're on. All good build servers will provide this as an environment variable or other build parameter, and it's trivial to pass the information to OctoVersion.

For local versioning, it's always possible to set up a bash/zsh alias along these lines:

alias ov="octoversion --CurrentBranch=`git branch --show-current`"

octoversion's People

Contributors

cailyoung avatar casstait avatar corey-underdown avatar dependabot[bot] avatar droyad avatar iwillspeak avatar liam-mackie avatar matt-richardson avatar michaelnoonan avatar octobob avatar ryangribble avatar scme0 avatar slewis74 avatar uglybugger avatar valadas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.