streamlit deployment file #2
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: workflow | |
# Define when the workflow will run | |
on: | |
push: | |
branches: | |
- main # Trigger the workflow on pushes to the 'main' branch | |
paths-ignore: | |
- 'README.md' # Ignore changes to README.md to prevent unnecessary runs | |
# Set permissions for the workflow | |
permissions: | |
id-token: write # Allow writing ID tokens | |
contents: read # Allow reading repository contents | |
jobs: | |
# Job for Continuous Integration | |
integration: | |
name: Continuous Integration | |
runs-on: ubuntu-latest # Run the job on the latest Ubuntu environment | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 # Check out the repository code | |
- name: Lint code | |
run: echo "Linting repository" # Placeholder for linting step | |
- name: Run unit tests | |
run: echo "Running unit tests" # Placeholder for running tests | |
# Job for building and pushing the Docker image to Amazon ECR | |
build-and-push-ecr-image: | |
name: Continuous Delivery | |
needs: integration # This job depends on the completion of the 'integration' job | |
runs-on: ubuntu-latest # Run this job on the latest Ubuntu environment | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 # Check out the repository code | |
- name: Install Utilities | |
run: | | |
sudo apt-get update # Update the package list | |
sudo apt-get install -y jq unzip # Install 'jq' and 'unzip' utilities | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # Use secrets for AWS access key | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # Use secrets for AWS secret key | |
aws-region: ${{ secrets.AWS_REGION }} # Use secrets for AWS region | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 # Log in to Amazon ECR | |
- name: Build, tag, and push image to Amazon ECR | |
id: build-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # Set ECR registry from login step | |
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} # Use secret for ECR repository name | |
IMAGE_TAG: latest # Set the image tag to 'latest' | |
run: | | |
# Build a Docker image and push it to ECR for deployment | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . # Build the Docker image | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG # Push the image to ECR | |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" # Set output variable | |
# Job for Continuous Deployment | |
Continuous-Deployment: | |
needs: build-and-push-ecr-image # This job depends on the completion of the 'build-and-push-ecr-image' job | |
runs-on: self-hosted # Run this job on a self-hosted runner | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 # Check out the repository code | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # Use secrets for AWS access key | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # Use secrets for AWS secret key | |
aws-region: ${{ secrets.AWS_REGION }} # Use secrets for AWS region | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 # Log in to Amazon ECR | |
- name: Pull latest images | |
run: | | |
docker pull ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.ECR_REPOSITORY_NAME }}:latest # Pull the latest Docker image from ECR | |
# Uncomment to stop and remove the container if it's running | |
# - name: Stop and remove container if running | |
# run: | | |
# docker ps -q --filter "name=cnncls" | grep -q . && docker stop cnncls && docker rm -fv cnncls | |
- name: Run Docker Image to serve users | |
run: | | |
# Run the Docker container and serve the application | |
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 | |
- name: Clean previous images and containers | |
run: | | |
docker system prune -f # Clean up unused Docker images and containers |