|
| 1 | +# GitHub Workflows |
| 2 | + |
| 3 | +This directory contains GitHub Actions workflows for the `nestjs-offline-oauth2` package. |
| 4 | + |
| 5 | +## Workflows |
| 6 | + |
| 7 | +### 1. CI Workflow (`ci.yml`) |
| 8 | + |
| 9 | +**Triggers:** |
| 10 | +- Push to `main` or `develop` branches |
| 11 | +- Pull requests to `main` or `develop` branches |
| 12 | + |
| 13 | +**What it does:** |
| 14 | +- Tests the package on Node.js 18.x and 20.x |
| 15 | +- Runs linting and formatting checks |
| 16 | +- Builds the project and verifies output |
| 17 | +- Validates package.json configuration |
| 18 | +- Performs a dry-run publish to ensure package is ready |
| 19 | +- Runs security audits |
| 20 | + |
| 21 | +### 2. NPM Publish Workflow (`npm-publish.yml`) |
| 22 | + |
| 23 | +**Triggers:** |
| 24 | +- **Automatic**: When a GitHub release is published |
| 25 | +- **Manual**: Workflow dispatch with version bump options |
| 26 | + |
| 27 | +**What it does:** |
| 28 | +- Installs dependencies and runs quality checks |
| 29 | +- Builds the project |
| 30 | +- Bumps version (manual trigger only) |
| 31 | +- Creates git tag (manual trigger only) |
| 32 | +- Publishes to NPM |
| 33 | +- Creates GitHub release (manual trigger only) |
| 34 | + |
| 35 | +## Setup Instructions |
| 36 | + |
| 37 | +### 1. NPM Token Setup |
| 38 | + |
| 39 | +1. Go to [npmjs.com](https://www.npmjs.com) and log in |
| 40 | +2. Navigate to your profile → Access Tokens |
| 41 | +3. Generate a new token with "Automation" or "Publish" permissions |
| 42 | +4. In your GitHub repository, go to Settings → Secrets and variables → Actions |
| 43 | +5. Add a new repository secret named `NPM_TOKEN` with your token value |
| 44 | + |
| 45 | +### 2. Repository Permissions |
| 46 | + |
| 47 | +Ensure your repository has the following permissions: |
| 48 | +- Actions: Read and write permissions |
| 49 | +- Contents: Write permissions (for creating releases and tags) |
| 50 | +- Metadata: Read permissions |
| 51 | + |
| 52 | +To set these: |
| 53 | +1. Go to Settings → Actions → General |
| 54 | +2. Under "Workflow permissions", select "Read and write permissions" |
| 55 | +3. Check "Allow GitHub Actions to create and approve pull requests" |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +### Publishing a New Version |
| 60 | + |
| 61 | +#### Method 1: Manual Workflow Dispatch (Recommended) |
| 62 | + |
| 63 | +1. Go to Actions tab in your GitHub repository |
| 64 | +2. Select "Publish to NPM" workflow |
| 65 | +3. Click "Run workflow" |
| 66 | +4. Choose the version bump type: |
| 67 | + - `patch`: Bug fixes (1.0.0 → 1.0.1) |
| 68 | + - `minor`: New features (1.0.0 → 1.1.0) |
| 69 | + - `major`: Breaking changes (1.0.0 → 2.0.0) |
| 70 | +5. Click "Run workflow" |
| 71 | + |
| 72 | +This will: |
| 73 | +- Bump the version in package.json |
| 74 | +- Create a git commit and tag |
| 75 | +- Publish to NPM |
| 76 | +- Create a GitHub release |
| 77 | + |
| 78 | +#### Method 2: GitHub Release |
| 79 | + |
| 80 | +1. Go to Releases in your repository |
| 81 | +2. Click "Create a new release" |
| 82 | +3. Create a new tag (e.g., `v1.0.1`) |
| 83 | +4. Add release notes |
| 84 | +5. Publish the release |
| 85 | + |
| 86 | +This will automatically trigger the NPM publish workflow. |
| 87 | + |
| 88 | +### Checking Build Status |
| 89 | + |
| 90 | +- All pull requests will automatically run the CI workflow |
| 91 | +- Check the Actions tab to see workflow status |
| 92 | +- Green checkmarks indicate successful builds |
| 93 | +- Red X marks indicate failures that need to be fixed |
| 94 | + |
| 95 | +## Workflow Files Explained |
| 96 | + |
| 97 | +### CI Workflow Features |
| 98 | + |
| 99 | +- **Multi-Node Testing**: Tests on Node.js 18.x and 20.x |
| 100 | +- **Code Quality**: Runs ESLint and Prettier checks |
| 101 | +- **Build Verification**: Ensures TypeScript compilation works |
| 102 | +- **Package Validation**: Checks package.json structure |
| 103 | +- **Security**: Runs npm audit for vulnerabilities |
| 104 | +- **Dry Run**: Tests publishing without actually publishing |
| 105 | + |
| 106 | +### Publish Workflow Features |
| 107 | + |
| 108 | +- **Quality Gates**: Runs linting and building before publishing |
| 109 | +- **Version Management**: Automatic version bumping for manual triggers |
| 110 | +- **Git Integration**: Creates commits and tags automatically |
| 111 | +- **NPM Publishing**: Publishes with public access |
| 112 | +- **Release Creation**: Generates GitHub releases with changelog |
| 113 | +- **Error Handling**: Validates build output before publishing |
| 114 | + |
| 115 | +## Troubleshooting |
| 116 | + |
| 117 | +### Common Issues |
| 118 | + |
| 119 | +1. **NPM_TOKEN not working** |
| 120 | + - Ensure the token has correct permissions |
| 121 | + - Check token hasn't expired |
| 122 | + - Verify secret name is exactly `NPM_TOKEN` |
| 123 | + |
| 124 | +2. **Permission denied errors** |
| 125 | + - Check repository workflow permissions |
| 126 | + - Ensure GITHUB_TOKEN has write access |
| 127 | + |
| 128 | +3. **Build failures** |
| 129 | + - Check TypeScript compilation locally |
| 130 | + - Ensure all dependencies are properly declared |
| 131 | + - Verify dist folder is created correctly |
| 132 | + |
| 133 | +4. **Version conflicts** |
| 134 | + - Ensure the version doesn't already exist on NPM |
| 135 | + - Check if git tags already exist |
| 136 | + |
| 137 | +### Manual Recovery |
| 138 | + |
| 139 | +If workflows fail and you need to publish manually: |
| 140 | + |
| 141 | +```bash |
| 142 | +# Build the project |
| 143 | +npm run build |
| 144 | + |
| 145 | +# Publish to NPM |
| 146 | +npm publish --access public |
| 147 | +``` |
| 148 | + |
| 149 | +## Best Practices |
| 150 | + |
| 151 | +1. **Always test locally** before pushing changes |
| 152 | +2. **Use semantic versioning** for version bumps |
| 153 | +3. **Write meaningful commit messages** for version bumps |
| 154 | +4. **Test the package** after publishing to ensure it works |
| 155 | +5. **Monitor workflow runs** and fix issues promptly |
| 156 | +6. **Keep dependencies updated** to avoid security issues |
| 157 | + |
| 158 | +## Security Considerations |
| 159 | + |
| 160 | +- Never commit NPM tokens to the repository |
| 161 | +- Regularly rotate access tokens |
| 162 | +- Monitor package downloads for unusual activity |
| 163 | +- Keep dependencies updated to avoid vulnerabilities |
| 164 | +- Use `npm audit` regularly to check for security issues |
0 commit comments