Skip to content

Commit ccff320

Browse files
authored
Merge pull request #2 from MicrosoftCloudEssentials-LearningHub/terraform-case2
Terraform case2
2 parents 624e9fb + 4117cfd commit ccff320

10 files changed

Lines changed: 781 additions & 27 deletions

File tree

.github/workflows/use-visitor-counter.yml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
uses: actions/checkout@v4
2222
with:
2323
fetch-depth: 0
24+
ref: ${{ github.head_ref || github.ref_name }}
2425

2526
- name: Shallow clone visitor counter logic
2627
run: git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git
@@ -57,30 +58,23 @@ jobs:
5758
git config --global user.name "github-actions[bot]"
5859
git config --global user.email "github-actions[bot]@users.noreply.github.com"
5960
60-
# Commit and push logic for PR events (merge, not rebase)
61-
- name: Commit and push changes (PR)
62-
if: github.event_name == 'pull_request'
61+
- name: Commit and merge changes
6362
env:
64-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
PR_BRANCH: ${{ github.head_ref || github.ref_name }}
64+
GIT_AUTHOR_NAME: github-actions[bot]
65+
GIT_AUTHOR_EMAIL: github-actions[bot]@users.noreply.github.com
66+
GIT_COMMITTER_NAME: github-actions[bot]
67+
GIT_COMMITTER_EMAIL: github-actions[bot]@users.noreply.github.com
6568
run: |
66-
git fetch origin
67-
git checkout ${{ github.head_ref }}
68-
git pull origin ${{ github.head_ref }} || echo "No merge needed"
69+
# Ensure we're on the correct branch
70+
git switch -c "$PR_BRANCH" || git switch "$PR_BRANCH"
71+
72+
# Stage and commit changes if any
6973
git add -A
70-
git commit -m "Update visitor count" || echo "No changes to commit"
71-
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
72-
git push origin HEAD:${{ github.head_ref }}
73-
74-
# Commit and push logic for non-PR events (merge, not rebase)
75-
- name: Commit and push changes (non-PR)
76-
if: github.event_name != 'pull_request'
77-
env:
78-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
79-
run: |
80-
git fetch origin
81-
git checkout ${{ github.ref_name }} || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
82-
git pull origin ${{ github.ref_name }} || echo "No merge needed"
83-
git add -A
84-
git commit -m "Update visitor count" || echo "No changes to commit"
85-
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
86-
git push origin HEAD:${{ github.ref_name }}
74+
git diff --staged --quiet || git commit -m "Update visitor count"
75+
76+
# Pull and merge existing changes
77+
git pull origin "$PR_BRANCH" --no-rebase
78+
79+
# Push all changes
80+
git push origin "$PR_BRANCH"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Last updated: 2025-08-27
108108

109109
<!-- START BADGE -->
110110
<div align="center">
111-
<img src="https://img.shields.io/badge/Total%20views-42-limegreen" alt="Total views">
111+
<img src="https://img.shields.io/badge/Total%20views-1403-limegreen" alt="Total views">
112112
<p>Refresh Date: 2025-09-05</p>
113113
</div>
114114
<!-- END BADGE -->

scenario1-high-decay/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Last updated: 2025-08-27
135135

136136
<!-- START BADGE -->
137137
<div align="center">
138-
<img src="https://img.shields.io/badge/Total%20views-42-limegreen" alt="Total views">
138+
<img src="https://img.shields.io/badge/Total%20views-1403-limegreen" alt="Total views">
139139
<p>Refresh Date: 2025-09-05</p>
140140
</div>
141141
<!-- END BADGE -->

scenario1-high-decay/terraform-infrastructure/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ graph TD;
126126

