【Github】Github Actions ~ 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2021/11/04/142835

の続き。

今回は、ハンズオン的なことをやってみる。

目次

【1】Github Actions の書き方
 1)イメージ
【2】Hello world
 1)手順
 2)動作確認
【3】actions/checkout@vX について
 1)fetch-depth
 2)ref
 3)repository
 4)path
 5)filter

【1】Github Actions の書き方

1)イメージ

* Githubイベントに応じて、Workflow (YAMLファイル)に掛かれたJobを実行していくだけ
 => Github Actions を身に着けることは、このWorkflowの書き方が分かればいいだけ。

Github Event ====> Workflow (.github/workflows/{WORKFLOW_NAME}.yml)
                   +-----------------------------------------------+
                   | workflow                                      |
                   | +-------------------------------------------+ |
                   | | job                                       | | => Runner (サーバで実行)
                   | +-------------------------------------------+ |
                   | +-------------------------------------------+ |
                   | | job                                       | | => Runner (サーバで実行)
                   | | +---------------------------------------+ | |
                   | | | step                                  | | |
                   | | +---------------------------------------+ | |
                   | | +---------------------------------------+ | |
                   | | | step                                  | | |
                   | | +---------------------------------------+ | |
                   | +-------------------------------------------+ |

【2】Hello world

https://docs.github.com/ja/actions/quickstart

に載っている Quick Start をやってみる。

1)手順

* 適当なリポジトリで以下を行う。

[1] Githubにアクセスし [Actions]を選択
[2] 「Simple workflow」内の「Set up this workflow」ボタン押下
[3] ファイル名「github-actions-demo.yml」にし、
 以下のサンプルを参考にYAMLファイルの内容を改変する
[4] 「Start Commit」ボタン押下
 => 後は、流れで、YAMLファイルをコミットしていく

github-actions-demo.yml

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the develop branch
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - run: echo "The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - run: echo "This job's status is ${{ job.status }}."

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

2)動作確認

[1] README.mdでもなんでもいいので、ソースを修正し、Commit & Push
[2] ブラウザで Github の該当のページを開き、 [Actions]を選択
 => 「X workflow runs」(X:数字 (e.g. 2))内で、
  [1]のコミット内容をトリガーに実行されている

【3】actions/checkout@vX について

* Github Actionsでソースコードをcloneする

サンプル

  uses: actions/checkout@v4
        with:
          path: path/to/hello # <- 配下にクローンする

1)fetch-depth

* 0 は、全てのブランチ・タグの全ての履歴を取得することを意味する
* Default: 1 (最新のコミットのみを取得する)

ex. Fetch all history for all tags and branches
https://github.com/actions/checkout?tab=readme-ov-file#fetch-all-history-for-all-tags-and-branches

- uses: actions/checkout@v4
  with:
    fetch-depth: 0

2)ref

* ブランチ名を指定

ex. Checkout a different branch
https://github.com/actions/checkout?tab=readme-ov-file#checkout-a-different-branch

- uses: actions/checkout@v4
  with:
    ref: my-branch

3)repository

* リポジトリ名を指定

ex. Checkout multiple repos (side by side)
https://github.com/actions/checkout?tab=readme-ov-file#checkout-multiple-repos-side-by-side

- name: Checkout
  uses: actions/checkout@v4
  with:
    path: main

- name: Checkout tools repo
  uses: actions/checkout@v4
  with:
    repository: my-org/my-tools
    path: my-tools

4)path

* リポジトリをどのパスに落とすか

ex. Checkout multiple repos (private)
https://github.com/actions/checkout?tab=readme-ov-file#checkout-multiple-repos-private

- name: Checkout
  uses: actions/checkout@v4
  with:
    path: main

- name: Checkout private tools
  uses: actions/checkout@v4
  with:
    repository: my-org/my-private-tools
    token: ${{ secrets.GH_PAT }} # `GH_PAT` is a secret that contains your PAT
    path: my-tools

5)filter

