Skip to content

Commit d7bf7df

Browse files
committed
🚀 Implement deployment workflow for Prometheus
1 parent a68f033 commit d7bf7df

8 files changed

Lines changed: 249 additions & 5 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Build and Deploy Prometheus
2+
3+
on:
4+
push:
5+
branches: main
6+
7+
env:
8+
AWS_REGION: ap-northeast-2
9+
ECR_REPOSITORY: beforegoing-prometheus
10+
S3_BUCKET_NAME: codedeploy-artifact-prometheus-beforegoingdapne2
11+
APPLICATION_NAME: codedeploy-app-prometheus-beforegoingdapne2
12+
DEPLOYMENT_GROUP_NAME: codedeploy-group-prometheus-beforegoingdapne2
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build-and-deploy:
19+
runs-on: ubuntu-latest
20+
defaults:
21+
run:
22+
working-directory: prometheus
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Configure AWS credentials
29+
uses: aws-actions/configure-aws-credentials@v4
30+
with:
31+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
aws-region: ${{ env.AWS_REGION }}
34+
35+
- name: Login to Amazon ECR
36+
id: login-ecr
37+
uses: aws-actions/amazon-ecr-login@v2
38+
39+
- name: Build, tag, and push image to Amazon ECR
40+
env:
41+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
42+
IMAGE_TAG: ${{ github.sha }}
43+
run: |
44+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
45+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
46+
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
47+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
48+
49+
- name: Create deployment package
50+
run: |
51+
zip -r ../deployment-${{ github.sha }}.zip .
52+
53+
- name: Upload deployment package to S3
54+
run: |
55+
aws s3 cp ../deployment-${{ github.sha }}.zip s3://$S3_BUCKET_NAME/deployment-${{ github.sha }}.zip
56+
57+
- name: Create CodeDeploy deployment
58+
run: |
59+
aws deploy create-deployment \
60+
--application-name $APPLICATION_NAME \
61+
--deployment-group-name $DEPLOYMENT_GROUP_NAME \
62+
--description "Deployment from GitHub Actions - commit ${{ github.sha }}" \
63+
--s3-location bucket=$S3_BUCKET_NAME,key=deployment-${{ github.sha }}.zip,bundleType=zip

‎prometheus/Dockerfile‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
FROM prom/prometheus:latest
22

3-
COPY ./prometheus.yml /etc/prometheus/prometheus.yml
4-
53
EXPOSE 9090
64

7-
CMD [ "--config.file=/etc/prometheus/prometheus.yml" ]
5+
CMD [ "--config.file=/etc/prometheus/prometheus.yml" ]

‎prometheus/appspec.yml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: 0.0
2+
os: linux
3+
files:
4+
- source: /
5+
destination: /opt/prometheus
6+
overwrite: yes
7+
8+
hooks:
9+
BeforeInstall:
10+
- location: scripts/before_install.sh
11+
timeout: 300
12+
runas: root
13+
14+
ApplicationStop:
15+
- location: scripts/stop_application.sh
16+
timeout: 300
17+
runas: root
18+
19+
ApplicationStart:
20+
- location: scripts/start_application.sh
21+
timeout: 300
22+
runas: root
23+
24+
ValidateService:
25+
- location: scripts/validate_service.sh
26+
timeout: 300
27+
runas: root

