Skip to content

Commit

Permalink
#5 🏗️ Exposed Env Vars Use Cases Exposed For Developers (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaya2devops committed Sep 20, 2023
1 parent 6ccef7d commit 347960a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# Define your project root
PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'
124 changes: 63 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,112 @@
# Streamlining Terraform CLI Setup
# Effective Use of Environment Variables
We will explore various practical use cases for environment variables, providing clear and concise instructions to enhance your understanding.

I commit using an emoji `:building_construction:` from here;

We began our journey by identifying the Linux distribution we were using, specifically Ubuntu, and determined this by inspecting the system file.
https://github.com/ikatyang/emoji-cheat-sheet/blob/master/README.md

[Check your OS Version](https://www.cyberciti.biz/faq/how-to-check-os-version-in-linux-command-line/)
### Listing Environment Variables
To easily list all environment variables in your current environment, use the following command:


We did it [just here](assets/os-distru.png).

Further, the cli failed due to the following.<br>
The point of concern was this line of code:
```sh
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
```

To enhance it, we modified it as follows:
```sh
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" -y
env
```

If you need to filter and display environment variables containing a specific keyword, employ the grep command as follows:
```
env | grep KEYWORD
```

Next, we consulted the official Terraform documentation to acquire the correct installation instructions:

https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
The `grep` command is a powerful tool for data filtering.


We meticulously followed these instructions one by one, resulting in successful installation.
## Managing Workspace in Gitpod
Gitpod workspaces begin at the `/workspace/here` directory. To avoid accidentally pushing unnecessary files to your repository, follow these steps in your bash script:

1. Navigate to the workspace at the beginning of your script:
```
cd /workspace
```

[See this asset](assets/refactor-tf.png) to observe the comparison.
2. After the installation is complete, return to your actual workspace repository:
```
cd /workspace/terraform-beginner-bootcamp
```

Subsequently, we transformed these steps into a Bash script, located at `./bin/here`
This approach ensures that your installation occurs in the correct location, with the CLI readily available.


### Automate the Script
### Understanding Gitpod-Provided Environment Variables

In the context of Gitpod, we simplified the setup step within the workflow from:
Gitpod automatically assigns environment variables to your workspace. To access them, execute the `env` command and look for variables like `THEIA_WORKSPACE_ROOT`, which will have values like `/workspace/terraform-beginner-bootcamp-2023`.

```yaml
init: |
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
You can use these variables in your scripts as shown below:
```

To just;

```yaml
init: |
source ./bin/install_terraform_cli
cd $THEIA_WORKSPACE_ROOT
```

Furthermore, we established a solution to address the scenario where initializing occurs only when starting a new workspace:
Remember

| Scenario | Initialization |
|-----------------------------------|------------------|
| When starting a new workspace | `init` |
| Launching an existing workspace | No `init` |
- To assign an environment variable in bash, use the syntax `VARIABLE_NAME='value'`.
- To print the content of a specific environment variable, employ the `echo $env` format.

**Solution;**

Use either the `before` or `command` directive as shown below:

### Assigning Environment Variables in Bash
You can manually assign environment variables within your bash script. Follow these steps:

```yaml
before: |
source ./bin/install_terraform_cli
1. At the top of your script, define the variable:
```
PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'
```

#### Linux Permissions

Take the file, make it executable and associate it with the current user.
2. Use the variable as needed in your script:
```
cd $PROJECT_ROOT
```
Alternatively, you can assign the variable when executing the script:

```sh
chmod u+x ./bin/install_terraform_cli
PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023' ./bin/install_terraform_cli
```

Or follownig this number:
Or export it within your environment:


```sh
chmod 744 ./bin/install_terraform_cli
export PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'
```

To persist this environment variable in Gitpod, utilize the gp env command:

Bash scripts start with shebang, learn more;
https://en.wikipedia.org/wiki/Chmod
```sh
gp env PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'
```
To remove the environment variable, use the unset command:

```sh
unset PROJECT_ROOT
```

Execute scripts;

Use the `./` shorthand notiation to execute the bash script.

eg. `./bin/install_terraform_cli` its included above.
## Create Dotenv

> Refer to [.gitpod.yml](.gitpod.yml)
For better organization, it's recommended to centralize all environment variables in a dotenv file.


### Considerations
1. Create file in the ``/bin/` and name it `.env.sample`.
2. Add your desired environment variables to it.
```
# Hello I am called .env
Create an issue [#3](https://github.com/yaya2devops/terraform-beginner-bootcamp-2023/issues/3) that will have the number three cause;
# Define your project root
PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'
```

- Our initial issue, identified as #1, was established.
- We initiated a Pull Request (PR) to implement the assigned task from that issue, which is now #2.
- Subsequently, we introduced a new issue, referenced as [#3](https://github.com/yaya2devops/terraform-beginner-bootcamp-2023/issues/3), with distinct objectives.
By following this approach, you can maintain a cleaner and more organized environment variable configuration in your project.

> You have to remove the .sample to make it work.


8 changes: 7 additions & 1 deletion bin/install_terraform_cli
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

PROJECT_ROOT='/workspace/terraform-beginner-bootcamp-2023'

cd /workspace

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

wget -O- https://apt.releases.hashicorp.com/gpg | \
Expand All @@ -16,4 +20,6 @@ sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update

sudo apt-get install terraform -y
sudo apt-get install terraform -y

cd $PROJECT_ROOT

0 comments on commit 347960a

Please sign in to comment.