Skip to content

Commit

Permalink
[DEVOPS-350] Only update Next variables once if app has already been …
Browse files Browse the repository at this point in the history
…deployed
  • Loading branch information
ebronson68 committed Dec 15, 2023
1 parent 4090a6f commit aaeec67
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions .github/workflows/ephemeral-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,45 @@ jobs:
TARGET_PORT=$(find . -iname "values.yaml" -exec grep "targetPort: " {} \; | awk -F ': ' '{print $2}' | uniq)
echo "targetPort=${TARGET_PORT}" >> $GITHUB_OUTPUT
- name: Check if Next.js app has already been deployed
if: contains(steps.get-envs.outputs.environmentVariables, 'BASE_URL')
id: check-deploy
run: |
HOSTNAME=$(az containerapp list --query "[?name == '${{ needs.prepare.outputs.containerAppName }}'].properties.configuration.ingress.fqdn" -o tsv)
if [[ -n "${HOSTNAME}" ]]; then
echo "existingAppUrl=https://${HOSTNAME}" >> $GITHUB_OUTPUT
fi
- 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
run: |
BASE_URL="${{ env.BASE_URL || env.NEXT_PUBLIC_BASE_URL || env.NEXTAUTH_URL }}"
HOSTNAME="${{ steps.already-deployed.outputs.existingAppUrl }}"
ENVIRONMENT_VARIABLES=""
BUILDARGS=""
# Replace URL in ENVIRONMENT_VARIABLES
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ 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")
fi
ENVIRONMENT_VARIABLES+="${VAR} "
done
# Replace URL in BUILDARGS
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ 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")
fi
BUILDARGS="${BUILDARGS} --build-arg ${VAR}"
done
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $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')
Expand All @@ -151,11 +190,6 @@ jobs:
username: ${{ secrets.registryUserName }}
password: ${{ secrets.registryPassword }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
install: true

- name: Build & Push Docker Image
run: |
docker buildx build ${{ steps.get-envs.outputs.buildArguments }} -t "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}:${{ needs.prepare.outputs.jiraTicketId }}" ${{ inputs.dockerFilePath }} --load
Expand Down Expand Up @@ -218,15 +252,15 @@ jobs:
- name: Update Next URL variables
id: next-vars
run: |
REPOSITORY_NAME=$(echo "${{ github.event.repository.name }}" | awk -F '_' '{print $1}' | tr -d "-")
BASE_URL="${{ env.BASE_URL || env.NEXT_PUBLIC_BASE_URL || env.NEXTAUTH_URL }}"
HOSTNAME="${{ needs.deploy.outputs.hostname }}"
ENVIRONMENT_VARIABLES=""
BUILDARGS=""
# Replace URL in ENVIRONMENT_VARIABLES
IFS=' ' read -r -a ENVVARS_ARRAY <<< "${{ steps.get-envs.outputs.environmentVariables }}"
for VAR in "${ENVVARS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${REPOSITORY_NAME}.com";then
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
fi
ENVIRONMENT_VARIABLES+="${VAR} "
Expand All @@ -235,7 +269,7 @@ jobs:
# Replace URL in BUILDARGS
IFS=' ' read -r -a BUILDARGS_ARRAY <<< "${{ steps.get-envs.outputs.buildArguments }}"
for VAR in "${BUILDARGS_ARRAY[@]}"; do
if echo "${VAR}" | grep -Eq "localhost|${REPOSITORY_NAME}.com";then
if echo "${VAR}" | grep -Eq "localhost|${BASE_URL}";then
VAR=$(echo "${VAR}" | awk -F '=' '{print $1}' | sed "s|$|=${HOSTNAME}|g")
fi
BUILDARGS="${BUILDARGS} --build-arg ${VAR}"
Expand All @@ -244,13 +278,6 @@ jobs:
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT
- name: Login to Azure Container Registry
uses: Azure/docker-login@v1
with:
login-server: ${{ secrets.registryHostName }}
username: ${{ secrets.registryUserName }}
password: ${{ secrets.registryPassword }}

- 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')
Expand All @@ -260,10 +287,12 @@ jobs:
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
echo "buildArguments=${BUILDARGS}" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Azure Container Registry
uses: Azure/docker-login@v1
with:
install: true
login-server: ${{ secrets.registryHostName }}
username: ${{ secrets.registryUserName }}
password: ${{ secrets.registryPassword }}

- name: Build & Push Docker Image with updated Next.js variables
run: |
Expand Down

0 comments on commit aaeec67

Please sign in to comment.