|
| 1 | +# Deploys a reflex site -- Intended to be called from another workflow e.g. |
| 2 | +# uses: TimChild/webserver-template/deploy-reflex-site@main |
| 3 | +# with: |
| 4 | +# vps-ip: ${{ vars.DROPLET_IP }} |
| 5 | +# ... |
| 6 | +# |
| 7 | +name: Deploy Reflex Site |
| 8 | + |
| 9 | +on: |
| 10 | + workflow-call: |
| 11 | + inputs: |
| 12 | + vps-ip: |
| 13 | + description: "The IP address of the VPS that is set up as a webserver" |
| 14 | + required: true |
| 15 | + type: string |
| 16 | + site-name: |
| 17 | + description: "The name of the site to deploy (same as when initializing the site, usually a single word)" |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + ssh-user: |
| 21 | + description: "The user to connect to the VPS as (defaults to '${{ inputs.ssh-user }}')" |
| 22 | + default: "${{ inputs.ssh-user }}" |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + dotenv-path: |
| 26 | + description: "Path to .env file (can be created during workflow) that will be sent to the server for the backend" |
| 27 | + default: ".env" |
| 28 | + required: true |
| 29 | + type: string |
| 30 | + secrets: |
| 31 | + ssh-private-key: |
| 32 | + description: "The private ssh key that grants access to the VPS" |
| 33 | + required: true |
| 34 | + type: string |
| 35 | + |
| 36 | +concurrency: |
| 37 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 38 | + cancel-in-progress: true |
| 39 | + |
| 40 | +jobs: |
| 41 | + deploy-frontend: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + permissions: |
| 44 | + contents: read |
| 45 | + |
| 46 | + steps: |
| 47 | + - name: Check variables set |
| 48 | + # Make it easier to see when environment setup is incorrect |
| 49 | + run: | |
| 50 | + if [ -z "${{ secrets.ssh-private-key }}" ]; then |
| 51 | + echo "ssh-private-key is not set" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + if [ -z "${{ inputs.vps-ip }}" ]; then |
| 55 | + echo "vps-ip is not set" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + if [ -z "${{ inputs.site-name }}" ]; then |
| 59 | + echo "site-name is not set" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Checkout code |
| 64 | + uses: actions/checkout@v4 |
| 65 | + with: |
| 66 | + ref: ${{ github.ref }} |
| 67 | + |
| 68 | + - name: Set up uv |
| 69 | + uses: astral-sh/setup-uv@v5 |
| 70 | + with: |
| 71 | + version: "0.6.x" |
| 72 | + |
| 73 | + - name: Build frontend |
| 74 | + run: | |
| 75 | + mkdir -p "tmp_frontend_zip" |
| 76 | + mkdir -p "site" |
| 77 | + uv run reflex export --frontend-only --zip-dest-dir "tmp_frontend_zip" |
| 78 | + unzip -q tmp_frontend_zip/frontend.zip -d site/ |
| 79 | + rm -r "tmp_frontend_zip" |
| 80 | +
|
| 81 | + - name: Send frontend static files |
| 82 | + |
| 83 | + with: |
| 84 | + host: ${{ inputs.vps-ip }} |
| 85 | + username: ${{ inputs.ssh-user }} |
| 86 | + key: ${{ secrets.ssh-private-key }} |
| 87 | + port: 22 |
| 88 | + source: ./site/ |
| 89 | + target: sites/${{ vars.site-name }}/static/ |
| 90 | + strip_components: 2 |
| 91 | + overwrite: true |
| 92 | + |
| 93 | + - name: Update frontend files on server |
| 94 | + |
| 95 | + with: |
| 96 | + host: ${{ inputs.vps-ip }} |
| 97 | + username: ${{ inputs.ssh-user }} |
| 98 | + key: ${{ secrets.ssh-private-key }} |
| 99 | + # Note: This script is present on the server from the webserver setup |
| 100 | + script: | |
| 101 | + ./scripts/webserver-update-static-files.sh |
| 102 | +
|
| 103 | + deploy-backend: |
| 104 | + runs-on: ubuntu-latest |
| 105 | + permissions: |
| 106 | + contents: read |
| 107 | + packages: write |
| 108 | + |
| 109 | + steps: |
| 110 | + - name: Checkout code |
| 111 | + uses: actions/checkout@v4 |
| 112 | + with: |
| 113 | + ref: ${{ github.ref }} |
| 114 | + |
| 115 | + - name: Set up Docker Buildx |
| 116 | + uses: docker/setup-buildx-action@v3 |
| 117 | + |
| 118 | + - name: Log in to GitHub Container Registry |
| 119 | + uses: docker/login-action@v3 |
| 120 | + with: |
| 121 | + registry: ghcr.io |
| 122 | + username: ${{ github.actor }} |
| 123 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + |
| 125 | + - name: Set lower case owner name |
| 126 | + shell: bash |
| 127 | + run: | |
| 128 | + echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV} |
| 129 | + env: |
| 130 | + OWNER: "${{ github.repository_owner }}" |
| 131 | + |
| 132 | + - name: Build and push Docker image |
| 133 | + uses: docker/build-push-action@v5 |
| 134 | + with: |
| 135 | + context: . |
| 136 | + file: backend.Dockerfile |
| 137 | + push: true |
| 138 | + tags: ghcr.io/${{ env.OWNER_LC }}/${{ vars.site-name }}-backend:latest |
| 139 | + |
| 140 | + - name: Send .env file to server |
| 141 | + if: ${{ inputs.dotenv-path != "" }} |
| 142 | + |
| 143 | + with: |
| 144 | + host: ${{ inputs.vps-ip }} |
| 145 | + username: ${{ inputs.ssh-user }} |
| 146 | + key: ${{ secrets.ssh-private-key }} |
| 147 | + port: 22 |
| 148 | + source: ${{ inputs.dotenv-path }} |
| 149 | + target: sites/${{ vars.site-name }}/.env |
| 150 | + overwrite: true |
| 151 | + |
| 152 | + - name: Set .env permissions |
| 153 | + if: ${{ inputs.dotenv-path != "" }} |
| 154 | + |
| 155 | + with: |
| 156 | + host: ${{ inputs.vps-ip }} |
| 157 | + username: ${{ inputs.ssh-user }} |
| 158 | + key: ${{ secrets.ssh-private-key }} |
| 159 | + script: | |
| 160 | + chmod 600 sites/${{ vars.site-name }}/.env |
| 161 | +
|
| 162 | + - name: Pull new backend image on server and restart updated container |
| 163 | + |
| 164 | + with: |
| 165 | + host: ${{ inputs.vps-ip }} |
| 166 | + username: ${{ inputs.ssh-user }} |
| 167 | + key: ${{ secrets.ssh-private-key }} |
| 168 | + script: | |
| 169 | + # Log in to GitHub Container Registry |
| 170 | + echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin && \ |
| 171 | + docker pull ghcr.io/${{ env.OWNER_LC }}/${{ vars.site-name }}-backend:latest && \ |
| 172 | + docker compose up -d |
0 commit comments