‎prometheus/prometheus.yml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ scrape_configs:
1313
ec2_sd_configs:
1414
- region: ap-northeast-2
1515
port: 80
16-
access_key:
17-
secret_key:
1816
filters:
1917
- name: tag:Monitoring
2018
values:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
export AWS_DEFAULT_REGION=ap-northeast-2
5+
export ECR_REGISTRY="116541189059.dkr.ecr.ap-northeast-2.amazonaws.com"
6+
export APP_NAME="prometheus"
7+
8+
echo "Preparing deployment environment..."
9+
10+
echo "Starting Docker service..."
11+
if ! systemctl is-active --quiet docker; then
12+
systemctl start docker
13+
fi
14+
systemctl enable docker
15+
16+
echo "Waiting for Docker to be ready..."
17+
timeout 30 bash -c 'until docker info >/dev/null 2>&1; do sleep 1; done'
18+
19+
echo "Logging into ECR..."
20+
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
21+
22+
echo "Cleaning up existing containers..."
23+
if [ "$(docker ps -aq)" ]; then
24+
docker stop $(docker ps -aq) 2>/dev/null || true
25+
docker rm $(docker ps -aq) 2>/dev/null || true
26+
fi
27+
28+
echo "Cleaning up old Docker images..."
29+
docker image prune -f
30+
docker system prune -f --volumes
31+
32+
echo "Creating application directories..."
33+
mkdir -p /opt/$APP_NAME/data
34+
35+
echo "Setting permissions for Prometheus data directory..."
36+
# The official Prometheus image runs as the 'nobody' user (UID 65534) which needs write access.
37+
chown 65534:65534 /opt/$APP_NAME/data
38+
39+
echo "Environment prepared successfully"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
export AWS_REGION="ap-northeast-2"
5+
export ECR_REGISTRY="116541189059.dkr.ecr.ap-northeast-2.amazonaws.com"
6+
export REPOSITORY_NAME="beforegoing-prometheus"
7+
export IMAGE_TAG="latest"
8+
export CONTAINER_NAME="prometheus"
9+
export APP_PORT=9090
10+
11+
echo "Starting Prometheus application container..."
12+
13+
echo "Pulling latest image: $ECR_REGISTRY/$REPOSITORY_NAME:$IMAGE_TAG"
14+
docker pull $ECR_REGISTRY/$REPOSITORY_NAME:$IMAGE_TAG
15+
16+
echo "Starting new container: $CONTAINER_NAME"
17+
docker run -d \
18+
--name $CONTAINER_NAME \
19+
--restart unless-stopped \
20+
-p $APP_PORT:$APP_PORT \
21+
-v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
22+
-v /opt/prometheus/data:/prometheus \
23+
-e TZ=Asia/Seoul \
24+
$ECR_REGISTRY/$REPOSITORY_NAME:$IMAGE_TAG
25+
26+
echo "Checking container status..."
27+
for i in {1..30}; do
28+
if docker ps --filter name=$CONTAINER_NAME --filter status=running -q | grep -q .; then
29+
echo "Container started successfully after $i attempts"
30+
break
31+
elif [ $i -eq 30 ]; then
32+
echo "Container failed to start after 30 attempts"
33+
docker logs $CONTAINER_NAME
34+
exit 1
35+
else
36+
echo "Attempt $i/30: Container not ready yet, waiting..."
37+
sleep 3
38+
fi
39+
done
40+
41+
echo "Container startup completed successfully!"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
export CONTAINER_NAME="prometheus"
5+
export GRACEFUL_TIMEOUT=30
6+
export FORCE_TIMEOUT=10
7+
8+
echo "Stopping Prometheus application container..."
9+
10+
check_container_status() {
11+
if docker ps -q --filter name="^$CONTAINER_NAME$" | grep -q .; then
12+
return 0
13+
else
14+
return 1
15+
fi
16+
}
17+
18+
if ! docker ps -a -q --filter name="^$CONTAINER_NAME$" | grep -q .; then
19+
echo "Container $CONTAINER_NAME not found"
20+
exit 0
21+
fi
22+
23+
if check_container_status; then
24+
echo "Stopping container gracefully (timeout: ${GRACEFUL_TIMEOUT}s)..."
25+
26+
echo "Last few log lines before shutdown:"
27+
docker logs --tail 5 $CONTAINER_NAME || true
28+
29+
docker stop $CONTAINER_NAME --time=$GRACEFUL_TIMEOUT
30+
31+
if check_container_status; then
32+
echo "Container still running, forcing stop..."
33+
timeout $FORCE_TIMEOUT docker kill $CONTAINER_NAME || true
34+
fi
35+
36+
echo "Container $CONTAINER_NAME stopped successfully"
37+
else
38+
echo "Container $CONTAINER_NAME is already stopped"
39+
fi
40+
41+
echo "Removing container: $CONTAINER_NAME"
42+
docker rm $CONTAINER_NAME || true
43+
44+
echo "Container cleanup completed"
45+
46+
echo "Cleaning up unused Docker resources..."
47+
docker system prune -f --volumes 2>/dev/null || true
48+
49+
echo "Application stop process completed successfully"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
HEALTH_CHECK_PORT=9090
5+
HEALTH_CHECK_URL="http://localhost:$HEALTH_CHECK_PORT/-/healthy"
6+
MAX_RETRIES=30
7+
SLEEP_SECONDS=5
8+
9+
echo "Performing health check on $HEALTH_CHECK_URL..."
10+
11+
for i in $(seq 1 $MAX_RETRIES); do
12+
echo "Attempt $i/$MAX_RETRIES..."
13+
14+
# Use curl to check connectivity and HTTP status in one go.
15+
# It will return "000" if it can't connect.
16+
HEALTH_STATUS=$(curl --silent --output /dev/null --write-out "%{http_code}" --max-time $SLEEP_SECONDS $HEALTH_CHECK_URL)
17+
18+
if [ "$HEALTH_STATUS" -eq 200 ]; then
19+
echo "Health check successful. Prometheus is healthy."
20+
exit 0
21+
fi
22+
23+
echo "Health check failed with HTTP status: $HEALTH_STATUS. Retrying in $SLEEP_SECONDS seconds..."
24+
sleep $SLEEP_SECONDS
25+
done
26+
27+
echo "Health check failed after $MAX_RETRIES attempts."
28+
echo "Please check the container logs for errors: docker logs prometheus"
29+
exit 1

0 commit comments

Comments
 (0)