Fix for deploy-ec2.yml #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ci-deploy-ec2 | |
on: | |
push: | |
branches: [ gopi-dev ] | |
permissions: | |
id-token: write | |
contents: read | |
env: | |
AWS_REGION: ${{ vars.AWS_REGION || 'us-east-1' }} | |
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY || 'careerforge' }} | |
EC2_INSTANCE_ID: ${{ vars.EC2_INSTANCE_ID }} | |
APP_PORT: "8080" | |
SERVICE_NAME: "careerforge" | |
jobs: | |
build-push-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: temurin | |
java-version: '21' | |
cache: maven | |
- name: Unit tests | |
working-directory: code/backend | |
run: mvn -B -ntp test | |
- name: Configure AWS credentials (OIDC) | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }} | |
aws-region: ${{ env.AWS_REGION }} | |
- run: aws sts get-caller-identity | |
- name: Ensure ECR repo exists | |
run: | | |
aws ecr describe-repositories --repository-names "${{ env.ECR_REPOSITORY }}" >/dev/null 2>&1 || \ | |
aws ecr create-repository --repository-name "${{ env.ECR_REPOSITORY }}" | |
- name: Login to ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Compute image tag | |
id: vars | |
run: | | |
echo "image=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push image | |
uses: docker/build-push-action@v6 | |
with: | |
context: code/backend | |
file: code/backend/Dockerfile | |
push: true | |
tags: ${{ steps.vars.outputs.image }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Deploy on EC2 via SSM (robust, POSIX) | |
env: | |
IMAGE: ${{ steps.vars.outputs.image }} | |
run: | | |
set -euo pipefail | |
# 1) Write a POSIX /bin/sh script locally with your values embedded | |
cat > deploy.sh <<EOF | |
#!/bin/sh | |
set -eu | |
# Install docker + deps (Ubuntu or Amazon Linux) | |
if command -v apt-get >/dev/null 2>&1; then | |
export DEBIAN_FRONTEND=noninteractive | |
apt-get update -y | |
apt-get install -y docker.io awscli curl | |
elif command -v dnf >/dev/null 2>&1; then | |
dnf -y update || true | |
dnf -y install docker awscli curl || yum -y install docker awscli curl | |
elif command -v yum >/dev/null 2>&1; then | |
yum -y update || true | |
yum -y install docker awscli curl | |
else | |
echo "no supported package manager found"; exit 1 | |
fi | |
# Ensure docker is running | |
systemctl enable --now docker || true | |
# ECR login | |
ACCOUNT_ID=\$(aws sts get-caller-identity --query Account --output text) | |
REGION='${AWS_REGION}' | |
REGISTRY=\${ACCOUNT_ID}.dkr.ecr.\${REGION}.amazonaws.com | |
aws ecr get-login-password --region "\${REGION}" | docker login --username AWS --password-stdin "\${REGISTRY}" | |
# Pull and run on host port 80 -> container 8080 | |
docker rm -f ${SERVICE_NAME} || true | |
docker pull ${IMAGE} | |
docker run -d --restart unless-stopped --name ${SERVICE_NAME} -p 80:8080 ${IMAGE} | |
# Evidence for the CI logs | |
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Ports}}' | |
curl -sS -o /dev/null -w '%{http_code}\n' http://localhost/hello | |
EOF | |
chmod +x deploy.sh | |
# 2) Base64 the script (no line wraps) for safe transport | |
B64=$(base64 -w0 deploy.sh || base64 deploy.sh | tr -d '\n') | |
# 3) Send to SSM as two simple commands: decode to file, run it | |
CMD_ID=$(aws ssm send-command \ | |
--instance-ids "$EC2_INSTANCE_ID" \ | |
--document-name "AWS-RunShellScript" \ | |
--comment "Deploy ${SERVICE_NAME}" \ | |
--parameters "commands=[\"echo $B64 | base64 -d > /tmp/deploy.sh\",\"sudo sh /tmp/deploy.sh\"]" \ | |
--query "Command.CommandId" --output text) | |
echo "CommandId: $CMD_ID" | |
# 4) Wait and print outputs | |
for i in $(seq 1 30); do | |
STATUS=$(aws ssm get-command-invocation \ | |
--command-id "$CMD_ID" \ | |
--instance-id "$EC2_INSTANCE_ID" \ | |
--query "Status" --output text) | |
echo "SSM status: $STATUS" | |
case "$STATUS" in | |
Success) break ;; | |
Cancelled|Failed|TimedOut) break ;; | |
esac | |
sleep 5 | |
done | |
aws ssm get-command-invocation \ | |
--command-id "$CMD_ID" \ | |
--instance-id "$EC2_INSTANCE_ID" \ | |
--query "{Status:Status, StdOut:StandardOutputContent, StdErr:StandardErrorContent}" \ | |
--output text | |
[ "$STATUS" = "Success" ] |