Skip to content

Commit 31a52f0

Browse files
build image
1 parent a92be17 commit 31a52f0

File tree

12 files changed

+150
-60
lines changed

12 files changed

+150
-60
lines changed

.github/workflows/build.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Build & Push Docker Image
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch:
7+
8+
env:
9+
IMAGE_NAME: terraform-webhook
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Log in to GitHub Container Registry
20+
uses: docker/login-action@v3
21+
with:
22+
registry: ghcr.io
23+
username: ${{ github.actor }}
24+
password: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- name: Build and push image
27+
uses: docker/build-push-action@v6
28+
with:
29+
push: true
30+
tags: ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest

.gitignore

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1 @@
1-
2-
### Terraform ###
3-
# Local .terraform directories
4-
**/.terraform/*
5-
6-
# .tfstate files
7-
*.tfstate
8-
*.tfstate.*
9-
10-
# Crash log files
11-
crash.log
12-
crash.*.log
13-
14-
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
15-
# password, private keys, and other secrets. These should not be part of version
16-
# control as they are data points which are potentially sensitive and subject
17-
# to change depending on the environment.
18-
*.tfvars
19-
*.tfvars.json
20-
21-
# Ignore override files as they are usually used to override resources locally and so
22-
# are not checked in
23-
override.tf
24-
override.tf.json
25-
*_override.tf
26-
*_override.tf.json
27-
28-
# Include override files you do wish to add to version control using negated pattern
29-
# !example_override.tf
30-
31-
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
32-
# example: *tfplan*
33-
34-
# Ignore CLI configuration files
35-
.terraformrc
36-
terraform.rc
1+
compose.yaml

.terraform.lock.hcl

Lines changed: 0 additions & 24 deletions
This file was deleted.

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM python:3.12-alpine
2+
3+
ENV TERRAFORM_VERSION=1.12.2
4+
5+
RUN apk add --no-cache ca-certificates unzip wget \
6+
&& update-ca-certificates \
7+
&& wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
8+
&& unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
9+
&& mv terraform /usr/local/bin/terraform \
10+
&& rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
11+
&& pip install --no-cache-dir flask
12+
13+
COPY server.py /app/server.py
14+
COPY terraform/*.tf /app/terraform/
15+
COPY terraform/*.lock.hcl /app/terraform/
16+
17+
WORKDIR /app/terraform
18+
RUN terraform init
19+
20+
WORKDIR /app
21+
22+
EXPOSE 8080
23+
CMD ["python3", "server.py"]

server.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask, request, jsonify
2+
import subprocess
3+
import os
4+
5+
app = Flask(__name__)
6+
TERRAFORM_DIR = "/app/terraform"
7+
8+
9+
@app.route("/webhook", methods=["POST"])
10+
def webhook():
11+
secret = request.headers.get("X-Webhook-Token")
12+
if secret != os.environ.get("WEBHOOK_TOKEN", "changeme"):
13+
return jsonify({"status": "forbidden"}), 403
14+
15+
try:
16+
result = subprocess.run(
17+
["terraform", "apply", "-auto-approve"],
18+
cwd=TERRAFORM_DIR,
19+
capture_output=True,
20+
text=True,
21+
)
22+
return jsonify(
23+
{"status": "success", "stdout": result.stdout, "stderr": result.stderr}
24+
)
25+
except Exception as e:
26+
return jsonify({"status": "error", "message": str(e)}), 500
27+
28+
29+
@app.route("/", methods=["GET"])
30+
def health():
31+
return jsonify({"status": "running"})
32+
33+
34+
if __name__ == "__main__":
35+
app.run(host="0.0.0.0", port=8080)

terraform/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
### Terraform ###
3+
# Local .terraform directories
4+
**/.terraform/*
5+
6+
# .tfstate files
7+
*.tfstate
8+
*.tfstate.*
9+
10+
# Crash log files
11+
crash.log
12+
crash.*.log
13+
14+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
15+
# password, private keys, and other secrets. These should not be part of version
16+
# control as they are data points which are potentially sensitive and subject
17+
# to change depending on the environment.
18+
*.tfvars
19+
*.tfvars.json
20+
21+
# Ignore override files as they are usually used to override resources locally and so
22+
# are not checked in
23+
override.tf
24+
override.tf.json
25+
*_override.tf
26+
*_override.tf.json
27+
28+
# Include override files you do wish to add to version control using negated pattern
29+
# !example_override.tf
30+
31+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
32+
# example: *tfplan*
33+
34+
# Ignore CLI configuration files
35+
.terraformrc
36+
terraform.rc

terraform/.terraform.lock.hcl

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)