Skip to content

Commit

Permalink
[DEVOPS-350] Skip updating Next.js variables if app has already been …
Browse files Browse the repository at this point in the history
…deployed (#101)

<details open>
<summary><a href="https://amuniversal.atlassian.net/browse/DEVOPS-350"
title="DEVOPS-350" target="_blank">DEVOPS-350</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
<td>Don't run Next.js ephemeral deploy job if app already exists</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Story"
src="https://amuniversal.atlassian.net/images/icons/issuetypes/story.png"
/>
        Story
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Progress</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
      <td>-</td>
    </tr>
  </table>
</details>
<!--
do not remove this marker as it will break action-jira-linter's
functionality.
  added_by_jira_lint
-->
---

<!-- Please make sure you read the contribution guidelines and then fill
out the blanks below.

Please format the PR title appropriately based on the type of change:
  [JIRA-XXX]: <description>
-->

## Description
![Screenshot 2023-12-15 at 5 04
22 PM](https://github.com/Andrews-McMeel-Universal/reusable_workflows/assets/111298136/64da13d2-7140-4c19-9edc-847dacbaa46f)


- Skip updating Next.js variables if app has already been deployed
- Replace `BASE_URL` variables with the correct BASE_URL values rather
than a modified version of the repository name.

## Related Links

<!-- List any links related to this pull request here

Replace "JIRA-XXX" with the your Jira issue key -->

- Jira Issue: DEVOPS-350
- [![📦️ Deploy Ephemeral
Environment](https://github.com/Andrews-McMeel-Universal/puzzle-society_ui/actions/workflows/ephemeral-deployments.yml/badge.svg?branch=story%2FDEVOPS-350%2Fonly-run-next-js-var-updates-once)](https://github.com/Andrews-McMeel-Universal/puzzle-society_ui/actions/workflows/ephemeral-deployments.yml)
  • Loading branch information
ebronson68 committed Dec 18, 2023
1 parent 4090a6f commit f0adafa
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions .github/workflows/ephemeral-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ 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.check-deploy.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} ${VAR}"
done
echo "environmentVariables=${ENVIRONMENT_VARIABLES}" >> $GITHUB_OUTPUT
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')
Expand All @@ -151,14 +191,9 @@ 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
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 push -a "${{ secrets.registryHostName }}/${{ inputs.dockerImageName }}"
- name: Deploy Azure Container App
Expand All @@ -173,7 +208,7 @@ jobs:
resourceGroup: ${{ inputs.clusterResourceGroup }}
targetPort: ${{ steps.target-port.outputs.targetPort }}
location: ${{ inputs.azureResourceLocation }}
environmentVariables: ${{ steps.remove-prime-keys.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}
environmentVariables: ${{ steps.remove-prime-keys.outputs.environmentVariables || steps.next-vars.outputs.environmentVariables || steps.get-envs.outputs.environmentVariables }}
ingress: external
disableTelemetry: true

Expand All @@ -196,10 +231,11 @@ jobs:
hostname: ${{ steps.hostname.outputs.hostname }}
next-app: ${{ steps.check-next.outputs.next-app }}
targetPort: ${{ steps.target-port.outputs.targetPort }}
alreadyDeployed: ${{ steps.next-vars.outputs.alreadyDeployed }}

update-next-deploy:
name: Update Next.js Container App
if: ${{ needs.deploy.outputs.next-app == 'true' }}
if: ${{ needs.deploy.outputs.next-app == 'true' && needs.deploy.outputs.alreadyDeployed != 'true' }}
needs: [prepare,deploy]
runs-on: ubuntu-latest
steps:
Expand All @@ -218,15 +254,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 +271,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 +280,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 +289,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 f0adafa

Please sign in to comment.