Skip to content

Commit 1fe7b4a

Browse files
fix: missing workflow file
1 parent 2808277 commit 1fe7b4a

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Terraform Deploy
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
aws_access_key_id:
7+
description: "AWS Access Key ID from Learner Lab"
8+
required: true
9+
type: string
10+
aws_secret_access_key:
11+
description: "AWS Secret Access Key from Learner Lab"
12+
required: true
13+
type: string
14+
aws_session_token:
15+
description: "AWS Session Token from Learner Lab"
16+
required: true
17+
type: string
18+
19+
jobs:
20+
deploy:
21+
runs-on: ubuntu-latest
22+
environment:
23+
name: AWS
24+
url: "https://api.${{ steps.terraform.outputs.public_ip }}.nip.io"
25+
26+
steps:
27+
- name: Checkout Code
28+
uses: actions/checkout@v4
29+
30+
- name: Setup Terraform
31+
uses: hashicorp/setup-terraform@v2
32+
33+
- name: Terraform Init
34+
run: |
35+
cd infrastructure/terraform
36+
terraform init
37+
38+
- name: Terraform Apply
39+
id: terraform
40+
run: |
41+
cd infrastructure/terraform
42+
terraform apply -auto-approve
43+
IP=$(terraform output -raw public_ip | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -n1 | tr -d '\r\n')
44+
echo "public_ip=$IP" >> $GITHUB_OUTPUT
45+
echo "Extrahierte IP: $IP"
46+
env:
47+
TF_VAR_aws_access_key: ${{ github.event.inputs.aws_access_key_id }}
48+
TF_VAR_aws_secret_key: ${{ github.event.inputs.aws_secret_access_key }}
49+
TF_VAR_aws_session_token: ${{ github.event.inputs.aws_session_token }}
50+
51+
- name: Wait for instance to be ready
52+
run: |
53+
echo "Waiting for instance to be ready..."
54+
sleep 60
55+
echo "Instance should be ready now"
56+
57+
- name: Install Docker on EC2 instance
58+
uses: appleboy/[email protected]
59+
with:
60+
host: ${{ steps.terraform.outputs.public_ip }}
61+
username: ${{ vars.AWS_EC2_USER }}
62+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
63+
script: |
64+
# Add Docker's official GPG key
65+
sudo apt-get update
66+
sudo apt-get install -y ca-certificates curl
67+
sudo install -m 0755 -d /etc/apt/keyrings
68+
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
69+
sudo chmod a+r /etc/apt/keyrings/docker.asc
70+
71+
# Add the repository to Apt sources
72+
echo \
73+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
74+
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
75+
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
76+
77+
# Install Docker
78+
sudo apt-get update
79+
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
80+
81+
# Add user to docker group
82+
sudo usermod -a -G docker ${{ vars.AWS_EC2_USER }}
83+
84+
# Apply group changes without logout
85+
newgrp docker
86+
87+
- name: Copy Docker Compose File From Repo to VM Host
88+
uses: appleboy/[email protected]
89+
with:
90+
host: ${{ steps.terraform.outputs.public_ip }}
91+
username: ${{ vars.AWS_EC2_USER }}
92+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
93+
source: "deployment/compose.aws.yml"
94+
target: "/home/${{ vars.AWS_EC2_USER }}/deployment"
95+
strip_components: 1
96+
overwrite: true
97+
98+
- name: SSH to VM and Create .env.prod
99+
uses: appleboy/[email protected]
100+
with:
101+
host: ${{ steps.terraform.outputs.public_ip }}
102+
username: ${{ vars.AWS_EC2_USER }}
103+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
104+
script: |
105+
cd /home/${{ vars.AWS_EC2_USER }}
106+
rm -f .env.prod
107+
touch .env.prod
108+
echo "EC2_PUBLIC_IP=${{ steps.terraform.outputs.public_ip }}" >> .env.prod
109+
echo "CLIENT_HOST=client.${{ steps.terraform.outputs.public_ip }}.nip.io" >> .env.prod
110+
echo "SERVER_HOST=api.${{ steps.terraform.outputs.public_ip }}.nip.io" >> .env.prod
111+
echo "PUBLIC_API_URL=https://api.${{ steps.terraform.outputs.public_ip }}.nip.io" >> .env.prod
112+
113+
- name: SSH to VM and Execute Docker-Compose Up
114+
uses: appleboy/[email protected]
115+
with:
116+
host: ${{ steps.terraform.outputs.public_ip }}
117+
username: ${{ vars.AWS_EC2_USER }}
118+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
119+
script: |
120+
cd /home/${{ vars.AWS_EC2_USER }}
121+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
122+
docker compose -f deployment/compose.aws.yml --env-file=/home/${{ vars.AWS_EC2_USER }}/.env.prod up -d

0 commit comments

Comments
 (0)