added smart portfolio #7
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: Deploy Preview (GitHub) | ||
|
Check failure on line 1 in .github/workflows/deploy-preview.yml
|
||
| on: | ||
| push: | ||
| branches-ignore: | ||
| - main | ||
| - master | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| env: | ||
| APP_DIR: Flask_py | ||
| jobs: | ||
| build-and-push: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| image_tag: ${{ steps.meta.outputs.image_tag }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| - name: Log in to GHCR | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Build metadata | ||
| id: meta | ||
| run: | | ||
| IMAGE_TAG=ghcr.io/${{ github.repository_owner }}/smart-portfolio-rebalancer:${{ github.sha }} | ||
| echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT | ||
| - name: Build and push image | ||
| run: | | ||
| docker build -t "${{ steps.meta.outputs.image_tag }}" "${APP_DIR}" | ||
| docker push "${{ steps.meta.outputs.image_tag }}" | ||
| deploy-preview: | ||
| runs-on: ubuntu-latest | ||
| needs: build-and-push | ||
| if: ${{ secrets.PREVIEW_HOST != '' && secrets.PREVIEW_USER != '' && secrets.PREVIEW_SSH_KEY != '' && secrets.PREVIEW_BASE_URL != '' }} | ||
| steps: | ||
| - name: Setup SSH key | ||
| uses: webfactory/ssh-agent@v0.9.0 | ||
| with: | ||
| ssh-private-key: ${{ secrets.PREVIEW_SSH_KEY }} | ||
| - name: Add host key | ||
| run: ssh-keyscan -H "${{ secrets.PREVIEW_HOST }}" >> ~/.ssh/known_hosts | ||
| - name: Deploy container | ||
| run: | | ||
| BRANCH_SLUG=$(echo "${GITHUB_REF_NAME}" | tr '[:upper:]' '[:lower:]' | sed 's#[^a-z0-9-]#-#g') | ||
| CONTAINER_NAME="rebalancer-${BRANCH_SLUG}" | ||
| PREVIEW_URL="https://${BRANCH_SLUG}.${{ secrets.PREVIEW_BASE_URL }}" | ||
| ssh "${{ secrets.PREVIEW_USER }}@${{ secrets.PREVIEW_HOST }}" " | ||
| docker pull ${{ needs.build-and-push.outputs.image_tag }} && | ||
| docker rm -f ${CONTAINER_NAME} || true && | ||
| docker run -d --name ${CONTAINER_NAME} \ | ||
| --restart unless-stopped \ | ||
| -e HOST=0.0.0.0 -e PORT=8000 \ | ||
| --label traefik.enable=true \ | ||
| --label traefik.http.routers.${CONTAINER_NAME}.rule=Host\\\`${BRANCH_SLUG}.${{ secrets.PREVIEW_BASE_URL }}\\\` \ | ||
| --label traefik.http.services.${CONTAINER_NAME}.loadbalancer.server.port=8000 \ | ||
| ${{ needs.build-and-push.outputs.image_tag }}" | ||
| echo "Preview deployed at: ${PREVIEW_URL}" | ||