GitHub

GitホスティングコラボレーションCI/CDDevOpsオープンソースプルリクエストActions

バージョン管理ツール

GitHub

概要

GitHubは、Gitベースのソフトウェア開発プラットフォームであり、世界最大級のコード共有・協業プラットフォームです。2008年に設立され、2018年にMicrosoftによって買収されました。プルリクエスト、Issues、GitHub Actionsなどの協調開発機能により、全世界で1億人以上の開発者が利用し、オープンソース開発から企業向け開発まで幅広く対応しています。AI機能やCI/CD統合により、現代のソフトウェア開発における中核的なプラットフォームとして地位を確立しています。

詳細

GitHub 2025年版は、85%のマーケットシェアを持つ最大のコード共有プラットフォームとして成長を続けています。AI機能の大幅強化により、GitHub Copilot Agent、Copilot Workspace、GitHub Advanced Security等の先進的な機能を提供。プルリクエスト、Issue管理、GitHub Actions、GitHub Pages、セキュリティ機能、API統合など包括的な開発支援機能により、個人開発者から大企業まで効率的な開発ワークフローを実現します。オープンソースコミュニティの中心地として、世界中の開発者が知識を共有し、協力して革新的なソフトウェアを創造する場を提供しています。

主な特徴

  • 世界最大の開発者コミュニティ: 1億人以上の開発者と3億以上のリポジトリ
  • 先進的なAI機能: GitHub Copilot Agent、自動プルリクエスト生成、脆弱性修正
  • 包括的なCI/CD: GitHub Actionsによる25,000以上の自動化ワークフロー
  • 統合セキュリティ: Advanced Security、Secret Scanning、Dependabot
  • オープンソース中心: 世界最大のオープンソースプロジェクト拠点
  • エンタープライズ対応: SAML/SCIM、監査ログ、コンプライアンス機能

メリット・デメリット

メリット

  • 世界最大の開発者コミュニティと豊富な学習リソース・ライブラリ
  • 直感的で使いやすいユーザーインターフェースと優れた開発者体験
  • GitHub Copilotによる最先端のAI支援コーディング機能
  • 25,000以上のGitHub Actionsによる豊富な自動化ワークフロー
  • オープンソースプロジェクトでの圧倒的な採用率と可視性
  • Microsoft生態系との強力な統合とエンタープライズサポート

デメリット

  • プライベートリポジトリの制限(Freeプランでは限定的機能)
  • 大規模な企業利用では高コスト(年間数十万円〜)
  • セルフホスト型の機能制限(GitHubエンタープライズサーバー必要)
  • 高度なプロジェクト管理機能の制限(専門ツールに劣る)
  • GitHubに依存したワークフローによるベンダーロックインリスク
  • 複雑なCI/CDパイプラインでは外部ツール統合が必要

参考ページ

書き方の例

アカウント設定とリポジトリ作成

# GitHubアカウント設定(ローカル)
git config --global user.name "Your GitHub Username"
git config --global user.email "[email protected]"

# SSH鍵の生成と設定
ssh-keygen -t ed25519 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# SSH鍵をGitHubに登録(公開鍵をコピー)
cat ~/.ssh/id_ed25519.pub

# 新しいリポジトリを作成・プッシュ
git init
git add README.md
git commit -m "Initial commit"
git branch -M main
git remote add origin [email protected]:username/repository.git
git push -u origin main

# 既存リポジトリのクローン
git clone [email protected]:username/repository.git

プルリクエストワークフロー

# 1. リポジトリをフォーク(GitHubのWebUI)

# 2. フォークしたリポジトリをクローン
git clone [email protected]:yourusername/original-repo.git
cd original-repo

# 3. オリジナルリポジトリをアップストリームに追加
git remote add upstream [email protected]:originaluser/original-repo.git

# 4. 最新の変更を取得
git fetch upstream
git checkout main
git merge upstream/main

# 5. 新しいブランチで作業
git checkout -b feature/improvement
# コード変更・テスト
git add .
git commit -m "feat: ユーザー認証機能を改善"

# 6. フォークにプッシュ
git push origin feature/improvement

# 7. GitHubでプルリクエスト作成
# WebUIで "Compare & pull request" ボタンをクリック

# 8. レビュー対応
git checkout feature/improvement
# 修正作業
git add .
git commit -m "fix: レビューコメントに対応"
git push origin feature/improvement

# 9. マージ後のクリーンアップ
git checkout main
git pull upstream main
git branch -d feature/improvement
git push origin --delete feature/improvement

Issues(課題管理)

# Issue作成例
## 概要
ログイン機能にバグがあります。

## 再現手順
1. ログインページにアクセス
2. 正しいメールアドレスとパスワードを入力
3. ログインボタンをクリック

## 期待される動作
ダッシュボードにリダイレクトされる

## 実際の動作
エラーメッセージが表示される: "Invalid credentials"

## 環境
- OS: macOS 14.2
- ブラウザ: Chrome 120.0
- バージョン: v2.1.0

## 追加情報
ログイン試行時のコンソールエラー:

TypeError: Cannot read property 'token' of undefined


ラベル: bug, priority-high, backend
アサイン: @backend-team

GitHub Actions(CI/CD)

