Skip to content

Commit 542a1de

Browse files
committed
Update GHA deploy
1 parent 3f28436 commit 542a1de

3 files changed

Lines changed: 94 additions & 171 deletions

File tree

.github/workflows/deploy-ec2.yml

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

.github/workflows/deploy.yml

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,114 @@
1-
name: Deploy stryng
2-
run-name: ${{ github.event_name == 'workflow_dispatch' && format('[{1}] - {0}', github.workflow, github.event.inputs.environment) || format('[uat] - {0}', github.event.head_commit.message) }}
1+
name: Deploy to EC2
2+
3+
run-name: ${{ github.event_name == 'workflow_dispatch' && format('[{0}] - {1}', github.event.inputs.environment, github.workflow) || format('[uat] - {0}', github.event.head_commit.message) }}
34

45
on:
5-
# push:
6-
# branches:
7-
# - main
6+
push:
7+
branches:
8+
- main
89
workflow_dispatch:
910
inputs:
1011
environment:
1112
description: 'Environment'
1213
type: choice
1314
options:
14-
- uat
15-
- prod
15+
- uat
16+
- prod
1617
required: true
1718

1819
jobs:
1920
deploy:
2021
runs-on: ubuntu-latest
22+
environment: ${{ github.event.inputs.environment || 'uat' }}
2123
concurrency:
22-
group: deploy-stryng-${{ github.event_name }}
23-
24-
container:
25-
image: modul8it/stryng_devops_devcontainer:latest
26-
options: --user 0:0
27-
credentials:
28-
username: ${{ secrets.DOCKERHUB_USERNAME }}
29-
password: ${{ secrets.DOCKERHUB_TOKEN }}
24+
group: deploy-ec2-${{ github.event.inputs.environment || 'uat' }}
25+
cancel-in-progress: false
3026

3127
env:
32-
HOME: /home/coder
33-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
34-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
35-
AWS_REGION: ${{ secrets.AWS_REGION }}
28+
TARGET_ENV: ${{ github.event.inputs.environment || 'uat' }}
3629

3730
steps:
38-
- name: Prepare env (inside container)
39-
shell: bash
40-
run: |
41-
set -euo pipefail
42-
if [[ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]]; then
43-
TARGET_ENV="${{ github.event.inputs.environment }}"
44-
else
45-
TARGET_ENV="uat"
46-
fi
47-
echo "TARGET_ENV=$TARGET_ENV" >> "$GITHUB_ENV"
48-
echo "TARGET_ENV_UPPERCASE=${TARGET_ENV^^}" >> "$GITHUB_ENV"
49-
echo "TARGET_ENV=$TARGET_ENV"
50-
echo "TARGET_ENV_UPPERCASE=${TARGET_ENV^^}"
51-
52-
- name: Checkout devops
31+
- name: Checkout repository
5332
uses: actions/checkout@v4
33+
34+
- name: Configure AWS credentials
35+
uses: aws-actions/configure-aws-credentials@v4
36+
with:
37+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
38+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
39+
aws-region: ${{ secrets.AWS_REGION }}
40+
41+
- name: Export secrets from AWS Secrets Manager
42+
uses: say8425/aws-secrets-manager-actions@v2
5443
with:
55-
repository: modul8dev/devops
56-
token: ${{ secrets.GH_TOKEN }}
57-
path: devops
58-
ref: main
44+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
45+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
46+
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
47+
SECRET_NAME: ${{ vars.AWS_SECRET_NAME }}
48+
OUTPUT_PATH: '.env'
49+
50+
- name: List files for debugging
51+
run: ls -la && cat .env
52+
53+
- name: Install jinjanator
54+
run: pip install jinjanator
5955

60-
- name: Deploy (inside container)
61-
shell: bash
56+
- name: Template nginx configuration
6257
run: |
63-
set -eux
64-
whoami
65-
echo "TARGET_ENV=$TARGET_ENV"
66-
echo "TARGET_ENV_UPPERCASE=$TARGET_ENV_UPPERCASE"
67-
68-
mkdir -p "$HOME/.ssh"
69-
echo "${{ secrets[format('SSH_PRIVATE_KEY_{0}', env.TARGET_ENV_UPPERCASE)] }}" > "$HOME/.ssh/m8_admin"
70-
chmod 600 "$HOME/.ssh/m8_admin"
71-
72-
cd devops/ansible
73-
ansible-playbook \
74-
-i "inventories/aws/$TARGET_ENV" \
75-
-u ubuntu \
76-
--private-key "$HOME/.ssh/m8_admin" \
77-
--tags install \
78-
deploy-stryng5.yml \
79-
-e "git_ref=${{ github.ref_name }}"
58+
jinjanate deploy/nginx/nginx.conf.j2 .env --format=env -o deploy/nginx/nginx.conf
59+
jinjanate deploy/docker-compose.yml.j2 .env --format=env -o deploy/docker-compose.yml
60+
61+
- name: Copy files to EC2
62+
uses: appleboy/scp-action@v1
63+
with:
64+
host: ${{ secrets.SSH_HOST }}
65+
username: ${{ secrets.SSH_USERNAME }}
66+
key: ${{ secrets.SSH_PRIVATE_KEY }}
67+
source: ".env,deploy/nginx/nginx.conf,deploy/docker-compose.yml"
68+
target: "~/repo/scp/"
69+
70+
- name: Deploy on EC2
71+
uses: appleboy/ssh-action@v1
72+
env:
73+
GITHUB_SSH_KEY: ${{ secrets.M8_GIHUB_SSH_KEY }}
74+
with:
75+
host: ${{ secrets.SSH_HOST }}
76+
username: ${{ secrets.SSH_USERNAME }}
77+
key: ${{ secrets.SSH_PRIVATE_KEY }}
78+
envs: GITHUB_SSH_KEY
79+
script: |
80+
set -euo pipefail
81+
82+
# Ensure GitHub SSH key is in place
83+
mkdir -p ~/.ssh
84+
echo "$GITHUB_SSH_KEY" > ~/.ssh/modul8_github_ssh_key
85+
chmod 600 ~/.ssh/modul8_github_ssh_key
86+
87+
# Start ssh-agent and add the GitHub SSH key
88+
if [ -z "${SSH_AUTH_SOCK:-}" ] || [ ! -S "${SSH_AUTH_SOCK:-}" ]; then
89+
eval "$(ssh-agent -s)"
90+
fi
91+
ssh-add ~/.ssh/modul8_github_ssh_key
92+
93+
mkdir -p ~/repo
94+
95+
# Clone or pull the latest code
96+
if [ ! -d ~/repo/stryng5 ]; then
97+
# Clone directly into the folder
98+
git clone git@github.com:modul8dev/stryng5.git ~/repo/stryng5
99+
else
100+
# Pull updates
101+
git -C ~/repo/stryng5 pull
102+
fi
103+
104+
cd ~/repo/stryng5
105+
106+
# Put rendered files in place
107+
mv ~/repo/scp/deploy/nginx/nginx.conf deploy/nginx/nginx.conf
108+
mv ~/repo/scp/deploy/docker-compose.yml deploy/docker-compose.yml
109+
mv ~/repo/scp/.env .env
110+
111+
# Pull latest images and (re)start services
112+
cd deploy
113+
docker compose up -d
114+
docker compose up -d --force-recreate stryng_app qcluster

deploy/docker-compose.yml.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ services:
4747
depends_on:
4848
- redis
4949
restart: unless-stopped
50+
networks:
51+
- stryng
5052

5153
nginx:
5254
image: nginx

0 commit comments

Comments
 (0)