diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index bbb2394..b9426e3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -1,59 +1,98 @@ -# Your workflow name. -name: Deploy to heroku. +name: workflow -# Run workflow on every push to main branch. on: push: - branches: [main] + branches: + - main + paths-ignore: + - 'README.md' + +permissions: + id-token: write + contents: read -# Your workflows jobs. jobs: - build: + integration: + name: Continuous Integration runs-on: ubuntu-latest steps: - # Check-out your repository. - - name: Checkout - uses: actions/checkout@v2 + - name: Checkout Code + uses: actions/checkout@v3 + - name: Lint code + run: echo "Linting repository" -### ⬇ IMPORTANT PART ⬇ ### + - name: Run unit tests + run: echo "Running unit tests" - - name: Build, Push and Release a Docker container to Heroku. # Your custom step name - uses: gonuit/heroku-docker-deploy@v1.3.3 # GitHub action name (leave it as it is). + build-and-push-ecr-image: + name: Continuous Delivery + needs: integration + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Install Utilities + run: | + sudo apt-get update + sudo apt-get install -y jq unzip + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 with: - # Below you must provide variables for your Heroku app. + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} - # The email address associated with your Heroku account. - # If you don't want to use repository secrets (which is recommended) you can do: - # email: my.email@example.com - email: ${{ secrets.HEROKU_EMAIL }} - - # Heroku API key associated with provided user's email. - # Api Key is available under your Heroku account settings. - heroku_api_key: ${{ secrets.HEROKU_API_KEY }} - - # Name of the heroku application to which the build is to be sent. - heroku_app_name: ${{ secrets.HEROKU_APP_NAME }} - - # (Optional, default: "./") - # Dockerfile directory. - # For example, if you have a Dockerfile in the root of your project, leave it as follows: - dockerfile_directory: ./ - - # (Optional, default: "Dockerfile") - # Dockerfile name. - dockerfile_name: Dockerfile - - # (Optional, default: "") - # Additional options of docker build command. - docker_options: "--no-cache" - - # (Optional, default: "web") - # Select the process type for which you want the docker container to be uploaded. - # By default, this argument is set to "web". - # For more information look at https://devcenter.heroku.com/articles/process-model - process_type: web + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} + IMAGE_TAG: latest + run: | + # Build a docker container and + # push it to ECR so that it can + # be deployed to ECS. + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" - -### ⬆ IMPORTANT PART ⬆ ### \ No newline at end of file + Continuous-Deployment: + needs: build-and-push-ecr-image + runs-on: self-hosted + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + + - name: Pull latest images + run: | + docker pull ${{secrets.AWS_ECR_LOGIN_URI}}/${{ secrets.ECR_REPOSITORY_NAME }}:latest + + # - 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: | + 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 \ No newline at end of file