* フィルタリング機能
* Default: null

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ ワークフロー制御 ~
https://dk521123.hatenablog.com/entry/2024/01/28/004128
Github Actions ~ if ~
https://dk521123.hatenablog.com/entry/2024/03/11/000000
Github Actions ~ workflow_dispatch / 手動実行 ~
https://dk521123.hatenablog.com/entry/2024/03/12/012953
Github Actions ~ workflow_run / workflow_call ~
https://dk521123.hatenablog.com/entry/2024/02/14/232546
Github Actions ~ pull_request / pull_request_target ~
https://dk521123.hatenablog.com/entry/2024/04/10/152101
Github Actions ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/12/21/155224
Github Actions ~ エラー処理 / continue-on-error ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057
Github Actions ~ GITHUB_ENV ~
https://dk521123.hatenablog.com/entry/2023/12/29/000840
Github Actions ~ GITHUB_OUTPUT ~
https://dk521123.hatenablog.com/entry/2024/01/30/002943
Github Actions ~ GITHUB_TOKEN / permissions ~
https://dk521123.hatenablog.com/entry/2024/04/22/221027
Github Actions ~ プロパティ ~
https://dk521123.hatenablog.com/entry/2023/12/23/231250
Github Actions ~ Annotations / Matcher ~
https://dk521123.hatenablog.com/entry/2024/03/25/182153
Github Actions ~ Artifact ~
https://dk521123.hatenablog.com/entry/2024/02/13/021717
Github Actions ~ timeout-minutes ~
https://dk521123.hatenablog.com/entry/2024/02/09/000129
Github Actions ~ Github Actions環境変数
https://dk521123.hatenablog.com/entry/2024/02/26/194437
Github Actions ~ 外部シェルスクリプト実行 ~
https://dk521123.hatenablog.com/entry/2024/01/19/003044
Github Actions ~ SQL Linter ~
https://dk521123.hatenablog.com/entry/2024/03/04/180308
Github Actions ~ Scala Linter ~
https://dk521123.hatenablog.com/entry/2024/04/02/002828
Github Actions ~ Pythonを使うには ~
https://dk521123.hatenablog.com/entry/2024/02/04/011205
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
Github Actions ~ Slack連携 ~
https://dk521123.hatenablog.com/entry/2024/04/03/003053
Github Actions ~ サンプル集 ~
https://dk521123.hatenablog.com/entry/2023/12/31/231438
Github Actions ~ 設定値を切り替えることを考える ~
https://dk521123.hatenablog.com/entry/2024/02/18/232926
Github Actions ~ Self-hosted runners / 入門編 ~
https://dk521123.hatenablog.com/entry/2023/12/18/204119
Github Actions ~ Self-hosted runners / あれこれ編 ~
https://dk521123.hatenablog.com/entry/2024/02/07/002736
Github Actions ~ セキュリティ/Third-Party Github Action ~
https://dk521123.hatenablog.com/entry/2024/04/05/000136
Github Actions ~ セキュリティ/インジェクション攻撃 ~
https://dk521123.hatenablog.com/entry/2024/04/16/222419
Github Actionsで「Waiting for a runner to pick up this job...」から進まない
https://dk521123.hatenablog.com/entry/2024/01/10/195350
GitHub CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/02/17/233836
reviewdog ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/04/13/232832
reviewdog ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2024/04/18/161200
Amazon SNSAWS CLI
https://dk521123.hatenablog.com/entry/2024/02/21/141346

【Azure】PythonでAzure環境にファイルアップロードする

■ はじめに

 Azure にファイル転送することになったので、メモ。
また、そもそも Azure (アジュール) が全く分からないので、
そういったことも簡単だが調べてみた。

目次

【1】Azure の ストレージサービス
 1)Azure Blob Storage
 2)Azure Data Lake Storage Gen2
【2】関連用語
 1)コンテナ
【3】PythonでAzureにファイル転送する
 1)環境設定
 2)サンプル
【4】補足:Azure の勉強について
 1)Microsoft Learn

【1】Azure の ストレージサービス

* 代表的なものは以下の通り。
~~~~~~
1)Azure Blob Storage
2)Azure Data Lake Storage Gen2 << 今回扱うサービス
~~~~~~

他にも「Azure File ストレージ」「Azure Queue Storage」
「Azure Table Storage」がある。
(今回は、関係ないので、これ以上の深追いはしない)

1)Azure Blob Storage

* Blob (ブロブ) = Binary Large Objects
* Azure の オブジェクトストレージ
 => AWS で言うと、S3みたいなもの?

2)Azure Data Lake Storage Gen2

https://docs.microsoft.com/ja-jp/azure/storage/blobs/data-lake-storage-introduction

より抜粋
~~~~~~~~~~
* Azure Blob Storage をベースに構築された
 ビッグ データ分析専用の一連の機能
* Azure Blob Storage と Azure Data Lake Storage Gen1 の機能を集約したもの
~~~~~~~~~~

【2】関連用語

1)コンテナ

* ファイルなどのオブジェクトを格納する入れもの

【3】PythonでAzureにファイル転送する

* 以下が詳しく載っている

https://docs.microsoft.com/ja-jp/azure/storage/blobs/data-lake-storage-directory-file-acl-python

1)環境設定

# Python 向けの Azure Data Lake Storage クライアント ライブラリをインストール
pip install azure-storage-file-datalake

2)サンプル

* 大きく分けて、3段階(下記サンプルのStep1~Step3を参照)

例1:既存コンテナにファイルを転送する

from azure.storage.filedatalake import DataLakeServiceClient

# Step1: アカウント キーを使用して接続する

# Azureストレージ アカウントの名前
storage_account_name = "demo_storege_user"
# ストレージ アカウントのアクセス キー
storage_account_key = "xxxxxxx"

service_client = DataLakeServiceClient(
  account_url=f"https://{storage_account_name}.dfs.core.windows.net",
  credential=storage_account_key
)

# Step2: 既存コンテナを取得する
file_system_client = service_client.get_file_system_client(
  file_system="my-file-system"
)
# コンテナが存在しない場合は、作成する
# file_system_client = service_client.create_file_system(
#   file_system="my-file-system"
# )

