Intro to Github Action @likecoin William Chong
Github action: interesting features - Automated workflow tool by Github - Deep integration with Github - Actions marketplace - Scheduled workflow and interesting event triggers
Why? - We were using circleci, until... - Also we want osx build for react-native and we happen to be using github team
Sample use case: liker land broswer extension - https://github.com/likecoin/liker-land-browser-extension - Webpack repo - Run eslint and webpack build for each PR to check for lint and build error
Workflow result (1)
Workflow result (2) - Linter output is viewable in Pull Request without having to go into actual workflow log to see the warnings and error. - Release note didn’t mention anything about github actions or eslint, but seems working very well. - https://github.blog/2018-12-14-intr oducing-check-runs-and-annotatio ns/
.github/workflows/main.yml - https://github.com/likecoin/liker-land-browser-extension/blob/master/.github/w orkflows/main.yml Trigger: on: push: branches: - '**' pull_request: branches: - '**' Jobs: jobs: build: runs-on: ubuntu-latest Steps: ... - name: Install dependencies run: yarn - name: Lint run: yarn lint Actions: .... - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: 10 ….
Trigger on: push: branches: - '**' pull_request: branches: - '**' - Triggers on every push to any branch or any pull request on any branch - Depends on different event, `github.ref` will be populated by different values. e.g. branch name, tag name, etc. - https://help.github.com/en/actions/referen ce/events-that-trigger-workflows
Jobs and Steps jobs: build: runs-on: ubuntu-latest Steps: ... - name: Install dependencies run: yarn - name: Lint run: yarn lint - name: Compile run: yarn build - Jobs can be run in ubuntu,window and osx. - Support matrix definition for running in more than 1 env - Steps can either be shell commands or actions
Actions Actions: .... - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: 10 …. - Actions are reusable steps packages that accept parameters - Supports js/ts or dockerized based action - For example, the actions/checkout@v2 replaces the complicated steps of checking if git exists, repository authentication, fetching repo, handle LFS and submodules - Similarly actions/setup-node helps to setup a node env, which accept a single param of node version, or an array for run matrix jobs with different node version in a workflow
More workflow sample: Liker Land app release - https://github.com/likecoin/likecoin-app/releases - React-native mobile app - We want to automatically build production android apk, only after we have created a release note on github, and attach it as release binary - Also tries to automatically download recompiled dsym from itunes, and upload to crashlytics
Example of dockerized action - https://github.com/skx/github-action-publish-binaries - I searched in the github action marketplace a bit and found this action, which happens to be an example of custom made docker based action. - The Dockerfile sets up an env for running curl, then proceed to run the bash script to format a upload URL and post the binary via curl - End up not using this action due to poor documentation of what the file param should be. UPLOAD_URL="https://uploads.github.com/repos/${GI THUB_REPOSITORY}/releases/${RELEASE_ID}/asse ts?name=${FILENAME}" echo "Upload URL is ${UPLOAD_URL}" # Generate a temporary file. tmp=$(mktemp) # Upload the artifact - capturing HTTP response-code in our output file. response=$(curl -sSL -XPOST -H "${AUTH_HEADER}" --upload-file "${file}" --header "Content-Type:application/octet-stream" --write-out "%{http_code}" --output $tmp "${UPLOAD_URL}")
Final version of our release workflow - name: Format release url id: url-format run: | RELEASE_ID=${{ github.event.release.id }} UPLOAD_URL="https://uploads.github.com/repos/${GITHUB_ REPOSITORY}/releases/${RELEASE_ID}/assets{?name,label} " echo "::set-output name=upload_url::$UPLOAD_URL" - name: Upload Release Asset id: upload-release-asset uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.url-format.outputs.upload_url }} asset_path: ./android/app/build/outputs/apk/release/app-release.apk asset_name: liker-land-app_${{ steps.version-format.outputs.version }}.apk asset_content_type: application/zip - https://github.com/likecoin/likecoin-app/blob/d evelop/.github/workflows/release.yaml - We end up reading the actual script ran in the previous action, and modified our workflow to prepare param for running the official actions/upload-release-asset. - Notice how I gave and id to the Format release url step, and ran echo "::set-output name= To set an output that can be used as later steps’ param - Actions param currently does not support env variable directly, which kind of make it troublesome to use, can be mitigated by setting an extra step to set env as output.
Other interesting usage other than PR/release - Github action can actually hook into event other than PR / push / release, e.g. issue created, issue comment, pr comment, etc - It also integrate nicely with github API, since github token is a built in variable - This opens up many interesting way to automate github using action - Automatic publish to github page - Auto reply to first time contributor and periodically mark issue as stale without building an extra bot or using external service - Label PRs according to file change - Automatically add reviewers to new PRs - Command to rebase your PR - Auto generate release note according to commit message
Issues: - Cache size is not good, trying to cache node_module of a large webpack web app will not work (seems improving) - Cache and artifact save/load can be non-intuitive - Action is customizable by docker, but the base runner image is not - Cannot rerun failed job except force push/somehow retrigger - Cannot trigger a workflow via web UI https://www.actionspanel.app/ - Cannot use env as yaml input value
Reference - https://github.com/marketplace - https://github.com/sdras/awesome-actions - https://github.com/actions/starter-workflows

