Skip to content

Commit 2ed0ce4

Browse files
authored
Merge pull request #6 from nginxinc/bangbingsyb/multi-file-config
Support sync multiple files NGINX configuration to an NGINX for Azure deployment
2 parents 034e778 + 80713d3 commit 2ed0ce4

File tree

4 files changed

+123
-52
lines changed

4 files changed

+123
-52
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The following example updates the configuration of a NGINX deployment in Azure e
1111
```yaml
1212
# File: .github/workflows/nginxForAzureDeploy.yml
1313

14-
name: Sync configuration to NGINX for Azure
14+
name: Sync the NGINX configuration from the Git repository to an NGINX for Azure deployment
1515
on:
1616
push:
1717
branches:
@@ -31,21 +31,23 @@ jobs:
3131
with:
3232
creds: ${{ secrets.AZURE_CREDENTIALS }}
3333

34-
- name: 'Sync NGINX configuration to NGINX on Azure instance'
34+
- name: 'Sync the NGINX configuration from the Git repository to the NGINX for Azure deployment'
3535
uses: nginxinc/nginx-for-azure-deploy-action@v1
3636
with:
3737
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
3838
resource-group-name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }}
3939
nginx-deployment-name: ${{ secrets.NGINX_DEPLOYMENT_NAME }}
40-
nginx-config-relative-file-path: ${{ secrets.NGINX_CONFIG_FILE }}
40+
nginx-config-directory-path: config/
41+
nginx-root-config-file: nginx.conf
42+
transformed-nginx-config-directory-path: /etc/nginx/
4143
```
4244
4345
### Sample workflow that authenticates with Azure using OIDC
4446
4547
```yaml
4648
# File: .github/workflows/nginxForAzureDeploy.yml
4749

48-
name: Sync configuration to NGINX for Azure
50+
name: Sync the NGINX configuration from the Git repository to an NGINX for Azure deployment
4951
on:
5052
push:
5153
branches:
@@ -71,11 +73,13 @@ jobs:
7173
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
7274
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
7375

74-
- name: 'Sync NGINX configuration to NGINX on Azure instance'
76+
- name: 'Sync the NGINX configuration from the Git repository to the NGINX for Azure deployment'
7577
uses: nginxinc/nginx-for-azure-deploy-action@v1
7678
with:
7779
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
7880
resource-group-name: ${{ secrets.AZURE_RESOURCE_GROUP_NAME }}
7981
nginx-deployment-name: ${{ secrets.NGINX_DEPLOYMENT_NAME }}
80-
nginx-config-relative-file-path: ${{ secrets.NGINX_CONFIG_FILE }}
82+
nginx-config-directory-path: config/
83+
nginx-root-config-file: nginx.conf
84+
transformed-nginx-config-directory-path: /etc/nginx/
8185
```

action.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
name: 'NGINX configuration sync'
2-
description: 'The action synchronizes NGINX configuration from a Git repository to an NGINX deployment on Azure.'
2+
description: 'The action synchronizes NGINX configuration from a Git repository to an NGINX for Azure deployment.'
33
inputs:
44
subscription-id:
5-
description: 'The Azure subscription ID of the NGINX deployment'
5+
description: 'The Azure subscription ID of the NGINX for Azure deployment.'
66
required: true
77
resource-group-name:
8-
description: 'The resource group of the NGINX deployment'
8+
description: 'The resource group of the NGINX for Azure deployment.'
99
required: true
1010
nginx-deployment-name:
11-
description: 'The name of the NGINX deployment'
11+
description: 'The name of the NGINX for Azure deployment.'
1212
required: true
13-
nginx-config-relative-file-path:
14-
description: 'The relative file path of the NGINX configuration file in the Git repository'
13+
nginx-config-directory-path:
14+
description: 'The NGINX configuration directory path relative to the root of the Git repository, example: "config/".'
1515
required: true
16-
default: './config/nginx.conf'
16+
nginx-root-config-file:
17+
description: >
18+
'The root NGINX configuration file path relative to the NGINX configuration directory in the Git repository, example: "nginx.conf".'
19+
required: false
20+
default: 'nginx.conf'
21+
transformed-nginx-config-directory-path:
22+
description: >
23+
'The transformed absolute path of the NGINX configuration directory in NGINX for Azure deployment, example: "/etc/nginx/".
24+
If the "include" directive in the NGINX configuration files uses absolute paths, the path transformation
25+
can be used to overwrite the file paths when the action synchronizes the files to the NGINX for Azure deployment.'
26+
required: false
27+
default: ''
1728
runs:
1829
using: "composite"
1930
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 }}
31+
- name: 'Synchronize NGINX configuration from the Git repository to an NGINX for Azure deployment'
32+
run: ${{github.action_path}}/src/deploy-config.sh ${{ inputs.subscription-id }} ${{ inputs.resource-group-name }} ${{ inputs.nginx-deployment-name }} ${{ inputs.nginx-config-directory-path }} ${{ inputs.nginx-root-config-file }} ${{ inputs.transformed-nginx-config-directory-path }}
2233
shell: bash

