Skip to content

Commit 0333da6

Browse files
authored
Merge pull request #198 from devforth/next
Next
2 parents 006e51b + 3a6227f commit 0333da6

File tree

15 files changed

+1177
-169
lines changed

15 files changed

+1177
-169
lines changed

adminforth/commands/createApp/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ async function installDependencies(ctx, cwd) {
241241
const customDir = ctx.customDir;
242242

243243
await Promise.all([
244-
await execa('npm', ['install', '--no-package-lock'], { cwd }),
244+
await execa('npm', ['install'], { cwd }),
245245
await execa('npm', ['install'], { cwd: customDir }),
246246
]);
247247
}

adminforth/documentation/blog/2024-08-05-chatgpt/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Here is how it looks in action:
1515

1616
<!-- truncate -->
1717

18-
## Simple controls
18+
# Simple controls
1919

2020
To control plugin we use our open-source [vue-suggestion-input](https://github.com/devforth/vue-suggestion-input).
2121
It allows to:

adminforth/documentation/blog/2024-11-14-compose-ec2-deployment-ci/index.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ Create file `Dockerfile` in `myadmin`:
2222

2323
```Dockerfile title="./myadmin/Dockerfile"
2424
# use the same node version which you used during dev
25-
FROM node:20-alpine
25+
FROM node:22-alpine
2626
WORKDIR /code/
2727
ADD package.json package-lock.json /code/
2828
RUN npm ci
2929
ADD . /code/
3030
RUN --mount=type=cache,target=/tmp npx tsx bundleNow.ts
31-
CMD ["npm", "run", "startLive"]
31+
CMD ["sh", "-c", "npm run migrate:prod && npm run prod"]
3232
```
3333

34-
3534
## Step 2 - compose.yml
3635

37-
create folder `deploy` and create file `compose.yml` inside:
36+
Create folder `deploy` and create file `compose.yml` inside:
3837

3938
```yml title="deploy/compose.yml"
4039

@@ -51,10 +50,10 @@ services:
5150
- "/var/run/docker.sock:/var/run/docker.sock:ro"
5251

5352
myadmin:
54-
build: ./myadmin
53+
build: ../myadmin
5554
restart: always
5655
env_file:
57-
- ./myadmin/.env
56+
- ./myadmin/.env.secrets.prod
5857
volumes:
5958
- myadmin-db:/code/db
6059
labels:
@@ -88,12 +87,12 @@ Create `deploy/.gitignore` file with next content:
8887
*.tfstate.*
8988
*.tfvars
9089
tfplan
90+
.env.secrets.prod
9191
```
9292

9393
## Step 5 - Main terraform file main.tf
9494

9595

96-
9796
First of all install Terraform as described here [terraform installation](https://developer.hashicorp.com/terraform/install#linux).
9897

9998

@@ -102,7 +101,7 @@ Create file `main.tf` in `deploy` folder:
102101
```hcl title="deploy/main.tf"
103102
104103
locals {
105-
app_name = "<your_app_name>"
104+
app_name = "<your_app_name>" # replace with your app name
106105
aws_region = "eu-central-1"
107106
}
108107
@@ -126,10 +125,10 @@ data "aws_vpc" "default" {
126125
default = true
127126
}
128127
129-
130128
resource "aws_eip" "eip" {
131129
domain = "vpc"
132130
}
131+
133132
resource "aws_eip_association" "eip_assoc" {
134133
instance_id = aws_instance.app_instance.id
135134
allocation_id = aws_eip.eip.id
@@ -230,13 +229,34 @@ resource "aws_instance" "app_instance" {
230229
systemctl start docker
231230
systemctl enable docker
232231
usermod -a -G docker ubuntu
232+
233+
echo "done" > /home/ubuntu/user_data_done
233234
EOF
234235
235236
tags = {
236237
Name = "${local.app_name}-instance"
237238
}
238239
}
239240
241+
resource "null_resource" "wait_for_user_data" {
242+
provisioner "remote-exec" {
243+
inline = [
244+
"echo 'Waiting for EC2 software install to finish...'",
245+
"while [ ! -f /home/ubuntu/user_data_done ]; do echo '...'; sleep 2; done",
246+
"echo 'EC2 software install finished.'"
247+
]
248+
249+
connection {
250+
type = "ssh"
251+
user = "ubuntu"
252+
private_key = file("./.keys/id_rsa")
253+
host = aws_eip_association.eip_assoc.public_ip
254+
}
255+
}
256+
257+
depends_on = [aws_instance.app_instance]
258+
}
259+
240260
resource "null_resource" "sync_files_and_run" {
241261
# Use rsync to exclude node_modules, .git, db
242262
provisioner "local-exec" {
@@ -262,16 +282,12 @@ resource "null_resource" "sync_files_and_run" {
262282
# Run docker compose after files have been copied
263283
provisioner "remote-exec" {
264284
inline = [
265-
# fail bash specially and intentionally to stop the script on error
266-
"bash -c 'while ! command -v docker &> /dev/null; do echo \"Waiting for Docker to be installed...\"; sleep 1; done'",
267-
"bash -c 'while ! docker info &> /dev/null; do echo \"Waiting for Docker to start...\"; sleep 1; done'",
268-
269285
# please note that prune might destroy build cache and make build slower, however it releases disk space
270286
"docker system prune -f",
271287
# "docker buildx prune -f --filter 'type!=exec.cachemount'",
272288
"cd /home/ubuntu/app/deploy",
273289
# COMPOSE_FORCE_NO_TTY is needed to run docker compose in non-interactive mode and prevent stdout mess up
274-
"COMPOSE_FORCE_NO_TTY=1 docker compose -p app -f compose.yml up --build -d"
290+
"COMPOSE_BAKE=true docker compose --progress=plain -p app -f compose.yml up --build -d"
275291
]
276292
277293
connection {
@@ -287,7 +303,7 @@ resource "null_resource" "sync_files_and_run" {
287303
always_run = timestamp()
288304
}
289305
290-
depends_on = [aws_instance.app_instance, aws_eip_association.eip_assoc]
306+
depends_on = [null_resource.wait_for_user_data, aws_eip_association.eip_assoc]
291307
}
292308
293309
@@ -310,6 +326,10 @@ resource "aws_s3_bucket_lifecycle_configuration" "terraform_state" {
310326
status = "Enabled"
311327
id = "Keep only the latest version of the state file"
312328
329+
filter {
330+
prefix = ""
331+
}
332+
313333
noncurrent_version_expiration {
314334
noncurrent_days = 30
315335
}
@@ -333,8 +353,6 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "terraform_state"
333353
}
334354
}
335355
}
336-
337-
338356
```
339357

340358
> 👆 Replace `<your_app_name>` with your app name (no spaces, only underscores or letters)
@@ -424,6 +442,11 @@ jobs:
424442
with:
425443
terraform_version: 1.10.1
426444
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
445+
- name: Prepare env
446+
run: |
447+
echo "ADMINFORTH_SECRET=$VAULT_ADMINFORTH_SECRET" > deploy/.env.secrets.prod
448+
env:
449+
VAULT_ADMINFORTH_SECRET: ${{ secrets.VAULT_ADMINFORTH_SECRET }}
427450
- name: Start building
428451
env:
429452
VAULT_AWS_ACCESS_KEY_ID: ${{ secrets.VAULT_AWS_ACCESS_KEY_ID }}
@@ -477,6 +500,6 @@ Go to your GitHub repository, then `Settings` -> `Secrets` -> `New repository se
477500
- `VAULT_AWS_SECRET_ACCESS_KEY` - your AWS secret key
478501
- `VAULT_SSH_PRIVATE_KEY` - make `cat ~/.ssh/id_rsa` and paste to GitHub secrets
479502
- `VAULT_SSH_PUBLIC_KEY` - make `cat ~/.ssh/id_rsa.pub` and paste to GitHub secrets
480-
503+
- `VAULT_ADMINFORTH_SECRET` - your AdminForth secret - random string, for example `openssl rand -base64 32 | tr -d '\n'`
481504

482505
Now you can push your changes to GitHub and see how it will be deployed automatically.

0 commit comments

Comments
 (0)