Skip to content

Fixed deploy-ec2.yml #2

Fixed deploy-ec2.yml

Fixed deploy-ec2.yml #2

Workflow file for this run

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 (Ubuntu/Amazon Linux compatible)
env:
IMAGE: ${{ steps.vars.outputs.image }}
run: |
set -euo pipefail
# 1) Send the command
CMD_ID=$(aws ssm send-command \
--instance-ids "$EC2_INSTANCE_ID" \
--document-name "AWS-RunShellScript" \
--comment "Deploy ${SERVICE_NAME}" \
--parameters commands='[
"set -euxo pipefail",
"# Detect package manager and install docker",
"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; \
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",
"# Enable & start docker",
"systemctl enable --now docker",
"# 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}",
"# (Re)start container 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}",
"# Show status and do a local health check",
"docker ps --format \"table {{.Names}}\\t{{.Image}}\\t{{.Ports}}\"",
"curl -sS -o /dev/null -w \"%{http_code}\\n\" http://localhost/hello"
]' \
--query "Command.CommandId" --output text)
echo "CommandId: $CMD_ID"
# 2) Wait for completion and surface logs
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"
if [ "$STATUS" = "Success" ]; then break; fi
if [ "$STATUS" = "Cancelled" ] || [ "$STATUS" = "Failed" ] || [ "$STATUS" = "TimedOut" ]; then break; fi
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
# Fail the job if SSM wasn’t successful
[ "$STATUS" = "Success" ]