--- title: Github Actions tags: git, github, cicd --- Github Actions === ## 理論 以前更新程式到伺服器 ![image](https://hackmd.io/_uploads/SyhaZJX_T.png) 為了保持一致改用 git, github (很多人一起寫時,不用git很容易出錯) ![image](https://hackmd.io/_uploads/HJnRGyXu6.png) 讓他變自動 ![image](https://hackmd.io/_uploads/SJ1YmymOT.png) ## 實戰 ```yml name: 'Github Actions' on: [push, pull_request] jobs: test: runs-on: ubuntu-latest if: "!contains(github.event.head_commit.message, '[skip ci]')" steps: - name: Checkout project uses: actions/checkout@v3 - name: Specify node version - uses: actions/setup-node@v3 with: node-version: '16.x' registry-url: 'https://registry.npmjs.org' - name: Install node_modules run: npm ci - name: Lint run: npm run lint - name: Test run: npm run test deploy: runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - name: Set up the private key uses: webfactory/ssh-agent@v0.7.0 with: ssh-private-key: ${{ secrets.SSH_KEY }} - name: Set known_hosts run: echo "${{ secrets.KNOWN_HOSTS }}" >> ~/.ssh/known_hosts - name: Connect to the production environment run: ssh ${{ secrets.USER_HOST }} ' cd /srv/node/my_project && git pull && npm ci ' ``` ## 參考資料 https://stackoverflow.com/questions/67516571/github-action-triggered-by-success-of-a-different-action https://stackoverflow.com/questions/63148639/create-dependencies-between-jobs-in-github-actions https://ubuntudroid.medium.com/labeling-github-auto-merge-pull-requests-d5a86f62280a