Skip to content

Commit

Permalink
feat: Add terraform apply action
Browse files Browse the repository at this point in the history
  • Loading branch information
pasqualedevita committed Jan 5, 2023
1 parent 0c92643 commit 31282cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# github-self-hosted-runner-azure-create-action
# terraform-apply-azure-action
68 changes: 17 additions & 51 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Create GitHub self hosted runner"
description: "Create a GitHub self hosted runner on Azure"
name: "Run terraform apply"
description: "Run terraform apply on Azure"

inputs:
client_id:
Expand All @@ -11,69 +11,35 @@ inputs:
subscription_id:
description: "Azure subscription Id"
required: true
container_app_environment_name:
description: "Azure container app environment name"
dir:
description: "terraform directory"
required: true
resource_group_name:
description: "Azure container app environment resource group name"
required: true
pat_token:
description: "GitHub PAT token to access to APIs to manage self hosted runner"
required: true
self_hosted_runner_image_tag:
description: "Self hosted runner image tag from https://github.com/pagopa/github-self-hosted-runner-azure/pkgs/container/github-self-hosted-runner-azure"
required: true
default: "v1.0.3@sha256:dde85b07a1de09d283b85c2d35bc3c399d4e5c8a1f60ac882353f62549e6359f"

outputs:
runner_name:
value: ${{ steps.create_github_runner.outputs.AZURE_CONTAINER_APP_NAME }}
description: 'Azure Container App Name'

runs:
using: "composite"
steps:
- name: Login
id: login
uses: azure/login@v1
# from https://github.com/Azure/login/commits/master
uses: azure/login@92a5484dfaf04ca78a94597f4f19fea633851fa2
with:
client-id: ${{ inputs.client_id }}
tenant-id: ${{ inputs.tenant_id }}
subscription-id: ${{ inputs.subscription_id }}

- name: Create GitHub Runner
id: create_github_runner
- name: Terraform apply
shell: bash
run: |
### 0) Configure runner names and ids
TIMESTAMP=$(date +%s)
SELF_HOSTED_RUNNER_IMAGE="ghcr.io/pagopa/github-self-hosted-runner-azure:${{ inputs.self_hosted_runner_image_tag }}"
AZURE_RESOURCE_GROUP_NAME="${{ inputs.resource_group_name }}"
AZURE_CONTAINER_APP_ENVIRONMENT_NAME="${{ inputs.container_app_environment_name }}"
AZURE_CONTAINER_APP_NAME="runner-${{ github.run_id }}${TIMESTAMP}"
echo "[INFO] AZURE_CONTAINER_APP_NAME: ${AZURE_CONTAINER_APP_NAME}"
echo "::set-output name=AZURE_CONTAINER_APP_NAME::${AZURE_CONTAINER_APP_NAME}"
export PATH="${HOME}/bin:$PATH"
export ARM_CLIENT_ID=${{ inputs.client_id }}
export ARM_TENANT_ID=${{ inputs.tenant_id }}
export ARM_SUBSCRIPTION_ID=${{ inputs.subscription_id }}
export ARM_USE_OIDC=true
export ARM_USE_AZUREAD=true
export ARM_STORAGE_USE_AZUREAD=true
### 1) Register runner into current GitHub Repository
GITHUB_TOKEN=$(curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ inputs.pat_token }}" \
https://api.github.com/repos/${{ github.repository }}/actions/runners/registration-token | jq ".token" -r)
cd ${{ inputs.dir }}
### 2) Create container app linked to created runner
az config set extension.use_dynamic_install=yes_without_prompt
terraform apply -lock-timeout=3000s -auto-approve -input=false tfplan
az containerapp create \
--name "${AZURE_CONTAINER_APP_NAME}" \
--resource-group "${AZURE_RESOURCE_GROUP_NAME}" \
--image "${SELF_HOSTED_RUNNER_IMAGE}" \
--min-replicas 1 \
--max-replicas 1 \
--environment "${AZURE_CONTAINER_APP_ENVIRONMENT_NAME}" \
--secrets github-token="${GITHUB_TOKEN}" \
--env-vars \
LABELS="${AZURE_CONTAINER_APP_NAME}" \
GITHUB_REPOSITORY="https://github.com/${{ github.repository }}" \
GITHUB_TOKEN=secretref:github-token
rm -rf tfplan

0 comments on commit 31282cf

Please sign in to comment.