Skip to content

Commit 6cabcdf

Browse files
committed
Fix for deploy-ec2.yml
1 parent 8ed0ab7 commit 6cabcdf

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

.github/workflows/deploy-ec2.yml

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,43 @@ jobs:
6666
cache-from: type=gha
6767
cache-to: type=gha,mode=max
6868

69-
# ---- Robust SSM deploy (POSIX /bin/sh; Ubuntu 24.04-safe) ----
69+
# ---- Final deploy step (fixes region expansion & non-TTY login; Ubuntu 24.04-safe) ----
7070
- name: Deploy on EC2 via SSM
7171
env:
7272
IMAGE: ${{ steps.vars.outputs.image }}
7373
run: |
7474
set -euo pipefail
75-
76-
# Write the remote script locally (heredoc), then base64 it to avoid YAML/JSON escaping issues.
75+
76+
# 1) Write a neutral script with placeholders so we can safely inject values
7777
cat > deploy.sh <<'EOF'
7878
#!/bin/sh
7979
set -eu
80-
81-
# -------- Install Docker (Ubuntu path uses docker.asc; no gpg --dearmor) --------
80+
81+
REGION="__REGION__"
82+
IMAGE="__IMAGE__"
83+
SERVICE_NAME="__SERVICE_NAME__"
84+
APP_PORT="__APP_PORT__"
85+
86+
# ---- Install Docker (Ubuntu path uses docker.asc; no gpg --dearmor / TTY) ----
8287
if command -v apt-get >/dev/null 2>&1; then
8388
export DEBIAN_FRONTEND=noninteractive
8489
apt-get update -y
85-
86-
# Remove conflicting Ubuntu packages to avoid "containerd.io : Conflicts: containerd"
90+
91+
# Remove conflicting Ubuntu docker/containerd packages (prevents "containerd.io : Conflicts: containerd")
8792
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
8893
apt-get remove -y "$pkg" || true
8994
done
90-
95+
9196
apt-get install -y ca-certificates curl unzip
9297
install -m 0755 -d /etc/apt/keyrings
9398
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
9499
chmod a+r /etc/apt/keyrings/docker.asc
95100
. /etc/os-release
96101
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $VERSION_CODENAME stable" > /etc/apt/sources.list.d/docker.list
97-
102+
98103
apt-get update -y
99104
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
100-
101-
# -------- Amazon Linux / RHEL family fallback --------
105+
102106
elif command -v dnf >/dev/null 2>&1; then
103107
dnf -y update || true
104108
dnf -y install docker curl unzip || yum -y install docker curl unzip
@@ -109,40 +113,44 @@ jobs:
109113
echo "no supported package manager found"
110114
exit 1
111115
fi
112-
116+
113117
systemctl enable --now docker || true
114-
115-
# -------- ECR login, pull latest, run on 80 -> ${APP_PORT} --------
118+
119+
# ---- ECR login (non-interactive per AWS docs) ----
116120
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
117-
REGION='${AWS_REGION}'
118121
REGISTRY="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com"
119-
122+
120123
aws ecr get-login-password --region "${REGION}" \
121124
| docker login --username AWS --password-stdin "${REGISTRY}"
122-
123-
docker rm -f ${SERVICE_NAME} || true
124-
docker pull ${IMAGE}
125-
docker run -d --restart unless-stopped --name ${SERVICE_NAME} -p 80:${APP_PORT} ${IMAGE}
126-
127-
# -------- Evidence / health check --------
125+
126+
# ---- Pull & run on host 80 -> container ${APP_PORT} ----
127+
docker rm -f "${SERVICE_NAME}" || true
128+
docker pull "${IMAGE}"
129+
docker run -d --restart unless-stopped --name "${SERVICE_NAME}" -p 80:"${APP_PORT}" "${IMAGE}"
130+
131+
# ---- Evidence / health check ----
128132
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Ports}}'
129133
curl -sS -o /dev/null -w '%{http_code}\n' http://localhost/hello
130134
EOF
131135
chmod +x deploy.sh
132-
133-
# Send to SSM (two simple commands) and execute
136+
137+
# 2) Inject real values (this avoids single-quoted heredoc expansion issues)
138+
sed -i "s|__REGION__|${{ env.AWS_REGION }}|g" deploy.sh
139+
sed -i "s|__IMAGE__|${{ steps.vars.outputs.image }}|g" deploy.sh
140+
sed -i "s|__SERVICE_NAME__|${{ env.SERVICE_NAME }}|g" deploy.sh
141+
sed -i "s|__APP_PORT__|${{ env.APP_PORT }}|g" deploy.sh
142+
143+
# 3) Send to SSM (no YAML/JSON quoting headaches) and execute
134144
B64=$(base64 -w0 deploy.sh || base64 deploy.sh | tr -d '\n')
135-
136145
CMD_ID=$(aws ssm send-command \
137146
--instance-ids "${EC2_INSTANCE_ID}" \
138147
--document-name "AWS-RunShellScript" \
139148
--comment "Deploy ${SERVICE_NAME}" \
140149
--parameters "commands=[\"echo ${B64} | base64 -d > /tmp/deploy.sh\",\"sudo sh /tmp/deploy.sh\"]" \
141150
--query "Command.CommandId" --output text)
142-
143151
echo "CommandId: ${CMD_ID}"
144-
145-
# Wait for completion and surface logs
152+
153+
# 4) Wait for completion and surface logs
146154
for i in $(seq 1 30); do
147155
STATUS=$(aws ssm get-command-invocation \
148156
--command-id "$CMD_ID" \
@@ -155,11 +163,11 @@ jobs:
155163
esac
156164
sleep 5
157165
done
158-
166+
159167
aws ssm get-command-invocation \
160168
--command-id "$CMD_ID" \
161169
--instance-id "${EC2_INSTANCE_ID}" \
162170
--query "{Status:Status, StdOut:StandardOutputContent, StdErr:StandardErrorContent}" \
163171
--output text
164-
172+
165173
[ "$STATUS" = "Success" ]

0 commit comments

Comments
 (0)