env-git
Description
The project is a library designed to facilitate the retrieval and parsing of environment variables from various hosting platforms. It provides functions to extract environment variables from environment files stored on GitLab, GitHub, and Bitbucket.
Overall, this library simplifies the process of retrieving and parsing environment variables from environment files stored on different hosting platforms, making it easier to manage and utilize those variables within an application.
Installation
Either through cloning with git or by using npm (the recommended way):
npm install env-git
Parameters
-
hostingPlatform
(string): The hosting platform where the repository is hosted. It accepts three possible values: "gitlab"
, "github"
, or "bitbucket"
. This parameter determines the platform-specific logic for retrieving the environment file.
-
config
(object): Additional configuration options for the hosting platform. This parameter is optional and can be used to provide specific configuration settings based on the hosting platform.
Usage
To represent both ways of providing configuration in the usage section, you can update the code examples as follows:
1. Using a Environment Variable
The library supports configuration using various file formats such as .env
, .json
, or .yaml
. By providing a configuration file in one of these formats.
To utilize the env-git
library, you can create a .env
file in your repository. The .env
file should contain the environment variables you want to retrieve and use in your application.
Here's an example of a .env
file:
GIT_ENV_PLATFORM=gitlab
GIT_ENV_ACCESS_TOKEN=your-access-token
GIT_ENV_REPO=your-repository
GIT_ENV_BRANCH=your-repo-branch
GIT_ENV_FILE_PATH=.env
GIT_ENV_OWNER=your-github-username # Only for GitHub
GIT_ENV_HOST_URL=your-gitlab-host-url # Only for GitLab
GIT_ENV_WORKSPACE=your-bitbucket-workspace # Only for Bitbucket
Note: Assign the appropriate values to each environment variable without changing the key names. This configuration enables the library to fetch and load the environment variables from the corresponding hosting platform based on the provided values.
Once you have the .env
file in your repository, you can use the loadEnvFromFile
function to load the environment variables directly from the file:
import GitENV, { PLATFORM } from "env-git";
async function main() {
const readENV = new GitENV();
await readENV.loadEnv();
}
main();
Users can choose the approach that suits their needs and provide the necessary configuration accordingly.
2. Using Function Arguments
The library provides two functions, loadEnv
and fetchEnv
, that allow you to retrieve and load environment variables using function arguments.
Here an example demonstrates how to directly specify the configuration using function arguments, allowing users to provide the necessary details.
import GitENV, { PLATFORM } from "env-git";
const platform = PLATFORM.GITHUB;
const config = {
access_token: "your-access-token",
repository: "your-repository",
repo_branch: "your-repo-branch",
file_path: ".env",
host_url: "your-gitlab-host-url", # Only for GitHub
owner: "your-github-username", # Only for GitLab
workspace: "your-bitbucket-workspace", # Only for Bitbucket
}
async function main() {
const readENV = new GitENV({platform, config});
await readENV.loadEnv();
}
main();
Obtaining Repository Access Token Platform Wise
GitLab
To obtain the GitLab repository access token and information, follow these steps:
GitHub
To obtain the GitHub repository access token and information, follow these steps:
Bitbucket
To obtain the Bitbucket repository access token and information, follow these steps: