I’m a fan of Travis CI and use it for continuous integration across pretty much all my open-source projects on GitHub. From time to time, I need to obtain a URL to a file in the repository in my build, e.g. to point a particular tool to it, in a way that respects branches as well as pull requests from forks.
Here’s how I do it:
Explanation:
env
marks the section of the build file (.travis.yml
) for defining environment variables.https://raw.githubusercontent.com/
is the prefix to a URL to a RAW file on GitHub.TRAVIS_PULL_REQUEST_SLUG
is an environment variable where Travis puts a string of the formowner_name/repo_name
, if the current job is a pull request. The variable is empty for a push build.TRAVIS_REPO_SLUG
is an environment variable containing a string of the formowner_name/repo_name
of the repository being built.TRAVIS_PULL_REQUEST_BRANCH
is an environment variable containing the name of the branch from which the pull request originated. The variable is empty for a push build.TRAVIS_BRANCH
is an environment variable containing the name of the branch (for push builds), the name of the branch targeted by the pull request (for pull request builds), or the name of the tag (for tag builds).{FILE_PATH}
is a placeholder for the path to the file you want to link to with respect to the root of the repository.${A:-$B}
construct returns the value of the environment variableA
ifA
is not empty. It returns the value of environment variableB
otherwise.
To see this in action, check out the build file of one of the projects I contribute to. We construct a URL to an OpenAPI specification to validate via Swagger Validator Badge. For more information about default environment variables in Travis, see this page.
Comments