Skip to content

Commit fc48387

Browse files
authored
Merge pull request #1 from nginxinc/bangbingsyb/git-action-poc
POC for single NGINX configuration file sync action
2 parents 252f0c3 + fedb02c commit fc48387

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# NGINX for Azure Deployment Action
2+
3+
This action syncs NGINX configuration files in the repository to an NGINX deployment in Azure. It enables continuous deployment scenarios where the configuration of the NGINX deployment is automatically updated when changes are made through GitHub workflows.
4+
5+
## Usage example
6+
7+
The following example updates the configuration of a NGINX deployment in Azure each time a change is made to the configuration file in config folder in the `main` branch.
8+
9+
### Sample workflow that authenticates with Azure using Azure Service Principal with a secret
10+
11+
```yaml
12+
# File: .github/workflows/nginxForAzureDeploy.yml
13+
14+
name: Sync configuration to NGINX for Azure
15+
on:
16+
push:
17+
branches:
18+
- main
19+
paths:
20+
- config/**
21+
22+
jobs:
23+
Deploy-NGINX-Configuration:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: 'Checkout repository'
27+
uses: actions/checkout@v2
28+
29+
- name: 'Run Azure Login using Azure Service Principal with a secret'
30+
uses: azure/login@v1
31+
with:
32+
creds: ${{ secrets.AZURE_CREDENTIALS }}
33+
34+
- name: 'Sync NGINX configuration to NGINX on Azure instance'
35+
uses: nginxinc/nginx-for-azure-deploy-action@v1
36+
with:
37+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
38+
resource-group-name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }}
39+
nginx-deployment-name: ${{ secrets.NGINX_DEPLOYMENT_NAME }}
40+
nginx-config-file-path: ${{ secrets.NGINX_CONFIG_FILE }}
41+
```
42+
43+
### Sample workflow that authenticates with Azure using OIDC
44+
45+
```yaml
46+
# File: .github/workflows/nginxForAzureDeploy.yml
47+
48+
name: Sync configuration to NGINX for Azure
49+
on:
50+
push:
51+
branches:
52+
- main
53+
paths:
54+
- config/**
55+
56+
permissions:
57+
id-token: write
58+
contents: read
59+
60+
jobs:
61+
Deploy-NGINX-Configuration:
62+
runs-on: ubuntu-latest
63+
steps:
64+
- name: 'Checkout repository'
65+
uses: actions/checkout@v2
66+
67+
- name: 'Run Azure Login using OIDC'
68+
uses: azure/login@v1
69+
with:
70+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
71+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
72+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
73+
74+
- name: 'Sync NGINX configuration to NGINX on Azure instance'
75+
uses: nginxinc/nginx-for-azure-deploy-action@v1
76+
with:
77+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
78+
resource-group-name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }}
79+
nginx-deployment-name: ${{ secrets.NGINX_DEPLOYMENT_NAME }}
80+
nginx-config-file-path: ${{ secrets.NGINX_CONFIG_FILE }}
81+
```

action.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: 'NGINX configuration sync'
2+
description: 'The action synchronizes NGINX configuration from a Git repository to an NGINX deployment on Azure.'
3+
inputs:
4+
subscription-id:
5+
description: 'The Azure subscription ID of the NGINX deployment'
6+
required: true
7+
resource-group-name:
8+
description: 'The resource group of the NGINX deployment'
9+
required: true
10+
nginx-deployment-name:
11+
description: 'The name of the NGINX deployment'
12+
required: true
13+
nginx-config-relative-file-path:
14+
description: 'The relative file path of the NGINX configuration file in the Git repository'
15+
required: true
16+
default: './config/nginx.conf'
17+
runs:
18+
using: "composite"
19+
steps:
20+
- name: 'Deploy configuration to the NGINX deployment in Azure'
21+
run: ${{github.action_path}}/src/deploy-config.sh ${{ inputs.subscription-id }} ${{ inputs.resource-group-name }} ${{ inputs.nginx-deployment-name }} ${{ inputs.nginx-config-relative-file-path }}
22+
shell: bash

src/deploy-config.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
subscriptionId=$1
6+
resourceGroupName=$2
7+
nginxDeploymentName=$3
8+
nginxConfigurationFile=$4
9+
10+
# Read and encode the NGINX configuration file content.
11+
if [ -f "$nginxConfigurationFile" ]
12+
then
13+
echo "The NGINX configuration file was found."
14+
else
15+
echo "The NGINX configuration file $nginxConfigurationFile does not exist."
16+
exit 2
17+
fi
18+
19+
encodedConfigContent=$(base64 $nginxConfigurationFile)
20+
echo "Base64 encoded NGINX configuration content"
21+
echo "$encodedConfigContent"
22+
echo ""
23+
24+
# Deploy the configuration to the NGINX instance on Azure using an ARM template.
25+
uuid="$(cat /proc/sys/kernel/random/uuid)"
26+
templateFile="template-$uuid.json"
27+
templateDeploymentName="${nginxDeploymentName:0:20}-$uuid"
28+
29+
wget -O "$templateFile" https://raw.githubusercontent.com/nginxinc/nginx-for-azure-deploy-action/main/src/nginx-for-azure-configuration-template.json
30+
echo "Downloaded the ARM template for deploying NGINX configuration"
31+
cat "$templateFile"
32+
echo ""
33+
34+
echo "Deploying NGINX configuration"
35+
echo "Subscription: $subscriptionId"
36+
echo "Resource group: $resourceGroupName"
37+
echo "NGINX deployment name: $nginxDeploymentName"
38+
echo "Template deployment name: $templateDeploymentName"
39+
echo ""
40+
41+
az account set -s "$subscriptionId" --verbose
42+
az deployment group create --name "$templateDeploymentName" --resource-group "$resourceGroupName" --template-file "$templateFile" --parameters nginxDeploymentName="$nginxDeploymentName" rootConfigContent="$encodedConfigContent" --verbose
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"nginxDeploymentName": {
6+
"type": "string",
7+
"metadata": {
8+
"description": "The name of the NGINX deployment resource to deploy the configuration."
9+
}
10+
},
11+
"rootConfigFilePath": {
12+
"type": "string",
13+
"defaultValue": "nginx.conf",
14+
"metadata": {
15+
"description": "The file path of the root NGINX configuration file"
16+
}
17+
},
18+
"rootConfigContent": {
19+
"type": "string",
20+
"metadata": {
21+
"description": "The based64 encoded content of the root NGINX configuration file"
22+
}
23+
}
24+
},
25+
"variables": {},
26+
"resources": [
27+
{
28+
"type": "NGINX.NGINXPLUS/nginxDeployments/configurations",
29+
"apiVersion": "2021-05-01-preview",
30+
"name": "[concat(parameters('nginxDeploymentName'), '/default')]",
31+
"properties": {
32+
"rootFile": "[parameters('rootConfigFilePath')]",
33+
"files": [
34+
{
35+
"content": "[parameters('rootConfigContent')]",
36+
"virtualPath": "[parameters('rootConfigFilePath')]"
37+
}
38+
]
39+
}
40+
}
41+
]
42+
}

0 commit comments

Comments
 (0)