-
Notifications
You must be signed in to change notification settings - Fork 0
118 lines (102 loc) · 4.25 KB
/
aws-infrastructure.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Setup Infrastructure on Amazon AWS
on: workflow_dispatch
env:
AWS_REGION: us-east-1 # set this to your preferred AWS region, e.g. us-west-1
permissions:
contents: read
jobs:
terraform:
name: Create AWS ressources
runs-on: ubuntu-latest
environment: aws
outputs:
server-ip: ${{ steps.terraform-apply.outputs.server-ip }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Setup Terraform CLI
uses: hashicorp/[email protected]
- name: Terraform plan and apply
id: terraform-apply
working-directory: terraform
shell: bash
run: |
bash deploy.sh
echo "server-ip=$(sh scripts/get_public_ip.sh ubuntu2404)" >> $GITHUB_OUTPUT
kamal:
name: Bootstrap Kamal on AWS EC2 instance
needs: terraform
runs-on: ubuntu-latest
environment: aws
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.AWS_SSH_PRIVATE_KEY }}
- name: Set up Ruby for Kamal
uses: ruby/setup-ruby@v1
env:
BUNDLE_GEMFILE: ./kamal/Gemfile
with:
ruby-version: 3.2.2
bundler-cache: true
- name: Expose GitHub environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "( . // {} ) | to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV
- name: Bootstrap Kamal
working-directory: kamal
env:
KAMAL_SERVER_IP: ${{ needs.terraform.outputs.server-ip }}
KAMAL_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
KAMAL_REGISTRY_PASSWORD: ${{ steps.login-ecr.outputs[format('docker_password_{0}_dkr_ecr_us_east_1_amazonaws_com', secrets.AWS_ACCOUNT_ID)] }}
run: |
# Ensures that all Servers have docker installed
bundle exec kamal server bootstrap
# Push the environment variables to the servers
bundle exec kamal env push
# INFO: Uncomment to add mysql to the server
# Also add all envs defiend in the deploy.yml as GitHub Environment vars.
# - name: Boot Mysql
# working-directory: kamal
# env:
# KAMAL_SERVER_IP: ${{ env.SERVER_IP }}
# KAMAL_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# KAMAL_REGISTRY_PASSWORD: ${{ steps.login-ecr.outputs[format('docker_password_{0}_dkr_ecr_us_east_1_amazonaws_com', secrets.AWS_ACCOUNT_ID)] }}
# run: |
# if [ $(bundle exec kamal accessory details mysql | grep -c "Accessory mysql") -eq 1 ]
# then
# echo "Mysql already booted."
# bundle exec kamal accessory restart mysql
# else
# bundle exec kamal accessory boot mysql
# fi