# .github/workflows/ci.yml
name: CI/CD Pipeline

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [18, 20, 22]
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Run linting
      run: npm run lint
    
    - name: Run tests
      run: npm test
    
    - name: Run coverage
      run: npm run test:coverage
    
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}

  security:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Run security audit
      run: npm audit --audit-level high
    
    - name: Run CodeQL Analysis
      uses: github/codeql-action/init@v3
      with:
        languages: javascript
    
    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v3

  deploy:
    needs: [test, security]
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Build application
      run: npm run build
    
    - name: Deploy to production
      uses: your-org/deploy-action@v1
      with:
        api-key: ${{ secrets.DEPLOY_API_KEY }}
        environment: production

GitHub Pages設定

# .github/workflows/pages.yml
name: Deploy to GitHub Pages

on:
  push:
    branches: [ main ]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Build site
      run: npm run build
    
    - name: Setup Pages
      uses: actions/configure-pages@v4
    
    - name: Upload artifact
      uses: actions/upload-pages-artifact@v3
      with:
        path: './dist'

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
    - name: Deploy to GitHub Pages
      id: deployment
      uses: actions/deploy-pages@v4

セキュリティ機能とSecret管理

# セキュリティワークフロー例
name: Security Checks

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '0 2 * * 1'  # 毎週月曜日午前2時

jobs:
  security:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Run Trivy vulnerability scanner
      uses: aquasecurity/trivy-action@master
      with:
        scan-type: 'fs'
        format: 'sarif'
        output: 'trivy-results.sarif'
    
    - name: Upload Trivy scan results
      uses: github/codeql-action/upload-sarif@v3
      with:
        sarif_file: 'trivy-results.sarif'
    
    - name: Run npm audit
      run: |
        npm audit --audit-level moderate
        npm audit fix --dry-run
    
    # Secretsの安全な使用例
    - name: Deploy with secrets
      env:
        API_KEY: ${{ secrets.API_KEY }}
        DATABASE_URL: ${{ secrets.DATABASE_URL }}
        DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
      run: |
        echo "Deploying with secure credentials..."
        # 環境変数として使用、直接echoしない
        deploy-script --api-key="$API_KEY"

GitHub CLI活用

# GitHub CLIのインストール・認証
brew install gh  # macOS
gh auth login

# リポジトリ操作
gh repo create my-new-repo --public
gh repo clone owner/repo
gh repo view owner/repo

# Issue操作
gh issue list
gh issue create --title "バグ報告" --body "詳細説明..."
gh issue view 123
gh issue close 123

# プルリクエスト操作
gh pr list
gh pr create --title "新機能追加" --body "詳細説明..."
gh pr view 456
gh pr merge 456 --squash
gh pr checkout 456

# リリース管理
gh release list
gh release create v1.0.0 --title "Version 1.0.0" --notes "初回リリース"
gh release upload v1.0.0 dist/*.zip

# Gist管理
gh gist create file.js --public
gh gist list
gh gist view gist-id

GitHub API活用

// GitHub API v4 (GraphQL) 使用例
const { Octokit } = require("@octokit/rest");

const octokit = new Octokit({
  auth: process.env.GITHUB_TOKEN,
});

// リポジトリ情報取得
async function getRepositoryInfo(owner, repo) {
  try {
    const { data } = await octokit.rest.repos.get({
      owner,
      repo,
    });
    
    console.log(`Repository: ${data.full_name}`);
    console.log(`Stars: ${data.stargazers_count}`);
    console.log(`Language: ${data.language}`);
    console.log(`Description: ${data.description}`);
  } catch (error) {
    console.error("Error:", error.message);
  }
}

// Issue作成
async function createIssue(owner, repo, title, body) {
  try {
    const { data } = await octokit.rest.issues.create({
      owner,
      repo,
      title,
      body,
      labels: ['bug', 'automated'],
    });
    
    console.log(`Issue created: ${data.html_url}`);
  } catch (error) {
    console.error("Error:", error.message);
  }
}

// プルリクエスト一覧取得
async function listPullRequests(owner, repo) {
  try {
    const { data } = await octokit.rest.pulls.list({
      owner,
      repo,
      state: 'open',
      per_page: 10,
    });
    
    data.forEach(pr => {
      console.log(`#${pr.number}: ${pr.title} by ${pr.user.login}`);
    });
  } catch (error) {
    console.error("Error:", error.message);
  }
}

// 使用例
getRepositoryInfo("octocat", "Hello-World");
createIssue("owner", "repo", "API Test Issue", "This is a test issue created via API");
listPullRequests("owner", "repo");

高度なワークフロー設定

# 複雑なワークフロー例
name: Advanced Workflow

on:
  push:
    branches: [ main, develop ]
    paths-ignore:
      - 'docs/**'
      - '*.md'
  pull_request:
    types: [opened, synchronize, reopened]
  release:
    types: [published]

env:
  NODE_VERSION: '20'
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      frontend: ${{ steps.changes.outputs.frontend }}
      backend: ${{ steps.changes.outputs.backend }}
    steps:
    - uses: actions/checkout@v4
    - uses: dorny/paths-filter@v3
      id: changes
      with:
        filters: |
          frontend:
            - 'frontend/**'
          backend:
            - 'backend/**'

  test-frontend:
    needs: changes
    if: needs.changes.outputs.frontend == 'true'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'npm'
        cache-dependency-path: frontend/package-lock.json
    
    - name: Install dependencies
      run: npm ci
      working-directory: ./frontend
    
    - name: Run tests
      run: npm test -- --coverage
      working-directory: ./frontend

  build-and-push:
    needs: [test-frontend]
    if: always() && (needs.test-frontend.result == 'success' || needs.test-frontend.result == 'skipped')
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=semver,pattern={{version}}
    
    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}