From dc4688e123d4ca824faca390159398aa44c9ec8a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 17 Feb 2018 19:30:45 +0100 Subject: [PATCH] Add Travis-CI. (#17) --- source/Nuke.Core.Tests/BuildServerTest.cs | 9 + source/Nuke.Core/BuildServers/Travis.cs | 159 ++++++++++++++++++ .../Nuke.Core/BuildServers/TravisEventType.cs | 21 +++ source/Nuke.Core/HostType.cs | 1 + 4 files changed, 190 insertions(+) create mode 100644 source/Nuke.Core/BuildServers/Travis.cs create mode 100644 source/Nuke.Core/BuildServers/TravisEventType.cs diff --git a/source/Nuke.Core.Tests/BuildServerTest.cs b/source/Nuke.Core.Tests/BuildServerTest.cs index 9d8db0300..4da54aa72 100644 --- a/source/Nuke.Core.Tests/BuildServerTest.cs +++ b/source/Nuke.Core.Tests/BuildServerTest.cs @@ -56,6 +56,15 @@ public void TestJenkins(PropertyInfo property) AssertProperty(Jenkins.Instance.NotNull(), property); } + [BuildServerTheory(typeof(Travis))] + [MemberData(nameof(Properties), typeof(Travis))] + public void TestTravis(PropertyInfo property) + { + AssertProperty(Travis.Instance.NotNull(), property); + Assert.True(Travis.Instance.Ci); + Assert.True(Travis.Instance.ContinousIntegration); + } + [BuildServerTheory(typeof(GitLab))] [MemberData(nameof(Properties), typeof(GitLab))] public void TestGitLab(PropertyInfo property) diff --git a/source/Nuke.Core/BuildServers/Travis.cs b/source/Nuke.Core/BuildServers/Travis.cs new file mode 100644 index 000000000..33ae29f8c --- /dev/null +++ b/source/Nuke.Core/BuildServers/Travis.cs @@ -0,0 +1,159 @@ +// Copyright Matthias Koch 2018. +// Distributed under the MIT License. +// https://github.com/nuke-build/nuke/blob/master/LICENSE + +using System; +using System.Linq; +using static Nuke.Core.EnvironmentInfo; + +namespace Nuke.Core.BuildServers +{ + /// + /// Interface according to the official website. + /// + [BuildServer] + public class Travis + { + private static Lazy s_instance = new Lazy(() => new Travis()); + + public static Travis Instance => NukeBuild.Instance?.Host == HostType.Travis ? s_instance.Value : null; + + internal static bool IsRunningTravis => Variable("TRAVIS") != null; + + internal Travis() + { + } + + public bool Ci => EnsureVariable("CI"); + public bool ContinousIntegration => EnsureVariable("CONTINUOUS_INTEGRATION"); + + /// + /// Whether you have defined any encrypted variables, including variables defined in the Repository Settings. + /// + public bool SecureEnvVars => EnsureVariable("TRAVIS_SECURE_ENV_VARS"); + + /// + /// Set to true if the job is allowed to fail otherwhise false. + /// + public bool AllowFailure => EnsureVariable("TRAVIS_ALLOW_FAILURE"); + + /// + /// For push builds, or builds not triggered by a pull request, this is the name of the branch. + /// For builds triggered by a pull request this is the name of the branch targeted by the pull request. + /// For builds triggered by a tag, this is the same as the name of the tag (TRAVIS_TAG). + /// + public string Branch => EnsureVariable("TRAVIS_BRANCH"); + + /// + /// The absolute path to the directory where the repository being built has been copied on the worker. + /// + public string BuildDir => EnsureVariable("TRAVIS_BUILD_DIR"); + + /// + /// The id of the current build that Travis CI uses internally. + /// + public string BuildId => EnsureVariable("TRAVIS_BUILD_ID"); + + /// + /// The number of the current build (for example, “4”). + /// + public int BuildNumber => EnsureVariable("TRAVIS_BUILD_NUMBER"); + + /// + /// The commit that the current build is testing. + /// + public string Commit => EnsureVariable("TRAVIS_COMMIT"); + + /// + /// The commit subject and body, unwrapped. + /// + public string CommitMessage => EnsureVariable("TRAVIS_COMMIT_MESSAGE"); + + /// + /// The range of commits that were included in the push or pull request. (Note that this is empty for builds triggered by the initial commit of a new branch.) + /// + public string CommitRange => Variable("TRAVIS_COMMIT_RANGE"); + + /// + /// Indicates how the build was triggered. + /// + public TravisEventType EventType => EnsureVariable("TRAVIS_EVENT_TYPE"); + + /// + /// The id of the current job that Travis CI uses internally. + /// + public string JobId => EnsureVariable("TRAVIS_JOB_ID"); + + /// + /// The number of the current job (for example, “4.1”). + /// + public string JobNumber => EnsureVariable("TRAVIS_JOB_NUMBER"); + + /// + /// On multi-OS builds, this value indicates the platform the job is running on. Values are linux and osx currently, to be extended in the future. + /// + public string OsName => Variable("TRAVIS_OS_NAME"); + + /// + /// TRAVIS_PULL_REQUEST is set to the pull request number if the current job is a pull request build, or false if it’s not. + /// + public string PullRequest => EnsureVariable("TRAVIS_PULL_REQUEST"); + + /// + /// If the current job is a pull request, the name of the branch from which the PR originated. + /// If the current job is a push build, this variable is empty(""). + /// + public string PullRequestBranch => EnsureVariable("TRAVIS_PULL_REQUEST_BRANCH"); + + /// + /// If the current job is a pull request, the commit SHA of the HEAD commit of the PR. + /// If the current job is a push build, this variable is empty(""). + /// + public string PullRequestSha => EnsureVariable("TRAVIS_PULL_REQUEST_SHA"); + + /// + /// If the current job is a pull request, the slug (in the form owner_name/repo_name) of the repository from which the PR originated. + /// If the current job is a push build, this variable is empty(""). + /// + public string PullRequestSlug => EnsureVariable("TRAVIS_PULL_REQUEST_SLUG"); + + /// + /// The slug (in form: owner_name/repo_name) of the repository currently being built. + /// + public string RepoSlug => EnsureVariable("TRAVIS_REPO_SLUG"); + + /// + /// true or false based on whether sudo is enabled. + /// + public string Sudo => EnsureVariable("TRAVIS_SUDO"); + + /// + /// Is set to 0 if the build is successful and 1 if the build is broken. + /// + public string TestResult => EnsureVariable("TRAVIS_TEST_RESULT"); + + /// + /// If the current build is for a git tag, this variable is set to the tag’s name. + /// + public string Tag => Variable("TRAVIS_TAG"); + + public string DartVersion => Variable("TRAVIS_DARTVersion"); + public string GoVersion => Variable("TRAVIS_GOVersion"); + public string HaxeVersion => Variable("TRAVIS_HAXEVersion"); + public string JdkVersion => Variable("TRAVIS_JDKVersion"); + public string JuliaVersion => Variable("TRAVIS_JULIAVersion"); + public string NodeVersion => Variable("TRAVIS_NODEVersion"); + public string OtpRelease => Variable("TRAVIS_OTP_RELEASE"); + public string PerlVersion => Variable("TRAVIS_PERLVersion"); + public string PhpVersion => Variable("TRAVIS_PHPVersion"); + public string PythonVersion => Variable("TRAVIS_PYTHONVersion"); + public string RVersion => Variable("TRAVIS_RVersion"); + public string RubyVersion => Variable("TRAVIS_RUBYVersion"); + public string RustVersion => Variable("TRAVIS_RUSTVersion"); + public string ScalaVersion => Variable("TRAVIS_SCALAVersion"); + public string XCodeSdk => Variable("TRAVIS_XCODE_SDK"); + public string XCodeScheme => Variable("TRAVIS_XCODE_SCHEME"); + public string XCodeProject => Variable("TRAVIS_XCODE_PROJECT"); + public string XCodeWorkspace => Variable("TRAVIS_XCODE_WORKSPACE"); + } +} diff --git a/source/Nuke.Core/BuildServers/TravisEventType.cs b/source/Nuke.Core/BuildServers/TravisEventType.cs new file mode 100644 index 000000000..234b3465c --- /dev/null +++ b/source/Nuke.Core/BuildServers/TravisEventType.cs @@ -0,0 +1,21 @@ +// Copyright Matthias Koch 2018. +// Distributed under the MIT License. +// https://github.com/nuke-build/nuke/blob/master/LICENSE + +using System; +using System.Linq; +using JetBrains.Annotations; + +// ReSharper disable InconsistentNaming + +namespace Nuke.Core.BuildServers +{ + [PublicAPI] + public enum TravisEventType + { + push, + pull_request, + api, + cron + } +} diff --git a/source/Nuke.Core/HostType.cs b/source/Nuke.Core/HostType.cs index 5fd8154dd..1d809b87e 100644 --- a/source/Nuke.Core/HostType.cs +++ b/source/Nuke.Core/HostType.cs @@ -15,6 +15,7 @@ public enum HostType Bitrise, AppVeyor, Jenkins, + Travis, GitLab } }