127127
<!-- START BADGE -->
128128
<div align="center">
129-
<img src="https://img.shields.io/badge/Total%20views-42-limegreen" alt="Total views">
129+
<img src="https://img.shields.io/badge/Total%20views-1403-limegreen" alt="Total views">
130130
<p>Refresh Date: 2025-09-05</p>
131131
</div>
132132
<!-- END BADGE -->
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Azure Infrastructure Terraform Template
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2025-09-05
9+
10+
----------
11+
12+
<details>
13+
<summary><b>Table of content</b> (Click to expand)</summary>
14+
15+
- [Prerequisites](#prerequisites)
16+
- [Overview](#overview)
17+
- [How to execute it](#how-to-execute-it)
18+
19+
</details>
20+
21+
## Prerequisites
22+
23+
- An `Azure subscription is required`. All other resources, including instructions for creating a Resource Group, are provided in this workshop.
24+
- `Contributor role assigned or any custom role that allows`: access to manage all resources, and the ability to deploy resources within subscription.
25+
- If you choose to use the Terraform approach, please ensure that:
26+
- [Terraform is installed on your local machine](https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli#install-terraform).
27+
- [Install the Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to work with both Terraform and Azure commands.
28+
29+
## Overview
30+
31+
<div align="center">
32+
<img src="https://github.com/user-attachments/assets/47c6f4a2-7366-410f-ae06-9c1aa9cd6514" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
33+
</div>
34+
35+
<div align="center">
36+
<img src="https://github.com/user-attachments/assets/e5d47acd-dabd-41b3-8fc5-da5f7c4b5b68" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
37+
</div>
38+
39+
> Templates structure:
40+
41+
```
42+
.
43+
├── README.md
44+
├────── main.tf
45+
├────── variables.tf
46+
├────── provider.tf
47+
├────── terraform.tfvars
48+
├────── outputs.tf
49+
```
50+
51+
- main.tf `(Main Terraform configuration file)`: This file contains the core infrastructure code. It defines the resources you want to create, such as virtual machines, networks, and storage. It's the primary file where you describe your infrastructure in a declarative manner.
52+
- variables.tf `(Variable definitions)`: This file is used to define variables that can be used throughout your Terraform configuration. By using variables, you can make your configuration more flexible and reusable. For example, you can define variables for resource names, sizes, and other parameters that might change between environments.
53+
- provider.tf `(Provider configurations)`: Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs. This file specifies which providers (e.g., AWS, Azure, Google Cloud) you are using and any necessary configuration for them, such as authentication details.
54+
- terraform.tfvars `(Variable values)`: This file contains the actual values for the variables defined in `variables.tf`. By separating variable definitions and values, you can easily switch between different sets of values for different environments (e.g., development, staging, production) without changing the main configuration files.
55+
- outputs.tf `(Output values)`: This file defines the output values that Terraform should return after applying the configuration. Outputs are useful for displaying information about the resources created, such as IP addresses, resource IDs, and other important details. They can also be used as inputs for other Terraform configurations or scripts.
56+
57+
## How to execute it
58+
59+
```mermaid
60+
graph TD;
61+
A[az login] --> B(terraform init)
62+
B --> C{Terraform provisioning stage}
63+
C -->|Review| D[terraform plan]
64+
C -->|Order Now| E[terraform apply]
65+
C -->|Delete Resource if needed| F[terraform destroy]
66+
```
67+
68+
> [!IMPORTANT]
69+
> Please modify `terraform.tfvars` with your information, then run the following flow. If you need more visual guidance, please check the video that illustrates the provisioning steps.
70+
71+
1. **Login to Azure**: This command logs you into your Azure account. It opens a browser window where you can enter your Azure credentials. Once logged in, you can manage your Azure resources from the command line.
72+
73+
> Go to the path where Terraform files are located:
74+
75+
```sh
76+
cd scenario2-optimized/terraform-infrastructure
77+
```
78+
79+
```sh
80+
az login
81+
```
82+
83+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/53b47aa7-134e-4cf7-b0b8-cdebdd0583ed" />
84+
85+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/1d9a247d-3dc9-472f-9305-4e4f0ecb72f1" />
86+
87+
2. **Initialize Terraform**: Initializes the working directory containing the Terraform configuration files. It downloads the necessary provider plugins and sets up the backend for storing the state.
88+
89+
``` sh
90+
terraform init
91+
```
92+
93+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/a7a32891-ad72-423a-a1fe-bdb50925b546" />
94+
95+
3. **Terraform Provisioning Stage**:
96+
97+
- **Review**: Creates an execution plan, showing what actions Terraform will take to achieve the desired state defined in your configuration files. It uses the variable values specified in `terraform.tfvars`.
98+
99+
```sh
100+
terraform plan -var-file terraform.tfvars
101+
```
102+
103+
> At the end, you will see a message in green if everything was executed successfully:
104+
105+
<img width="550" alt="Screenshot 2025-03-18 145143" src="https://github.com/user-attachments/assets/4741e863-1ccd-4f2a-a0b8-d5d1964bd890" />
106+
107+
- **Order Now**: Applies the changes required to reach the desired state of the configuration. It prompts for confirmation before making any changes. It also uses the variable values specified in `terraform.tfvars`.
108+
109+
```sh
110+
terraform apply -var-file terraform.tfvars
111+
```
112+
113+
> At the end, you will see a message in green if everything was executed successfully:
114+
115+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/2b32b63f-3e9f-46da-a5e9-c39360135251">
116+
117+
- **Remove**: Destroys the infrastructure managed by Terraform. It prompts for confirmation before deleting any resources. It also uses the variable values specified in `terraform.tfvars`.
118+
119+
```sh
120+
terraform destroy -var-file terraform.tfvars
121+
```
122+
123+
> At the end, you will see a message in green if everything was executed successfully:
124+
125+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/f2089d03-3a3d-431d-b462-8148ef519104">
126+
127+
<!-- START BADGE -->
128+
<div align="center">
129+
<img src="https://img.shields.io/badge/Total%20views-1403-limegreen" alt="Total views">
130+
<p>Refresh Date: 2025-09-05</p>
131+
</div>
132+
<!-- END BADGE -->

0 commit comments

Comments
 (0)