Tutorial: GitHub Actions deploys

Last updated 3 min read
Report issue

Overview#

This document provides a comprehensive guide on setting up GitHub Actions to automate the deployment of applications to GritivaCore. The goal is to ensure that every push to the main branch triggers a CI/CD pipeline that builds and deploys the application within five minutes.

Prerequisites#

Before you begin, ensure you have the following:

  • A GitHub repository.
  • An API key for GritivaCore, created in the panel under SET. → API keys (it looks like grv_…).
  • SSH access to the VM that runs your production scope — GritivaCore has no remote command-execution endpoint, so the rollover itself happens over SSH.

Setting Up GitHub Actions#

Follow these steps to configure your GitHub Actions workflow for deployment.

Creating a Workflow#

  1. Create an API key in GritivaCore: Settings → API keys → New key. Copy the grv_… value; it is shown once. You can list the keys that exist later with GET /api/v1/api-keys.
  2. Add it to your GitHub repository:
    • Navigate to SET. → Secrets and variables → Actions → New repository secret and name it GRITIVA_API_KEY.
  3. Add two more secrets for the rollover step: DEPLOY_SSH_KEY (a private key authorised on the VM) and DEPLOY_HOST (the VM's address). Add the scope's id as GRITIVA_SCOPE_ID — you can read it from GET /api/v1/scopes.

Configuring Deployment Steps#

Create a file named .github/workflows/deploy.yml in your repository with the following content:

YAML
name: Deploy
on: { push: { branches: [main] } }
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build image
        run: docker build -t myorg/myapp:${{ github.sha }} .
      - name: Push image
        run: docker push myorg/myapp:${{ github.sha }}
      - name: Roll the scope over SSH
        env:
          DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
          DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          install -d -m 700 ~/.ssh
          printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/deploy && chmod 600 ~/.ssh/deploy
          ssh -i ~/.ssh/deploy -o StrictHostKeyChecking=accept-new \
            deploy@"$DEPLOY_HOST" \
            "cd /srv/myapp && IMAGE_TAG=$IMAGE_TAG docker compose pull && IMAGE_TAG=$IMAGE_TAG docker compose up -d"
      - name: Check the scope
        env:
          GRITIVA_API_KEY: ${{ secrets.GRITIVA_API_KEY }}
          SCOPE_ID: ${{ secrets.GRITIVA_SCOPE_ID }}
        run: |
          curl -sf -H "X-API-Key: $GRITIVA_API_KEY" \
            "https://api.gritiva.com/api/v1/scopes/$SCOPE_ID"

The last step is a gate, not a deploy: it reads the scope back and fails the job if the API does not answer. A healthy scope reports status running and health healthy.

  1. Test the setup by pushing a commit to the main branch and tailing the logs in the GritivaCore panel.

Common Deployment Scenarios#

  • Deploying a new application version.
  • Rolling back to a previous version by pointing the compose file at the previous image tag:
    BASH
    ssh deploy@<vm> 'cd /srv/myapp && IMAGE_TAG=<prev-sha> docker compose up -d'
  • Handling environment-specific configurations.

Troubleshooting#

If you encounter issues during deployment, consider the following:

  • Ensure GRITIVA_API_KEY, DEPLOY_SSH_KEY and DEPLOY_HOST are all set in GitHub Secrets.
  • Check the workflow logs for errors during the build, push or SSH step.
  • On the VM, docker compose logs -f covers the app and journalctl -u gritiva-agent covers the agent.

Conclusion#

By following this guide, you can successfully set up GitHub Actions to automate your deployment process to GritivaCore, ensuring efficient and reliable application updates.

Best Practices

Best Practices
Consider the following best practices for your GitHub Actions workflows: - Use per-environment tokens and revoke leaked tokens promptly. - Avoid using `:latest` tags; prefer image digests for stability. - Implement pre-build health checks to ensure application readiness.

Further Reading

Further Reading
For deeper insights into GitHub Actions, refer to the [GitHub Actions documentation](https://docs.github.com/en/actions).