Haramaki0326 / StudyToDo

2021年にチャレンジ、やりたいことリスト
0 stars 0 forks source link

GitHub Actions について #50

Open Haramaki0326 opened 3 years ago

Haramaki0326 commented 3 years ago

参考

Haramaki0326 commented 3 years ago
Haramaki0326 commented 3 years ago

GitHub Actionsで自分のリポジトリ内のファイルにアクセスするには以下の一文が必要

steps:
      - uses: actions/checkout@v2

サンプル

name: Node.js CI

on:
  # mainにプルリクエストをすると起動する
  pull_request:
    branches:
      - main
  # それ以外のブランチではpushをすると起動する
  push:
    branches-ignore:
      - 'main' 

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: '16'
    - run: npm install
    - run: npm run greet
Haramaki0326 commented 3 years ago

GitHub Actionsの定石は以下のような感じかな

package.json

{
  "name": "training",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "format": "prettier --write \"src/**/*.{js,json,md,html}\"",
    "check-format": "prettier --check \"src/**/*.{js,json,md,html}\"",
    "lint": "eslint --fix \"src/**/*.js\"",
    "fix": "npm run format && npm run lint",
    "prepare": "husky install",
    "lint-staged": "lint-staged"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Haramaki0326/Training.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Haramaki0326/Training/issues"
  },
  "homepage": "https://github.com/Haramaki0326/Training#readme",
  "lint-staged": {
    "*.js": [
      "prettier --write",
      "eslint --fix"
    ]
  },
  "devDependencies": {
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "husky": "^7.0.1",
    "lint-staged": "^11.1.2",
    "prettier": "^2.3.2"
  }
}

frontend-actions.yml

name: Check frontEnds

on:
  # mainにプルリクエストをすると起動する
  pull_request:
    branches:
      - main
  # それ以外のブランチではpushをすると起動する
  push:
    branches-ignore:
      - 'main' 

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: 16.x
    - name: Install packages
      run: npm ci
    - name: Prettier
      run: npm run check-format
    - name: Lint
      run: npm run lint
Haramaki0326 commented 3 years ago

GitHub Pagesへ自動デプロイする

参考

ポイント

実装

https://github.com/Haramaki0326/Training/blob/main/.github/workflows/main.yml

name: Build and Deploy

on:
  # mainにプルリクエストをすると起動する
  pull_request:
    branches:
      - main
  # mainにpushをすると起動する
  push:
    branches:
      - main

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v2
      with:
        node-version: 16.x
    - name: Install NPM packages
      run: npm ci
    - name: Prettier
      run: npm run check-format
    - name: Lint
      run: npm run lint
    - name: Build website
      run: npm run build --if-present
    - name: Deploy website
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: ./dist