何度も調べて何度もテストしたりしたので、多用するものをまとめていきたい。
// 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を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等
分 時 日 月 曜日
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 }}"
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-1
やrefs/tags/v1.0.0
。##*/
はrefs/heads/
やrefs/tags/
部分を削除するので、feature-branch-1
,v1.0.0
を取得できる。
- 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。
- 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/contexts
https://docs.github.com/ja/actions/learn-github-actions/workflow-syntax-for-github-actions
バッジを受け取った著者にはZennから現金やAmazonギフトカードが還元されます。