From aaeec67a6d5bd8d8a1e24456377442e1cf1625a8 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Fri, 15 Dec 2023 16:15:16 -0600 Subject: [PATCH] [DEVOPS-350] Only update Next variables once if app has already been deployed --- .github/workflows/ephemeral-deploy.yaml | 65 ++++++++++++++++++------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ephemeral-deploy.yaml b/.github/workflows/ephemeral-deploy.yaml index 3bedbb1e..cd5012e6 100644 --- a/.github/workflows/ephemeral-deploy.yaml +++ b/.github/workflows/ephemeral-deploy.yaml @@ -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') @@ -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 @@ -218,7 +252,7 @@ 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="" @@ -226,7 +260,7 @@ jobs: # 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} " @@ -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}" @@ -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') @@ -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: |