Intro to Github Actions @likecoin

  • 1.
    Intro to GithubAction @likecoin William Chong
  • 2.
    Github action: interestingfeatures - Automated workflow tool by Github - Deep integration with Github - Actions marketplace - Scheduled workflow and interesting event triggers
  • 3.
    Why? - We wereusing circleci, until... - Also we want osx build for react-native and we happen to be using github team
  • 4.
    Sample use case:liker land broswer extension - https://github.com/likecoin/liker-land-browser-extension - Webpack repo - Run eslint and webpack build for each PR to check for lint and build error
  • 5.
  • 6.
    Workflow result (2) -Linter output is viewable in Pull Request without having to go into actual workflow log to see the warnings and error. - Release note didn’t mention anything about github actions or eslint, but seems working very well. - https://github.blog/2018-12-14-intr oducing-check-runs-and-annotatio ns/
  • 7.
    .github/workflows/main.yml - https://github.com/likecoin/liker-land-browser-extension/blob/master/.github/w orkflows/main.yml Trigger: on: push: branches: - '**' pull_request: branches: -'**' Jobs: jobs: build: runs-on: ubuntu-latest Steps: ... - name: Install dependencies run: yarn - name: Lint run: yarn lint Actions: .... - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: 10 ….
  • 8.
    Trigger on: push: branches: - '**' pull_request: branches: - '**' -Triggers on every push to any branch or any pull request on any branch - Depends on different event, `github.ref` will be populated by different values. e.g. branch name, tag name, etc. - https://help.github.com/en/actions/referen ce/events-that-trigger-workflows
  • 9.
    Jobs and Steps jobs: build: runs-on:ubuntu-latest Steps: ... - name: Install dependencies run: yarn - name: Lint run: yarn lint - name: Compile run: yarn build - Jobs can be run in ubuntu,window and osx. - Support matrix definition for running in more than 1 env - Steps can either be shell commands or actions
  • 10.
    Actions Actions: .... - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version:10 …. - Actions are reusable steps packages that accept parameters - Supports js/ts or dockerized based action - For example, the actions/checkout@v2 replaces the complicated steps of checking if git exists, repository authentication, fetching repo, handle LFS and submodules - Similarly actions/setup-node helps to setup a node env, which accept a single param of node version, or an array for run matrix jobs with different node version in a workflow
  • 11.
    More workflow sample:Liker Land app release - https://github.com/likecoin/likecoin-app/releases - React-native mobile app - We want to automatically build production android apk, only after we have created a release note on github, and attach it as release binary - Also tries to automatically download recompiled dsym from itunes, and upload to crashlytics
  • 12.
    Example of dockerizedaction - https://github.com/skx/github-action-publish-binaries - I searched in the github action marketplace a bit and found this action, which happens to be an example of custom made docker based action. - The Dockerfile sets up an env for running curl, then proceed to run the bash script to format a upload URL and post the binary via curl - End up not using this action due to poor documentation of what the file param should be. UPLOAD_URL="https://uploads.github.com/repos/${GI THUB_REPOSITORY}/releases/${RELEASE_ID}/asse ts?name=${FILENAME}" echo "Upload URL is ${UPLOAD_URL}" # Generate a temporary file. tmp=$(mktemp) # Upload the artifact - capturing HTTP response-code in our output file. response=$(curl -sSL -XPOST -H "${AUTH_HEADER}" --upload-file "${file}" --header "Content-Type:application/octet-stream" --write-out "%{http_code}" --output $tmp "${UPLOAD_URL}")
  • 13.
    Final version ofour release workflow - name: Format release url id: url-format run: | RELEASE_ID=${{ github.event.release.id }} UPLOAD_URL="https://uploads.github.com/repos/${GITHUB_ REPOSITORY}/releases/${RELEASE_ID}/assets{?name,label} " echo "::set-output name=upload_url::$UPLOAD_URL" - name: Upload Release Asset id: upload-release-asset uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.url-format.outputs.upload_url }} asset_path: ./android/app/build/outputs/apk/release/app-release.apk asset_name: liker-land-app_${{ steps.version-format.outputs.version }}.apk asset_content_type: application/zip - https://github.com/likecoin/likecoin-app/blob/d evelop/.github/workflows/release.yaml - We end up reading the actual script ran in the previous action, and modified our workflow to prepare param for running the official actions/upload-release-asset. - Notice how I gave and id to the Format release url step, and ran echo "::set-output name= To set an output that can be used as later steps’ param - Actions param currently does not support env variable directly, which kind of make it troublesome to use, can be mitigated by setting an extra step to set env as output.
  • 14.
    Other interesting usageother than PR/release - Github action can actually hook into event other than PR / push / release, e.g. issue created, issue comment, pr comment, etc - It also integrate nicely with github API, since github token is a built in variable - This opens up many interesting way to automate github using action - Automatic publish to github page - Auto reply to first time contributor and periodically mark issue as stale without building an extra bot or using external service - Label PRs according to file change - Automatically add reviewers to new PRs - Command to rebase your PR - Auto generate release note according to commit message
  • 15.
    Issues: - Cache sizeis not good, trying to cache node_module of a large webpack web app will not work (seems improving) - Cache and artifact save/load can be non-intuitive - Action is customizable by docker, but the base runner image is not - Cannot rerun failed job except force push/somehow retrigger - Cannot trigger a workflow via web UI https://www.actionspanel.app/ - Cannot use env as yaml input value
  • 16.