LAB 49 ‐ Connect to GitHub Repo - DevWithNehaCloud/Oracle-Cloud-Infrastructure-wiki GitHub Wiki

Connecting Local Terraform Code to GitHub repository using OCI Code Editor

This guide explains the step-by-step process of securely pushing your local Terraform code from the OCI (Oracle Cloud Infrastructure) Console's built-in Code Editor to a GitHub repository.


Step 1: Create a New Repository on GitHub

First, we need to create an empty space (Repository) on GitHub to host our project.

  1. Go to your GitHub account and log in.
  2. In the top-right corner, click the + (Plus icon) and select New repository.
  3. Enter a Repository name (e.g., oci-terraform-code).
  4. Set the visibility to Public
  5. CRITICAL: Under the "Initialize this repository with" section, enable README
  6. Click the Create repository button.
  7. Copy the HTTPS URL displayed on the screen. (It will look like: https://github.com/your-username/your-repo-name.git)

Step 2: Open OCI Code Editor & Configure Git Identity

Now, open the OCI Code Editor to set up your Git credentials. This tells Git who is making the changes.

  1. Log in to your OCI Console.
  2. Click the Code Editor icon in the top navigation bar to open the terminal and workspace.
  3. Run the following commands in the Code Editor terminal:

Set your Git email address

git config --global user.email "[email protected]"

Set your Git profile name

git config --global user.name "Your Name"

Step 3: Enable Credential Helper (To Avoid Repeated Passwords)

To prevent OCI Code Editor from asking for your credentials every single time you push or pull code, run this command to make Git remember them:

git config --global credential.helper store

Step 4: Initialize Git & Stage Terraform Code

Let's initialize Git in your project directory and stage your Terraform files for the initial commit.

Navigate to your Terraform project folder in the terminal using cd your-folder-name.

Execute the following commands in sequence:

1. Initialize a blank local Git repository

git init

2. Stage all files in the current directory

git add .

3. Check the status (All files should now appear in green text)

git status

Step 5: Commit & Link to GitHub

Now, create a snapshot of your current code version and link your local project to the GitHub remote repository.

1. Commit your staged changes with a descriptive message

git commit -m "Initial details of my terraform repo"

2. Link your local repository to GitHub

(Replace with the HTTPS URL you copied in Step 1)

git remote add origin <your-copied-github-url>

Step 6: Push Code to GitHub

This is the final execution step where your local Terraform code gets securely uploaded to your GitHub repository.

Push the local master branch code to the remote origin repository

git push -u origin master

⚠️ Authentication Note: When you run this command, the terminal will prompt you for:

Username: Type your GitHub username.

Password: Do not enter your standard GitHub account password. GitHub requires a Personal Access Token (PAT) here. Paste your token and press Enter.

Once authenticated, your code will successfully upload!

🎯 Verification on GitHub Since we pushed directly to the main tracking branch (master), there is no need to perform any Merge actions on GitHub.

Simply refresh your GitHub repository page in your web browser.

You will now see all your Terraform structure files (main.tf, variables.tf, etc.) successfully uploaded and visible online!

Note on Branches & Pull Requests: After pushing your code, you might see a yellow banner on GitHub saying "Compare & pull request", along with two branches: a default empty main branch and your newly uploaded master branch.

You do not need to click "Compare & pull request". This banner appears because GitHub is simply suggesting you merge the code from your master branch into the empty main branch. Since you are working individually on this project, there is no need to go through the merge process. All your Terraform files are already safe and accessible in the master branch. You can safely ignore this banner and simply switch the branch dropdown to master to view your files.

OR

Initially, my local repository was using the master branch, while GitHub had main as the default branch. My code was pushed successfully to the master branch, but GitHub showed a compare warning because main and master had different histories. Since all my project code was in the master branch, I changed the default branch from main to master. This made the local and GitHub branches consistent and simplified future code pushes. Now you don't need to change the dropdown from master to main to view your files since master is the default branch now.

Repository Structure Updated Successfully

Initially, I pushed the code from inside the compute_service folder. As a result, GitHub displayed only the contents of that folder (dev, test, prod, uat, instance_module) at the repository root level instead of showing the compute_service folder itself.

To organize the repository properly, I recreated the repository and created a parent folder named oci-terraform-code. Then, by following the same Git commands documented above (git init, git add, git commit, git remote add origin, and git push), I pushed both service folders under this parent directory.

My repository structure is now:

oci-terraform-code
├── compute_service
│   ├── dev
│   ├── test
│   ├── prod
│   ├── uat
│   └── instance_module
│
└── identity_service
    ├── dev
    ├── test
    ├── prod
    ├── uat
    └── compartment_module

Now, all Compute-related Terraform code is stored inside compute_service, and all Identity/Compartment-related Terraform code is stored inside identity_service, resulting in a cleaner, more organized, and scalable repository structure.

Note: The same Git commands mentioned above were used throughout the process. No additional Git initialization was required inside the service folders. Only the parent folder (oci-terraform-code) is maintained as the Git repository, making it easy to add new Terraform services in the future.

Additional Note: Since Git credentials had already been configured earlier, there was no need to enter the GitHub username and Personal Access Token (PAT) again. The existing credential configuration was reused automatically, allowing subsequent git push operations to complete without additional authentication prompts.

How to Create a New GitHub Personal Access Token (Classic)

Follow these steps to generate a personal access token to authenticate your command line, VS Code, or Git pushes.

Step-by-Step Guide

  1. Open Settings:

    • Go to GitHub and log in.
    • Click on your Profile Picture in the top-right corner.
    • Select Settings from the dropdown menu.
  2. Access Developer Settings:

    • Scroll down to the bottom of the left sidebar.
    • Click on Developer settings.
  3. Navigate to Tokens:

    • In the left sidebar, click on Personal access tokens.
    • Select Tokens (classic) from the dropdown.
  4. Generate the Token:

    • Click the Generate new token button.
    • Select Generate new token (classic).
  5. Configure Token Details:

    • Note: Enter a descriptive name (e.g., VSCode-Push) so you know what this token is used for.
    • Expiration: Choose an expiration period (e.g., 30 or 90 days).
    • Scopes (Permissions): Check the box next to repo (this is critical—it grants the permission required to push your code).
  6. Create and Save:

    • Scroll to the very bottom of the page.
    • Click Generate token.

[!WARNING] Copy your token immediately! Make sure to copy the generated token code right away and save it securely (e.g., in a password manager or a secure note). Once you refresh or navigate away from this page, you will never be able to see this token again.