# Introduce Github Action

We can use self-host runner when the quota of github action running out. We can also use JIRA action to save some time on Jira. There are 2 snippts about these 2 scenarios. We can use these in the future.

## Self-host runner example

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: actions-runner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: actions-runner
  template:
    metadata:
      labels:
        app: actions-runner
    spec:
      volumes:
      - name: dockersock
        hostPath:
          path: /var/run/docker.sock
      - name: workdir
        hostPath:
          path: /tmp/github-runner-your-repo
      containers:
      - name: runner
        image: 385595570414.dkr.ecr.us-west-2.amazonaws.com/dbaas/chenyiwen/github-runner:latest
        env:
        - name: RUNNER_TOKEN
          value: <fill the runner token here>
        - name: REPO_URL
          value: https://github.com/tidbcloud/infra-api
        - name: RUNNER_NAME_PREFIX
          value: test
        - name: RUNNER_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: RUNNER_WORKDIR
          value: /tmp/github-runner-your-repo
        volumeMounts:
        - name: dockersock
          mountPath: /var/run/docker.sock
        - name: workdir
          mountPath: /tmp/github-runner-your-repo
```

## Jira action

```yaml
name: Jira Example
# on: push
on: {}

jobs:
  build:
    runs-on: ubuntu-latest
    name: Jira Example
    steps:
      - name: Login
        uses: atlassian/gajira-login@master
        env:
          JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
          JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
          JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}

      - name: Jira TODO
        uses: atlassian/gajira-todo@master
        with:
          project: GA
          issuetype: Task
          description: Created automatically via GitHub Actions
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## PR action

```yaml
name: Add user to metadb
on:
  workflow_dispatch:
    inputs:
      project:
        description: "Project name, e.g: chenyiwen"
        required: true

jobs:
  create-related-pr:
    runs-on: ubuntu-latest
    steps:
      - name: Configure git
        env:
          TOKEN: ${{ github.token }}
        run: git config --global url."https://${TOKEN}@github.com/".insteadOf "https://github.com/"

      - name: add metadb accounts
        uses: technote-space/create-pr-action@v2
        with:
          EXECUTE_COMMANDS: |
            sed -i "s/metadb:users:/metadb:users:\n  - ${{ github.event.inputs.project }}/g" test.yaml
          COMMIT_MESSAGE: "add to metadb users"
          COMMIT_NAME: "add to metadb users"
          COMMIT_EMAIL: "ops@tidbcloud.com"
          PR_BRANCH_NAME: "ops-add-metadb-users-${PR_ID}"
          PR_TITLE: "add ${{ github.event.inputs.project }} to metadb users"
```

## Github Action style docker push

```yaml
name: build-and-push-to-ecr
on:
  push:
    branches:
      - main
jobs:
  # we can use path filter here.
  infra-provider-build-and-push:
    name: infra-provider-build-and-push
    runs-on: ubuntu-18.04
    env:
      AWS_ROLE_ARN: ${{ secrets.DBAAS_DEV_AWS_ROLE }}
      AWS_REGION: us-west-2
    steps:
      - name: Checkout
        uses: actions/checkout@v2


      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Configure go module
        env:
          TOKEN: ${{ secrets.GIT_ACTION_BOT }}
        run: |
          git config --global url."https://${TOKEN}@github.com/".insteadOf "https://github.com/"
          go mod vendor

      - name: Build and push - Infra Provider
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          tags: ${{ steps.login-ecr.outputs.registry }}/dbaas/infra-api/infra-provider:${{ github.sha }}
          push: true

      # distribute to gcr
      # - name: Login to GCR
      #   uses: docker/login-action@v1
      #   with:
      #     registry: gcr.io
      #     username: _json_key
      #     password: ${{ secrets.GCR_JSON_KEY }}

      # - name: Push image to gcr
      #   uses: akhilerm/tag-push-action@v1
      #   with:
      #     src: ${{ steps.login-ecr.outputs.registry }}/dbaas/infra-api/infra-provider:${{ github.sha }}
      #     dst: |
      #       gcr tags
      #       ${{ steps.meta.outputs.tags }}

  migration-controller-build-and-push:
    name: migration-controller-build-and-push
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Configure go module
        env:
          TOKEN: ${{ secrets.GIT_ACTION_BOT }}
        run: |
          git config --global url."https://${TOKEN}@github.com/".insteadOf "https://github.com/"
          go mod vendor

      - name: Build and push - Migration Controller
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./cmd/migration-controller/Dockerfile
          tags: ${{ steps.login-ecr.outputs.registry }}/dbaas/infra-api/migration-controller:${{ github.sha }}
          push: true
```