- Triggers on every `push` or `pull request` targeting the `main` branch.
- Builds a development Docker image using `Dockerfile.dev`, optimized for testing.
- Executes unit tests using Vitest inside a clean, containerized environment to ensure consistency.
- Halts the workflow immediately if any test fails — enforcing code quality.
- Caches both Docker build layers and npm dependencies for faster CI runs.
- Authenticates securely with Docker Hub using GitHub repository secrets.
- Builds a production-ready image using the `prod` stage in `Dockerfile`.
- Tags and pushes the final image to Docker Hub with both `latest` and short SHA tags for traceability.
> [!NOTE]
> For more information about `docker/build-push-action`, refer to the [GitHub Action README](https://github.com/docker/build-push-action/blob/master/README.md).
---
### Step 3: Run the workflow
After you've added your workflow file, it's time to trigger and observe the CI/CD process in action.
1. Commit and push your workflow file
Select "Commit changes…" in the GitHub editor.
- This push will automatically trigger the GitHub Actions pipeline.
2. Monitor the workflow execution
1. Go to the Actions tab in your GitHub repository.
2. Click into the workflow run to follow each step: **build**, **test**, and (if successful) **push**.
3. Verify the Docker image on Docker Hub
- After a successful workflow run, visit your [Docker Hub repositories](https://hub.docker.com/repositories).
- You should see a new image under your repository with:
- Repository name: `${your-repository-name}`
- Tags include:
- `latest` – represents the most recent successful build; ideal for quick testing or deployment.
- `<short-sha>` – a unique identifier based on the commit hash, useful for version tracking, rollbacks, and traceability.
> [!TIP] Protect your main branch
> To maintain code quality and prevent accidental direct pushes, enable branch protection rules:
> - Navigate to your **GitHub repo → Settings → Branches**.
> - Under Branch protection rules, click **Add rule**.
> - Specify `main` as the branch name.