-
Notifications
You must be signed in to change notification settings - Fork 10
129 lines (117 loc) · 4.27 KB
/
deploy.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
119
120
121
122
123
124
125
126
127
128
129
name: 🚀 Deploy
on:
workflow_dispatch:
inputs:
branch_to_deploy:
type: string
description: "Branch name, Version number or Sha hash to deploy"
required: true
environment:
type: choice
description: "Environment to deploy to"
required: true
default: "qa"
options:
- prd
- qa
- staging
permissions:
actions: read
checks: read
contents: write
deployments: read
id-token: write
issues: read
discussions: read
packages: read
pages: read
pull-requests: read
repository-projects: read
security-events: read
statuses: read
run-name: 🚀 Deploy ${{ inputs.branch_to_deploy || github.ref_name }}
jobs:
# =====================================================
# Job: Manual Deployment
# =====================================================
deploy:
runs-on: ubuntu-latest
steps:
- name: ✅ Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure AWS credentials
id: aws_creds
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: eu-west-1
role-to-assume: ${{ secrets.RESOURCES_DEPLOY_ROLE }}
role-session-name: OIDCSession
mask-aws-account-id: true
output-credentials: true
- name: Deploy to remote server via SSH
uses: appleboy/ssh-action@v1.0.3
env:
AWS_ACCESS_KEY_ID: ${{ steps.aws_creds.outputs.aws-access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ steps.aws_creds.outputs.aws-secret-access-key }}
AWS_SESSION_TOKEN: ${{ steps.aws_creds.outputs.aws-session-token }}
AWS_ACCOUNT_ID: ${{ steps.aws_creds.outputs.aws-account-id }}
SHA: ${{ github.sha }}
DOCKER_TAG: ${{ inputs.branch_to_deploy }}
ENVIRONMENT: ${{ inputs.environment }}
with:
host: ${{ secrets.DEMO_ARKETYPE_HOST }}
username: ${{ secrets.DEMO_ARKETYPE_USER }}
key: ${{ secrets.ACTIONS_PRIVATE_SSH_KEY }}
envs: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
script: |
#!/bin/bash
set -x
echo "sha: ${{ env.SHA }}"
export AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}
export AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}
export AWS_SESSION_TOKEN=${{ env.AWS_SESSION_TOKEN }}
export AWS_SESSION_TOKEN=${{ env.AWS_SESSION_TOKEN }}
# Parameters
ENVIRONMENT=${{ inputs.environment }}
DOCKER_TAG=${{ inputs.branch_to_deploy }}
# Define port based on environment
case "$ENVIRONMENT" in
staging)
DOCKER_PORT=4000
;;
qa)
DOCKER_PORT=4001
;;
prd)
DOCKER_PORT=4002
;;
*)
echo "Unknown environment: $ENVIRONMENT"
exit 1
;;
esac
# Docker image URL
IMAGE_URL="${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.eu-west-1.amazonaws.com/venly-arketype-eu-west-1"
# Create the environment file for systemd
ENV_FILE="/etc/default/venly-arketype-${ENVIRONMENT}"
{
echo "DOCKER_IMAGE_URL=${IMAGE_URL}"
echo "DOCKER_TAG=${DOCKER_TAG}"
echo "PORT_MAPPING=127.0.0.1:${DOCKER_PORT}:4000"
} > $ENV_FILE
# Docker login
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.eu-west-1.amazonaws.com
# Prune Docker system
docker system prune -f
# Pull Docker image
docker pull $IMAGE_URL:$DOCKER_TAG
# Deployment
echo "Deploying $DOCKER_TAG"
SERVICE_NAME="venly-arketype@${ENVIRONMENT}.service"
sudo systemctl stop $SERVICE_NAME
sudo systemctl start $SERVICE_NAME
# Docker logout
docker logout ${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.eu-west-1.amazonaws.com
echo "Deployment to $ENVIRONMENT completed successfully"