Github actionsメモ

## ブランチをトリガーに

```yaml
on:
  push:
    branches:
      - master
```

[GitHub Actionsのワークフロー構文 - GitHub ヘルプ](https://help.github.com/ja/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions)

## タグをトリガーに

```yaml
on:
  push:
    tags:
      - v*
```

[GitHub Actionsのワークフロー構文 - GitHub ヘルプ](https://help.github.com/ja/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions)

## タグ名を変数として使用

```yaml
    - name: Set env
      run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:10})

    - name: Build
      run: make build-image
      env:
        IMAGE_TAG: ${{ env.RELEASE_VERSION }}
```

[continuous integration - Get the current pushed tag in Github Actions - Stack Overflow](https://stackoverflow.com/questions/58177786/get-the-current-pushed-tag-in-github-actions)

GITHUB_REFには `refs/tags/v0.0.1` というような値が渡されるため

## Slack通知

```yaml
    - name: Slack Notification
      uses: homoluctus/slatify@master
      if: always()
      with:
        type: ${{ job.status }}
        job_name: '*Deploy*'
        url: ${{ secrets.SLACK_WEBHOOK }}
```

自前で書いてもいいし、[Marketplaceにもいっぱいある](https://github.com/marketplace?utf8=%E2%9C%93&type=actions&query=slack)。

[homoluctus/slatify](https://github.com/marketplace/actions/slatify) はいくつか試した中でいい感じだった。


## Node.js

```yaml
    - uses: actions/setup-node@v1
      with:
        node-version: '12.x'
```

[Using Node.js with GitHub Actions - GitHub ヘルプ](https://help.github.com/ja/actions/automating-your-workflow-with-github-actions/using-nodejs-with-github-actions#installing-dependencies)

## AWS API Key

```yaml
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-2
```

["Configure AWS Credentials" Action For GitHub Actions · Actions · GitHub Marketplace](https://github.com/marketplace/actions/configure-aws-credentials-action-for-github-actions)

## ECR

```yaml
    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: my-ecr-repo
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

    - name: Logout of Amazon ECR
      if: always()
      run: docker logout ${{ steps.login-ecr.outputs.registry }}
```

[aws-actions/amazon-ecr-login: Logs into Amazon ECR with the local Docker client.](https://github.com/aws-actions/amazon-ecr-login)

## yarn キャッシュ

```yaml
    - name: Get yarn cache
      id: yarn-cache
      run: echo "::set-output name=dir::$(yarn cache dir)"
    
    - uses: actions/cache@v1
      with:
        path: ${{ steps.yarn-cache.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-yarn-
```

[cache/examples.md at master · actions/cache](https://github.com/actions/cache/blob/master/examples.md#node---yarn)