Skip to content

Commit 98b9699

Browse files
workflow
1 parent 620a5a6 commit 98b9699

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

.github/workflows/main.yaml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: workflow
2+
3+
# Define when the workflow will run
4+
on:
5+
push:
6+
branches:
7+
- main # Trigger the workflow on pushes to the 'main' branch
8+
paths-ignore:
9+
- 'README.md' # Ignore changes to README.md to prevent unnecessary runs
10+
11+
# Set permissions for the workflow
12+
permissions:
13+
id-token: write # Allow writing ID tokens
14+
contents: read # Allow reading repository contents
15+
16+
jobs:
17+
# Job for Continuous Integration
18+
integration:
19+
name: Continuous Integration
20+
runs-on: ubuntu-latest # Run the job on the latest Ubuntu environment
21+
steps:
22+
- name: Checkout Code
23+
uses: actions/checkout@v3 # Check out the repository code
24+
25+
- name: Lint code
26+
run: echo "Linting repository" # Placeholder for linting step
27+
28+
- name: Run unit tests
29+
run: echo "Running unit tests" # Placeholder for running tests
30+
31+
# Job for building and pushing the Docker image to Amazon ECR
32+
build-and-push-ecr-image:
33+
name: Continuous Delivery
34+
needs: integration # This job depends on the completion of the 'integration' job
35+
runs-on: ubuntu-latest # Run this job on the latest Ubuntu environment
36+
steps:
37+
- name: Checkout Code
38+
uses: actions/checkout@v3 # Check out the repository code
39+
40+
- name: Install Utilities
41+
run: |
42+
sudo apt-get update # Update the package list
43+
sudo apt-get install -y jq unzip # Install 'jq' and 'unzip' utilities
44+
45+
- name: Configure AWS credentials
46+
uses: aws-actions/configure-aws-credentials@v1
47+
with:
48+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # Use secrets for AWS access key
49+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # Use secrets for AWS secret key
50+
aws-region: ${{ secrets.AWS_REGION }} # Use secrets for AWS region
51+
52+
- name: Login to Amazon ECR
53+
id: login-ecr
54+
uses: aws-actions/amazon-ecr-login@v1 # Log in to Amazon ECR
55+
56+
- name: Build, tag, and push image to Amazon ECR
57+
id: build-image
58+
env:
59+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # Set ECR registry from login step
60+
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} # Use secret for ECR repository name
61+
IMAGE_TAG: latest # Set the image tag to 'latest'
62+
run: |
63+
# Build a Docker image and push it to ECR for deployment
64+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . # Build the Docker image
65+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG # Push the image to ECR
66+
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" # Set output variable
67+
68+
# Job for Continuous Deployment
69+
Continuous-Deployment:
70+
needs: build-and-push-ecr-image # This job depends on the completion of the 'build-and-push-ecr-image' job
71+
runs-on: self-hosted # Run this job on a self-hosted runner
72+
steps:
73+
- name: Checkout
74+
uses: actions/checkout@v3 # Check out the repository code
75+
76+
- name: Configure AWS credentials
77+
uses: aws-actions/configure-aws-credentials@v1
78+
with:
79+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # Use secrets for AWS access key
80+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # Use secrets for AWS secret key
81+
aws-region: ${{ secrets.AWS_REGION }} # Use secrets for AWS region
82+
83+
- name: Login to Amazon ECR
84+
id: login-ecr
85+
uses: aws-actions/amazon-ecr-login@v1 # Log in to Amazon ECR
86+
87+
- name: Pull latest images
88+
run: |
89+
docker pull ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.ECR_REPOSITORY_NAME }}:latest # Pull the latest Docker image from ECR
90+
91+
# Uncomment to stop and remove the container if it's running
92+
# - name: Stop and remove container if running
93+
# run: |
94+
# docker ps -q --filter "name=cnncls" | grep -q . && docker stop cnncls && docker rm -fv cnncls
95+
96+
- name: Run Docker Image to serve users
97+
run: |
98+
# Run the Docker container and serve the application
99+
docker run -d -p 8080:8080 --name=cnncls -e 'AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}' -e 'AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}' -e 'AWS_REGION=${{ secrets.AWS_REGION }}' ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.ECR_REPOSITORY_NAME }}:latest
100+
101+
- name: Clean previous images and containers
102+
run: |
103+
docker system prune -f # Clean up unused Docker images and containers

0 commit comments

Comments
 (0)