src/deploy-config.sh

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,100 @@
22
set -euo pipefail
33
IFS=$'\n\t'
44

5-
subscriptionId=$1
6-
resourceGroupName=$2
7-
nginxDeploymentName=$3
8-
nginxConfigurationFile=$4
5+
subscription_id=$1
6+
resource_group_name=$2
7+
nginx_deployment_name=$3
8+
config_dir_path=$4
9+
root_config_file=$5
10+
transformed_config_dir_path=${6:-''}
911

10-
# Read and encode the NGINX configuration file content.
11-
if [ -f "$nginxConfigurationFile" ]
12+
# Validation and preprocessing
13+
14+
if [[ "$config_dir_path" = /* ]]
15+
then
16+
echo "The NGINX configuration directory path in the repository '$config_dir_path' must be a relative path."
17+
exit 1
18+
elif [[ ! "$config_dir_path" = */ ]]
19+
then
20+
echo "The NGINX configuration directory path '$config_dir_path' does not end with '/'. Appending a trailing '/'."
21+
config_dir_path="$config_dir_path/"
22+
fi
23+
24+
if [[ -d "$config_dir_path" ]]
25+
then
26+
echo "The NGINX configuration directory '$config_dir_path' was found."
27+
else
28+
echo "The NGINX configuration directory '$config_dir_path' does not exist."
29+
exit 1
30+
fi
31+
32+
if [[ "$root_config_file" = /* ]]
33+
then
34+
echo "The NGINX configuration root file path '$root_config_file' must be a relative path to the NGINX configuration directory."
35+
exit 1
36+
fi
37+
38+
# Remove the leading './' from the root configuration file path if any.
39+
root_config_file=${root_config_file/#'./'/}
40+
41+
root_config_file_repo_path="$config_dir_path$root_config_file"
42+
if [[ -f "$root_config_file_repo_path" ]]
1243
then
13-
echo "The NGINX configuration file was found."
44+
echo "The root NGINX configuration file '$root_config_file_repo_path' was found."
1445
else
15-
echo "The NGINX configuration file $nginxConfigurationFile does not exist."
16-
exit 2
46+
echo "The root NGINX configuration file '$root_config_file_repo_path' does not exist."
47+
exit 1
48+
fi
49+
50+
if [[ -n "$transformed_config_dir_path" ]]
51+
then
52+
if [[ ! "$transformed_config_dir_path" = /* ]]
53+
then
54+
echo "The specified transformed NGINX configuration directory path '$transformed_config_dir_path' must be an absolute path that starts with '/'."
55+
exit 1
56+
elif [[ ! "$transformed_config_dir_path" = */ ]]
57+
then
58+
echo "The specified transformed NGINX configuration directory path '$transformed_config_dir_path' does not end with '/'. Appending a trailing '/'."
59+
transformed_config_dir_path="$transformed_config_dir_path/"
60+
fi
1761
fi
1862

