😀

Azure と GitHub Actions の連携を CLI だけで試してみた

に公開

Azure と GitHub Actions の連携を CLI だけで試してみました。

自分の検証環境

$ az version
{
  "azure-cli": "2.43.0",
  "azure-cli-core": "2.43.0",
  "azure-cli-telemetry": "1.0.8",
  "extensions": {}
}

$ gh --version
gh version 2.20.2 (2022-11-15)
https://github.com/cli/cli/releases/tag/v2.20.2

Azure CLI と GitHub CLI だけで検証

prefix=mnrgapp
region=japaneast

gid=$(az group create \
  --name ${prefix}-rg \
  --location $region \
  --query id \
  --output tsv)

auth=$(az ad sp create-for-rbac \
  --name ${prefix} \
  --years 100 \
  --role Contributor \
  --sdk-auth true \
  --scopes $gid)

gh repo create ${prefix} \
  --private

gh secret set AZURE_CREDENTIALS \
  --repo mnrst/${prefix} \
  --body "$auth"

mkdir ${prefix}

cd ${prefix}

mkdir -p .github/workflows

cat <<"EOF" > .github/workflows/azure-login-test.yml
on: [push]

name: AzureLoginSample

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Login with Azure
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      - name: Azure CLI script
        uses: azure/CLI@v1
        with:
          azcliversion: 2.43.0
          inlineScript: |
            az group list -o table
EOF

git init

git add -A

git commit -m "first commit"

git branch -m main

git remote add origin https://github.com/mnrst/${prefix}.git

git push -u origin main

検証結果

GitHub Actions から Azure サービスプリンシパルで許可したリソースグループだけがリストに表示されました。

image.png

参考

https://learn.microsoft.com/ja-jp/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux

https://cli.github.com/manual/

Discussion