# Step3: ファイルをディレクトリにアップロードする
#  => 参考にしたサイトの
#  「大きなファイルをディレクトリにアップロードする」参照
directory_client = file_system_client.get_directory_client("my-directory")
file_client = directory_client.get_file_client("azure-uploaded-file.txt")

# バイナリファイルなら "r" => "rb" に変更
with open("C:\\local-file-to-upload.txt", "r") as local_file:
  file_contents = local_file.read()
  # upload_data メソッドを使用して大きなファイルをアップロード
  file_client.upload_data(file_contents, overwrite=True)
  # 大きいファイルをサポートしていない場合
  # file_client.append_data(
  #   data=file_contents,
  #   offset=0,
  #   length=len(file_contents)
  # )
  # file_client.flush_data(len(file_contents))

【4】補足:Azure の勉強について

* Azure は、AWSと同じで、無料枠でもアカウント作成にはクレジットが必須
* ただし、Microsoft Learnという学習サイトは用意されている

1)Microsoft Learn

* ハンズオン環境としてAzure環境が利用できるオンライン学習サイト
 => 本格的ではなく、とりあえず教養として勉強したいので、
  こういったサイトは、エンジニアとして、ありがたい、、、
 => 今回の記事に関わる以下のカリキュラムがある
 + Azure ストレージ サービスについて
 + Azure Storage アカウントの作成

公式サイト
https://docs.microsoft.com/ja-jp/learn/

参考文献

Microsoft Learn
https://atmarkit.itmedia.co.jp/ait/articles/1811/07/news015.html
https://www.acrovision.jp/service/azure/?p=1258

関連記事

Azure CLI ~ az storage azcopy blob upload ~
https://dk521123.hatenablog.com/entry/2022/11/11/111614

【AWS】Secrets Manager ~ AWS CLI 編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/03/12/220717
https://dk521123.hatenablog.com/entry/2021/10/05/105550

の続き。

 AWS Secrets Manager でのシークレットの作成を
AWS CLIで行う必要ができたので、メモ。
ついでに過去の記事も整理。

目次

【1】コマンド
 1)list-secrets
 2)get-secret-value
 3)create-secret
 4)put-secret-value
【2】サンプル
 例1:list-secrets(シークレット一覧)
 例2:get-secret-value(シークレットの取得)
 例3:create-secret (シークレットの作成)

【1】コマンド

* 紹介していないコマンドについては、
 以下の公式ドキュメントを参照。

https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/index.html
https://docs.aws.amazon.com/ja_jp/secretsmanager/latest/userguide/tutorials_basic.html#tutorial-basic-step2
https://docs.aws.amazon.com/ja_jp/secretsmanager/latest/userguide/tutorials_db-rotate.html

1)list-secrets

* シークレット一覧表示

2)get-secret-value

* シークレットの取得。

公式ドキュメント
https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/get-secret-value.html

3)create-secret

* シークレットの作成

公式ドキュメント
https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/create-secret.html

4)put-secret-value

【2】サンプル

例1:list-secrets(シークレット一覧)

# 一覧がJSON形式で返ってくる
aws secretsmanager list-secrets

# Jq を利用してシークレット名を一覧で取得
aws secretsmanager list-secrets | jq ".SecretList[] .Name"

例2:get-secret-value(シークレットの取得)

# 権限があれば、JSONで結果が返ってくる
aws secretsmanager get-secret-value \
--region us-east-1 --secret-id sample-rds-key

# jqコマンドを使用してJSONをパースする (sudo apt install jq などでインストール)
# jq の -r オプション : 囲み文字であるダブルクォートが除去
aws secretsmanager get-secret-value \
--region us-east-1 --secret-id sample-rds-key | jq -r .SecretString | jq -r .username

# 変数に格納する
secret=$(aws secretsmanager get-secret-value  --region us-east-1 --secret-id sample-rds-key | jq .SecretString | jq fromjson)
user=$(echo $secret | jq -r .username)
password=$(echo $secret | jq -r .password)

# 確認
echo $user

例3:create-secret (シークレットの作成)

# JSONファイルをシークレットとして作成(登録?)
aws secretsmanager create-secret \
--region us-east-1 \
--name demo-secret --description "This is a sample" \
--secret-string file://input_demo.json
# 「file://」は重要。詳細は、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2022/06/01/154618

<出力結果 (作成したらARNなどが返ってくる)>

{
  "ARN": "arn:aws:secretsmanager:us-east-1:000000000:secret:demo-secret-xxxxx",
  "Name": "demo-secret",
  "VersionId": "xxxx-xxxx-xxxx-xxxx"
}

input_demo.json

{
  "Key1": "Hello",
  "Key2": "World",
  "Key3": "!!!"
}

参考文献

https://qiita.com/inductor/items/ac8f4a1a947f0d3bbaa6

関連記事

機密データの管理 ~ Secrets Manager 編 ~
https://dk521123.hatenablog.com/entry/2020/03/12/220717
機密データの管理 ~ Secrets Manager / boto3 編 ~
https://dk521123.hatenablog.com/entry/2021/10/05/105550
エラー「Invalid length for parameter SAMLMetadataDocument」が発生
https://dk521123.hatenablog.com/entry/2022/06/01/154618
AWS CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/12/01/000000