19-
encodedConfigContent=$(base64 "$nginxConfigurationFile")
20-
echo "Base64 encoded NGINX configuration content"
21-
echo "$encodedConfigContent"
63+
transformed_root_config_file_path="$transformed_config_dir_path$root_config_file"
64+
echo "The transformed root NGINX configuration file path is '$transformed_root_config_file_path'."
65+
66+
# Create a NGINX configuration tarball.
67+
68+
config_tarball="nginx-config.tar.gz"
69+
70+
echo "Creating a tarball from the NGINX configuration directory."
71+
tar -cvzf "$config_tarball" -C "$config_dir_path" --xform s:'./':"$transformed_config_dir_path": .
72+
echo "Successfully created the tarball from the NGINX configuration directory."
73+
74+
echo "Listing the NGINX configuration file paths in the tarball."
75+
tar -tf "$config_tarball"
76+
77+
encoded_config_tarball=$(base64 "$config_tarball")
78+
echo "The base64 encoded NGINX configuration tarball"
79+
echo "$encoded_config_tarball"
2280
echo ""
2381

24-
# Deploy the configuration to the NGINX instance on Azure using an ARM template.
82+
# Synchronize the NGINX configuration tarball to the NGINX for Azure deployment.
83+
2584
uuid="$(cat /proc/sys/kernel/random/uuid)"
26-
templateFile="template-$uuid.json"
27-
templateDeploymentName="${nginxDeploymentName:0:20}-$uuid"
85+
template_file="template-$uuid.json"
86+
template_deployment_name="${nginx_deployment_name:0:20}-$uuid"
2887

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"
88+
wget -O "$template_file" https://raw.githubusercontent.com/nginxinc/nginx-for-azure-deploy-action/main/src/nginx-for-azure-configuration-template.json
89+
echo "Downloaded the ARM template for synchronizing NGINX configuration."
90+
cat "$template_file"
3291
echo ""
3392

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"
93+
echo "Synchronizing NGINX configuration"
94+
echo "Subscription ID: $subscription_id"
95+
echo "Resource group name: $resource_group_name"
96+
echo "NGINX for Azure deployment name: $nginx_deployment_name"
97+
echo "ARM template deployment name: $template_deployment_name"
3998
echo ""
4099

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
100+
az account set -s "$subscription_id" --verbose
101+
az deployment group create --name "$template_deployment_name" --resource-group "$resource_group_name" --template-file "$template_file" --parameters nginxDeploymentName="$nginx_deployment_name" rootFile="$transformed_root_config_file_path" tarball="$encoded_config_tarball" --verbose

src/nginx-for-azure-configuration-template.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
"nginxDeploymentName": {
66
"type": "string",
77
"metadata": {
8-
"description": "The name of the NGINX deployment resource to deploy the configuration."
8+
"description": "The name of the NGINX for Azure deployment to synchronize the configuration."
99
}
1010
},
11-
"rootConfigFilePath": {
11+
"rootFile": {
1212
"type": "string",
1313
"defaultValue": "nginx.conf",
1414
"metadata": {
15-
"description": "The file path of the root NGINX configuration file"
15+
"description": "The file path of the root NGINX configuration file."
1616
}
1717
},
18-
"rootConfigContent": {
18+
"tarball": {
1919
"type": "string",
2020
"metadata": {
21-
"description": "The based64 encoded content of the root NGINX configuration file"
21+
"description": "The based64 encoded NGINX configuration tarball."
2222
}
2323
}
2424
},
@@ -29,13 +29,10 @@
2929
"apiVersion": "2021-05-01-preview",
3030
"name": "[concat(parameters('nginxDeploymentName'), '/default')]",
3131
"properties": {
32-
"rootFile": "[parameters('rootConfigFilePath')]",
33-
"files": [
34-
{
35-
"content": "[parameters('rootConfigContent')]",
36-
"virtualPath": "[parameters('rootConfigFilePath')]"
37-
}
38-
]
32+
"rootFile": "[parameters('rootFile')]",
33+
"package": {
34+
"data": "[parameters('tarball')]"
35+
}
3936
}
4037
}
4138
]

0 commit comments

Comments
 (0)