Movatterモバイル変換


[0]ホーム

URL:


Zenn
masaaaniamasaaania
😺

Github Actions チートシート

に公開
2022/07/01

概要

何度も調べて何度もテストしたりしたので、多用するものをまとめていきたい。

項目

push時に実行

// feature/aaaで動く。 feature/aaa/bbbでは動かないon:  push:    branches:      - feature/*// feature/aaa, feature/aaa/bbbで動くon:  push:    branches:      - feature/**// なにかしらのtagがpushされたときに実行、branchのpushは無視on:  push:    tags: [ '**' ]    branches-ignore: [ '**' ]    // 指定したpathの変更だけでは実行しないon:  push:    branches:      - main    paths-ignore:      - '*.md'      - 'docs/**'

フィルターパターンのチートシート

マニュアル実行

on:  workflow_dispatch:    inputs:      env:        description: "実行環境"        required: true        default: "dev"      year:        description: "対象年 (ex. 2021)"        required: true      month:        description: "対象月 (ex. 1~12)"        required: true      day:        description: "対象日 (ex. 1~31)"        required: true      hour:        description: "対象時間 (ex. 0~23)"        required: truejobs:  my-job:    runs-on: ubuntu-latest    steps:      - name: Use the inputs        run: |  echo ${{ github.event.inputs.env }}  echo ${{ github.event.inputs.year }}  echo ${{ github.event.inputs.month }}  echo ${{ github.event.inputs.day }}  echo ${{ github.event.inputs.hour }}

workflow_dispatchをapi実行

↑で指定したworkflow_dispatchをcurlで実行する。

curl -X POST \    -H "Accespt: application/vnd.github.v3+json" \    -H "Authorization: token ${TOKEN}" \    https://api.github.com/repos/{$YOUR_ORG}/${YOUR_REPO}/actions/workflows/${YOUR_WORKFLOW_FILENAME}/dispatches \    -d '{"ref":"master", "inputs":{"env":"'${env}'","year":"'${year}'", "month":"'${month}'", "day":"'${day}'", "hour":"'${hour}'"}}'

${TOKEN}はこのworkflowを実行できるユーザーのPersonal Access Token等

cron実行

分 時 日 月 曜日
timezoneはUTC

┌───────────── minute (0 - 59)│ ┌───────────── hour (0 - 23)│ │ ┌───────────── day of the month (1 - 31)│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)│ │ │ │ ││ │ │ │ ││ │ │ │ │* * * * *on:  schedule:    - cron: "0,30 1-3 * * *"

環境変数追加

steps:  - name: Set the value    id: step_one    run: echo "user_name=xxx" >> $GITHUB_ENV  - name: Use the value    id: step_two    run: echo "${{ env.user_name }}"

outputパラメータ追加

steps:  - name: Set the value    id: step_one    run: echo "::set-output name=user_name::xxx"  - name: Use the value    id: step_two    run: echo "${{ steps.step_one.outputs.user_name }}"

ログ内の特定文字列をマスクする

"***"でマスクする。

steps:  - name: Set mask    run: echo "::add-mask::$MY_PASSWORD"

ファイルをアーカイブする

- name: Archive test result      uses: actions/upload-artifact@v1      with:        name: test-result        path: $GITHUB_WORKSPACE/target/surefire-reports

$GITHUB_WORKSPACEはデフォルト環境変数。
actions/checkoutがgitレポジトリの内容をチェックアウトしてくるディレクトリパス。

タグ・ブランチ名を取得する

- name: tag  run: echo ${GITHUB_REF##*/}

$GITHUB_REFはワークフローをトリガーしたブランチまたはタグref。
たとえばrefs/heads/feature-branch-1refs/tags/v1.0.0
##*/refs/heads/refs/tags/部分を削除するので、feature-branch-1,v1.0.0を取得できる。

slackチャンネルに通知を送る

- name: Send to slack channel  run: |    curl -X POST \    -d "payload={\"channel\": \"#YOUR_CHANNEL\", \"username\": \"YOUR_USER_NAME\", \"text\": \"YOUR_MESSAGE\", \"icon_emoji\": \":smile:\"}" \    ${{ secrets.SLACK_NOTIFICATION_URL }}

SLACK_NOTIFICATION_URLはIncoming WebhookのURL。

slackチャンネルにファイルをuploadする

- name: Upload file to slack channel  run: |    curl -F file=@$GITHUB_WORKSPACE/YOUR_FILE_NAME.csv \    -F channels=#YOUR_CHANNEL \    -H "Authorization: Bearer $SLACK_APP_OAUTH_TOKEN" \    https://slack.com/api/files.upload

ファイルのuploadはIncoming WebhookではできないのでSlackアプリを作成し、指定するチャンネルにインストールしておく必要がある。
SLACK_APP_OAUTH_TOKENはslackアプリのOAUTHトークン。

よく確認するリンク

デフォルト環境変数一覧

https://docs.github.com/ja/actions/learn-github-actions/environment-variables#default-environment-variables

ワークフローコマンド

https://docs.github.com/ja/actions/learn-github-actions/workflow-commands-for-github-actions#setting-an-output-parameter

コンテキスト

https://docs.github.com/ja/actions/learn-github-actions/contexts

ワークフロー構文

https://docs.github.com/ja/actions/learn-github-actions/workflow-syntax-for-github-actions

バッジを贈って著者を応援しよう

バッジを受け取った著者にはZennから現金やAmazonギフトカードが還元されます。


[8]ページ先頭

©2009-2025 Movatter.jp