Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEVOPS-360] Fix Next.js environmentVariables not being updated in ephemeral deployments #102

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions .github/workflows/ephemeral-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ jobs:
TARGET_PORT=$(find . -iname "values.yaml" -exec grep "targetPort: " {} \; | awk -F ': ' '{print $2}' | uniq)
echo "targetPort=${TARGET_PORT}" >> $GITHUB_OUTPUT

- name: Check if app is a Next.js application
if: contains(steps.get-envs.outputs.environmentVariables, 'BASE_URL')
id: check-next
run: |
echo "next-app=true" >> $GITHUB_OUTPUT

- name: Check if Next.js app has already been deployed
if: contains(steps.get-envs.outputs.environmentVariables, 'BASE_URL')
id: check-deploy
Expand All @@ -146,6 +152,13 @@ jobs:
echo "existingAppUrl=https://${HOSTNAME}" >> $GITHUB_OUTPUT
fi

- name: Remove keys from environment variables
id: remove-prime-keys
if: contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PUBLIC_KEY') || contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PRIVATE_KEY')
run: |
ENVIRONMENT_VARIABLES=$(echo "${{ steps.get-envs.outputs.environmentVariables }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT

- name: Update Next URL variables with existing container app URL
if: contains(steps.get-envs.outputs.environmentVariables, 'BASE_URL') && steps.check-deploy.outputs.existingAppUrl
id: next-vars
Expand All @@ -156,7 +169,7 @@ jobs:
BUILDARGS=""

# Replace URL in ENVIRONMENT_VARIABLES
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ steps.get-envs.outputs.environmentVariables }}"
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ steps.remove-prime-keys.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}"
for VAR in "${ENVVARS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
Expand All @@ -165,7 +178,7 @@ jobs:
done

# Replace URL in BUILDARGS
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ steps.get-envs.outputs.buildArguments }}"
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ steps.remove-prime-keys.outputs.buildArguments || steps.get-envs.outputs.buildArguments }}"
for VAR in "${BUILDARGS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
Expand All @@ -177,13 +190,6 @@ jobs:
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT
echo "alreadyDeployed=true" >> $GITHUB_OUTPUT

- name: Remove keys from environment variables
id: remove-prime-keys
if: contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PUBLIC_KEY') || contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PRIVATE_KEY')
run: |
ENVIRONMENT_VARIABLES=$(echo "${{ steps.get-envs.outputs.environmentVariables }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT

- name: Login to Azure Container Registry
uses: Azure/docker-login@v1
with:
Expand All @@ -193,7 +199,7 @@ jobs:

- name: Build & Push Docker Image
run: |
docker buildx build ${{ steps.remove-prime-keys.outputs.buildArguments || steps.next-vars.outputs.buildArguments || steps.get-envs.outputs.buildArguments }} -t "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}:${{ needs.prepare.outputs.jiraTicketId }}" ${{ inputs.dockerFilePath }} --load
docker buildx build ${{ steps.next-vars.outputs.buildArguments || steps.get-envs.outputs.buildArguments }} -t "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}:${{ needs.prepare.outputs.jiraTicketId }}" ${{ inputs.dockerFilePath }} --load
docker push -a "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}"

- name: Deploy Azure Container App
Expand All @@ -208,7 +214,7 @@ jobs:
resourceGroup: ${{ inputs.clusterResourceGroup }}
targetPort: ${{ steps.target-port.outputs.targetPort }}
location: ${{ inputs.azureResourceLocation }}
environmentVariables: ${{ steps.remove-prime-keys.outputs.environmentVariables || steps.next-vars.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}
environmentVariables: ${{ steps.next-vars.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}
ingress: external
disableTelemetry: true

Expand All @@ -221,12 +227,6 @@ jobs:
run: |
HOSTNAME=$(az containerapp list --query "[?name == '${{ needs.prepare.outputs.containerAppName }}'].properties.configuration.ingress.fqdn" -o tsv)
echo "hostname=https://${HOSTNAME}" >> $GITHUB_OUTPUT

- name: Check if app is a Next.js application
if: contains(steps.get-envs.outputs.environmentVariables, 'BASE_URL')
id: check-next
run: |
echo "next-app=true" >> $GITHUB_OUTPUT
outputs:
hostname: ${{ steps.hostname.outputs.hostname }}
next-app: ${{ steps.check-next.outputs.next-app }}
Expand All @@ -251,6 +251,15 @@ jobs:
environmentKeyVault: ${{ inputs.environmentKeyVaultPrefix }}-${{ needs.prepare.outputs.environment }}
contentTypes: BuildArg Env

- name: Remove keys from variables
id: remove-prime-keys
if: contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PUBLIC_KEY') || contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PRIVATE_KEY')
run: |
ENVIRONMENT_VARIABLES=$(echo "${{ steps.get-envs.outputs.environmentVariables }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
BUILDARGS=$(echo "${{ steps.get-envs.outputs.buildArguments }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT

- name: Update Next URL variables
id: next-vars
run: |
Expand All @@ -260,7 +269,7 @@ jobs:
BUILDARGS=""

# Replace URL in ENVIRONMENT_VARIABLES
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ steps.get-envs.outputs.environmentVariables }}"
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ steps.remove-prime-keys.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}"
for VAR in "${ENVVARS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
Expand All @@ -269,7 +278,7 @@ jobs:
done

# Replace URL in BUILDARGS
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ steps.get-envs.outputs.buildArguments }}"
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ steps.remove-prime-keys.outputs.buildArguments || steps.get-envs.outputs.buildArguments }}"
for VAR in "${BUILDARGS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
Expand All @@ -280,15 +289,6 @@ jobs:
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT

- name: Remove keys from variables
id: remove-prime-keys
if: contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PUBLIC_KEY') || contains(steps.get-envs.outputs.environmentVariables, 'PRIME_PRIVATE_KEY')
run: |
ENVIRONMENT_VARIABLES=$(echo "${{ steps.next-vars.outputs.environmentVariables }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
BUILDARGS=$(echo "${{ steps.next-vars.outputs.buildArguments }}" | sed -E 's/(PRIME_PRIVATE_KEY|PRIME_PUBLIC_KEY)=[^ ]* //g')
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT

- name: Login to Azure Container Registry
uses: Azure/docker-login@v1
with:
Expand All @@ -298,7 +298,7 @@ jobs:

- name: Build & Push Docker Image with updated Next.js variables
run: |
docker buildx build --no-cache ${{ steps.remove-prime-keys.outputs.buildArguments || steps.next-vars.outputs.buildArguments }} -t "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}:${{ needs.prepare.outputs.jiraTicketId }}" ${{ inputs.dockerFilePath }} --load
docker buildx build --no-cache ${{ steps.next-vars.outputs.buildArguments }} -t "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}:${{ needs.prepare.outputs.jiraTicketId }}" ${{ inputs.dockerFilePath }} --load
docker push -a "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}"

- name: Login via Az module
Expand All @@ -318,7 +318,7 @@ jobs:
resourceGroup: ${{ inputs.clusterResourceGroup }}
targetPort: ${{ needs.deploy.outputs.targetPort }}
location: ${{ inputs.azureResourceLocation }}
environmentVariables: ${{ steps.remove-prime-keys.outputs.environmentVariables || steps.next-vars.outputs.environmentVariables }}
environmentVariables: ${{ steps.next-vars.outputs.environmentVariables }}
ingress: external
disableTelemetry: true

Expand Down
Loading