diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d72e419 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,57 @@ +name: Build and Push to ECR + +on: + push: + branches: [ main ] + pull_request: + types: + - closed + branches: + - main + +env: + AWS_REGION: us-east-1 + ECR_REPOSITORY: crawfordc + APP_NAME: theseus + DOCKERFILE_PATH: ./app + + +jobs: + build-and-push: + if: github.event.pull_request.merged == true || github.event_name == 'push' + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push image to Amazon ECR + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + run: | + # Build with cache + docker pull $ECR_REGISTRY/$ECR_REPOSITORY:$APP_NAME || true + docker build \ + --cache-from $ECR_REGISTRY/$ECR_REPOSITORY:$APP_NAME \ + --tag $ECR_REGISTRY/$ECR_REPOSITORY:$APP_NAME \ + --tag $ECR_REGISTRY/$ECR_REPOSITORY:$APP_NAME-$IMAGE_TAG \ + -f $DOCKERFILE_PATH/Dockerfile \ + $DOCKERFILE_PATH + + # Push both tags + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$APP_NAME + + - name: Output Image URIs + run: | + echo "Latest Image = ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ env.APP_NAME }}"