-
Notifications
You must be signed in to change notification settings - Fork 1
150 lines (130 loc) · 4.66 KB
/
Copy pathbackend-react-dev-deploy.yml
File metadata and controls
150 lines (130 loc) · 4.66 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Roadmap: DEV-075
name: Backend React-dev deploy
on:
workflow_run:
workflows:
- Backend PostgreSQL CI
types:
- completed
branches:
- master
permissions:
contents: read
concurrency:
group: backend-react-dev-deploy
cancel-in-progress: false
jobs:
deploy:
name: Deploy backend to React-dev
if: >-
${{
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'master' &&
github.event.workflow_run.head_repository.full_name == github.repository &&
github.event.workflow_run.head_sha != ''
}}
runs-on: ubuntu-latest
timeout-minutes: 30
env:
DEPLOY_SHA: ${{ github.event.workflow_run.head_sha }}
DEPLOY_RUN_ID: ${{ github.run_id }}
steps:
- name: Validate deploy source
shell: bash
run: |
set -Eeuo pipefail
if [[ ! "$DEPLOY_SHA" =~ ^[0-9a-f]{40}$ ]]; then
echo "Successful CI run did not provide a valid commit SHA." >&2
exit 1
fi
if [[ ! "$DEPLOY_RUN_ID" =~ ^[0-9]+$ ]]; then
echo "GitHub Actions run ID is invalid." >&2
exit 1
fi
- name: Checkout tested commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 1
- name: Verify checked out commit
shell: bash
run: |
set -Eeuo pipefail
actual_sha="$(git rev-parse HEAD)"
if [[ "$actual_sha" != "$DEPLOY_SHA" ]]; then
echo "Checked out commit does not match the successful CI run." >&2
exit 1
fi
- name: Prepare pinned SSH configuration
shell: bash
env:
REACT_DEV_SSH_HOST: ${{ secrets.REACT_DEV_SSH_HOST }}
REACT_DEV_SSH_PORT: ${{ secrets.REACT_DEV_SSH_PORT }}
REACT_DEV_SSH_USER: ${{ secrets.REACT_DEV_SSH_USER }}
REACT_DEV_SSH_PRIVATE_KEY: ${{ secrets.REACT_DEV_SSH_PRIVATE_KEY }}
REACT_DEV_SSH_KNOWN_HOSTS: ${{ secrets.REACT_DEV_SSH_KNOWN_HOSTS }}
run: |
set -Eeuo pipefail
required_secrets=(
REACT_DEV_SSH_HOST
REACT_DEV_SSH_PORT
REACT_DEV_SSH_USER
REACT_DEV_SSH_PRIVATE_KEY
REACT_DEV_SSH_KNOWN_HOSTS
)
for secret_name in "${required_secrets[@]}"; do
if [[ -z "${!secret_name:-}" ]]; then
echo "Required GitHub Secret is empty: ${secret_name}" >&2
exit 1
fi
done
if [[ ! "$REACT_DEV_SSH_PORT" =~ ^[0-9]+$ ]] ||
((REACT_DEV_SSH_PORT < 1 || REACT_DEV_SSH_PORT > 65535)); then
echo "React-dev SSH port is invalid." >&2
exit 1
fi
if [[ ! "$REACT_DEV_SSH_USER" =~ ^[a-z_][a-z0-9_.-]*$ ]]; then
echo "React-dev SSH user is invalid." >&2
exit 1
fi
if [[ "$REACT_DEV_SSH_HOST" =~ [[:space:]] ]]; then
echo "React-dev SSH host is invalid." >&2
exit 1
fi
install -d -m 700 "$HOME/.ssh"
printf '%s\n' "$REACT_DEV_SSH_PRIVATE_KEY" |
tr -d '\r' > "$HOME/.ssh/react_dev_deploy_key"
printf '%s\n' "$REACT_DEV_SSH_KNOWN_HOSTS" |
tr -d '\r' > "$HOME/.ssh/react_dev_known_hosts"
chmod 600 \
"$HOME/.ssh/react_dev_deploy_key" \
"$HOME/.ssh/react_dev_known_hosts"
test -s "$HOME/.ssh/react_dev_deploy_key"
test -s "$HOME/.ssh/react_dev_known_hosts"
ssh-keygen -y -f "$HOME/.ssh/react_dev_deploy_key" >/dev/null
- name: Deploy tested commit to React-dev
shell: bash
env:
REACT_DEV_SSH_HOST: ${{ secrets.REACT_DEV_SSH_HOST }}
REACT_DEV_SSH_PORT: ${{ secrets.REACT_DEV_SSH_PORT }}
REACT_DEV_SSH_USER: ${{ secrets.REACT_DEV_SSH_USER }}
run: |
set -Eeuo pipefail
ssh_options=(
-i "$HOME/.ssh/react_dev_deploy_key"
-p "$REACT_DEV_SSH_PORT"
-o BatchMode=yes
-o IdentitiesOnly=yes
-o PasswordAuthentication=no
-o KbdInteractiveAuthentication=no
-o StrictHostKeyChecking=yes
-o UserKnownHostsFile="$HOME/.ssh/react_dev_known_hosts"
-o ConnectTimeout=10
-o ServerAliveInterval=15
-o ServerAliveCountMax=3
)
ssh "${ssh_options[@]}" \
-- "$REACT_DEV_SSH_USER@$REACT_DEV_SSH_HOST" \
"bash -s -- '$DEPLOY_SHA' '$DEPLOY_RUN_ID'" \
< scripts/deploy_react_dev.sh