Update deploy workflow SSH setup #9
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: Rails CI/CD Deploy | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - "config/specs/**" | |
| - "app/**" | |
| - "config/**" | |
| - "db/**" | |
| - "lib/**" | |
| - "Gemfile" | |
| - "Gemfile.lock" | |
| - "Dockerfile" | |
| - "config/deploy.yml" | |
| - ".github/workflows/deploy.yml" | |
| workflow_dispatch: | |
| jobs: | |
| test: | |
| name: Validate and Test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Ruby | |
| uses: ruby/setup-ruby@v1 | |
| with: | |
| bundler-cache: true | |
| - name: Validate business spec | |
| run: bundle exec rake specs:validate | |
| - name: Run tests | |
| run: bundle exec rails test | |
| env: | |
| RAILS_ENV: test | |
| deploy: | |
| name: Deploy to Lightsail | |
| runs-on: ubuntu-latest | |
| needs: test | |
| if: github.ref == 'refs/heads/main' | |
| environment: production | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Ruby | |
| uses: ruby/setup-ruby@v1 | |
| with: | |
| bundler-cache: true | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GHCR_TOKEN }} | |
| - name: Set up SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.LIGHTSAIL_SSH_KEY }}" > ~/.ssh/lightsail_key | |
| chmod 600 ~/.ssh/lightsail_key | |
| ssh-keygen -R "${{ vars.LIGHTSAIL_HOST }}" || true | |
| ssh-keyscan -H "${{ vars.LIGHTSAIL_HOST }}" >> ~/.ssh/known_hosts | |
| cat > ~/.ssh/config <<EOF | |
| Host ${{ vars.LIGHTSAIL_HOST }} | |
| User ubuntu | |
| IdentityFile ~/.ssh/lightsail_key | |
| IdentitiesOnly yes | |
| StrictHostKeyChecking yes | |
| EOF | |
| chmod 600 ~/.ssh/config | |
| - name: Start ssh-agent | |
| run: | | |
| eval "$(ssh-agent -s)" | |
| ssh-add ~/.ssh/lightsail_key | |
| - name: Write Kamal secrets | |
| run: | | |
| mkdir -p .kamal | |
| cat > .kamal/secrets <<EOF | |
| RAILS_MASTER_KEY=${{ secrets.RAILS_MASTER_KEY }} | |
| KAMAL_REGISTRY_PASSWORD=${{ secrets.GHCR_TOKEN }} | |
| EOF | |
| - name: Deploy with Kamal | |
| run: bundle exec kamal deploy |