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

Feature: add build caching #14

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
102 changes: 58 additions & 44 deletions build-frontend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,83 @@ name: Build Frontend
description: Builds Storefront app docker image
inputs:
frontend:
description: 'Switch between Next.js and Nuxt'
description: "Switch between Next.js and Nuxt"
required: false
default: 'next'
default: "next"
project_name:
description: 'Project name'
description: "Project name"
required: true
docker_registry_url:
description: 'Docker registry url'
description: "Docker registry url"
required: false
default: 'registry.vuestorefront.cloud'
default: "registry.storefrontcloud.io"
docker_registry_ref:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we by default compute the value? It should be like:
${{ inputs.project_name }}-storefrontcloud-io

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it won't build if i use this change unfortunately. i can't seem to reference inputs within the inputs section of this file. it has to be hard coded.

However within the codebase the continuous-delivery.yml file looks like this

docker_registry_ref: ${{ env.DOCKER_REGISTRY_URL }}/${{ env.CUSTOMER_NAME }}-storefrontcloud-io

description: "Docker registry reference"
required: true
cloud_username:
description: 'Cloud username'
description: "Cloud username"
required: true
cloud_password:
description: 'Cloud password'
description: "Cloud password"
required: true
cloud_region:
description: 'Cloud region'
description: "Cloud region"
required: true
npm_email:
description: 'NPM email'
description: "NPM email"
required: true
npm_pass:
description: 'NPM password'
description: "NPM password"
required: true
npm_user:
description: 'NPM user'
description: "NPM user"
required: true
npm_registry:
description: 'NPM registry'
description: "NPM registry"
required: false
default: 'https://registrynpm.storefrontcloud.io'
default: "https://registrynpm.storefrontcloud.io"
api_base_url:
description: 'Base Middleware API URL for Storefront. By default https://project_name.region.gcp.storefrontcloud.io/api'
description: "Base Middleware API URL for Storefront. By default https://project_name.region.gcp.storefrontcloud.io/api"
required: false
multistore_enabled:
description: 'Switch for multistore'
description: "Switch for multistore"
required: false
default: 'false'
default: "false"
image_provider:
description: 'Image provider name'
description: "Image provider name"
required: false
image_provider_upload_url:
description: 'Image Provider upload url'
description: "Image Provider upload url"
required: false
image_provider_fetch_url:
description: 'Image Provider fetch url'
description: "Image Provider fetch url"
required: false
coveo_organization_id:
description: 'Coveo organization id'
description: "Coveo organization id"
required: false
coveo_access_token:
description: 'Coveo access token'
description: "Coveo access token"
required: false
version:
description: 'Version of the app'
description: "Version of the app"
required: false

runs:
using: 'composite'
using: "composite"
steps:
- name: Setup node
uses: actions/setup-node@v3
# Need buildx for caching
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3

- name: Login Alokai Docker Registry
uses: docker/login-action@v3
with:
node-version: 18.x
- name: Build and publish docker image (Next.js frontend)
registry: ${{ inputs.docker_registry_url }}
username: ${{ inputs.npm_user }}
password: ${{ inputs.npm_pass }}

- name: Build and push image (Next.js frontend)
if: ${{ inputs.frontend == 'next' || inputs.frontend == 'nextjs' }}
uses: elgohr/Publish-Docker-Github-Action@v5
with:
Expand All @@ -89,26 +101,28 @@ runs:
NEXT_PUBLIC_IMAGE_LOADER_FETCH_URL: ${{ inputs.image_provider_fetch_url }}
NEXT_PUBLIC_COVEO_ORGANIZATION_ID: ${{ inputs.coveo_organization_id }}
NEXT_PUBLIC_COVEO_ACCESS_TOKEN: ${{ inputs.coveo_access_token }}
- name: Build and publish docker image (Nuxt frontend)

- name: Build and push
if: ${{ inputs.frontend == 'nuxt' }}
uses: elgohr/Publish-Docker-Github-Action@v5
uses: docker/build-push-action@v5
with:
name: ${{ inputs.project_name }}-storefrontcloud-io/vue-storefront:${{ inputs.version || github.sha }}
registry: ${{ inputs.docker_registry_url }}
username: ${{ inputs.cloud_username }}
password: ${{ inputs.cloud_password }}
dockerfile: .vuestorefrontcloud/docker/nuxtjs/Dockerfile-frontend
buildoptions: --compress
buildargs: NPM_EMAIL,NPM_PASS,NPM_USER,NPM_REGISTRY,NUXT_PUBLIC_API_BASE_URL,NUXT_PUBLIC_MULTISTORE_ENABLED,NUXT_IMAGE_PROVIDER,NUXT_PUBLIC_IMAGE_LOADER_FETCH_URL,NUXT_PUBLIC_IMAGE_LOADER_UPLOAD_URL,NUXT_PUBLIC_COVEO_ORGANIZATION_ID,NUXT_PUBLIC_COVEO_ACCESS_TOKEN
context: .
file: .vuestorefrontcloud/docker/nuxtjs/Dockerfile-frontend
push: true
tags: ${{ inputs.docker_registry_ref }}/vue-storefront:${{ github.sha }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need a space for custom versioning: ${{ inputs.version || github.sha }}

# Inline cache
cache-from: "type=registry,ref=${{ inputs.docker_registry_ref }}/vue-storefront:${{ inputs.project_name }}-buildcache"
cache-to: "type=registry,ref=${{ inputs.docker_registry_ref }}/vue-storefront:${{ inputs.project_name }}-buildcache,mode=max"
build-args: |
NPM_EMAIL=${{ inputs.npm_email }}
NPM_PASS=${{ inputs.npm_pass }}
NPM_USER=${{ inputs.npm_user }}
NPM_REGISTRY=https://registrynpm.storefrontcloud.io
grixu marked this conversation as resolved.
Show resolved Hide resolved
NUXT_PUBLIC_API_BASE_URL: https://${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io/api/
Copy link
Collaborator

@grixu grixu Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're passing the build args this way, I guess that we don't have to declare the envs key below.

env:
NPM_EMAIL: ${{ inputs.npm_email }}
NPM_PASS: ${{ inputs.npm_pass }}
NPM_USER: ${{ inputs.npm_user }}
NPM_REGISTRY: ${{ inputs.npm_registry }}
NUXT_PUBLIC_API_BASE_URL: ${{ inputs.api_base_url || format('https://{0}.{1}.{2}/api', inputs.project_name, inputs.cloud_region, 'gcp.storefrontcloud.io') }}
NUXT_PUBLIC_MULTISTORE_ENABLED: ${{ inputs.multistore_enabled }}
NUXT_IMAGE_PROVIDER: ${{ inputs.image_provider }}
NUXT_PUBLIC_IMAGE_LOADER_UPLOAD_URL: ${{ inputs.image_provider_upload_url }}
NUXT_PUBLIC_IMAGE_LOADER_FETCH_URL: ${{ inputs.image_provider_fetch_url }}
NUXT_PUBLIC_COVEO_ORGANIZATION_ID: ${{ inputs.coveo_organization_id }}
NUXT_PUBLIC_COVEO_ACCESS_TOKEN: ${{ inputs.coveo_access_token }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we release the upgrade to the Next 14 & App Router, those env variables must stay for both of the frontends - we would rather not introduce the differences between them in the deployment

NPM_REGISTRY: https://registrynpm.storefrontcloud.io
NUXT_PUBLIC_API_BASE_URL: https://${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io/api/

49 changes: 40 additions & 9 deletions build-middleware/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ inputs:
docker_registry_url:
description: 'Docker registry url'
required: false
default: 'registry.vuestorefront.cloud'
default: "registry.storefrontcloud.io"
docker_registry_ref:
description: "Docker registry reference"
required: true
cloud_username:
description: 'Cloud username'
required: true
Expand All @@ -33,23 +36,29 @@ inputs:
runs:
using: "composite"
steps:
# Need buildx for caching
- name: Set up Docker Buildx
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to define buildx before caching happens

id: buildx
uses: docker/setup-buildx-action@v3

- name: Login Alokai Docker Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.docker_registry_url }}
username: ${{ inputs.npm_user }}
password: ${{ inputs.npm_pass }}

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Encode Basic Auth
id: base64-auth
shell: bash
run: |
echo "BASIC_AUTH_TOKEN=$(echo -n ${{ inputs.cloud_username }}:${{ inputs.cloud_password }} | base64 -w 0)" >> "$GITHUB_OUTPUT"
- name: Check for existing image
id: check-existing-image
uses: javajawa/check-registry-for-image@v2
with:
registry: ${{ inputs.docker_registry_url }}
auth: "Basic ${{ steps.base64-auth.outputs.BASIC_AUTH_TOKEN }}"
repository: ${{ inputs.project_name }}-storefrontcloud-io/vue-storefront-middleware
tag: ${{ inputs.version || github.sha }}

- name: Build and publish docker image (middleware)
uses: elgohr/Publish-Docker-Github-Action@v5
if: ${{ steps.check-existing-image.outputs.exists == 'false' }}
Expand All @@ -66,3 +75,25 @@ runs:
NPM_PASS: ${{ inputs.npm_pass }}
NPM_USER: ${{ inputs.npm_user }}
NPM_REGISTRY: ${{ inputs.npm_registry }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: .vuestorefrontcloud/docker/middleware/Dockerfile-middleware
push: true
tags: ${{ inputs.docker_registry_ref }}/vue-storefront-middleware:${{ github.sha }}
# Inline cache
cache-from: "type=registry,ref=${{ inputs.docker_registry_ref }}/vue-storefront-middleware:${{ inputs.project_name }}-buildcache"
cache-to: "type=registry,ref=${{ inputs.docker_registry_ref }}/vue-storefront-middleware:${{ inputs.project_name }}-buildcache,mode=max"
build-args: |
NPM_EMAIL=${{ inputs.npm_email }}
NPM_PASS=${{ inputs.npm_pass }}
NPM_USER=${{ inputs.npm_user }}
NPM_REGISTRY=https://registrynpm.storefrontcloud.io
grixu marked this conversation as resolved.
Show resolved Hide resolved
env:
NPM_EMAIL: ${{ inputs.npm_email }}
NPM_PASS: ${{ inputs.npm_pass }}
NPM_USER: ${{ inputs.npm_user }}
NPM_REGISTRY: https://registrynpm.storefrontcloud.io

85 changes: 51 additions & 34 deletions deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,92 @@ name: Deploy Storefront
description: Builds Middleware app docker image
inputs:
project_name:
description: 'Project name'
description: "Project name"
required: true
docker_registry_url:
description: 'Docker registry url'
description: "Docker registry url"
required: false
default: 'registry.vuestorefront.cloud'
default: "registry.vuestorefront.cloud"
cloud_username:
description: 'Cloud username'
description: "Cloud username"
required: true
cloud_password:
description: 'Cloud password'
description: "Cloud password"
required: true
cloud_region:
description: 'Cloud region'
description: "Cloud region"
required: true
console_api_url:
description: 'URI for Vue Storefront Console API'
description: "URI for Vue Storefront Console API"
required: false
default: 'https://api.platform.vuestorefront.io'
default: "https://api.platform.vuestorefront.io"
version:
description: 'Version of the app'
description: "Version of the app"
required: false

runs:
using: "composite"
steps:
- uses: chrnorm/deployment-action@releases/v2
- uses: chrnorm/deployment-action@v2
name: Create GitHub deployment
id: deployment
with:
token: '${{ github.token }}'
token: "${{ github.token }}"
environment-url: https://${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io
environment: production
initial-status: in_progress

- name: Deploy Frontend App (${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io)
uses: fjogeleit/http-request-action@v1
with:
url: "${{ inputs.console_api_url }}/cloud/instances/${{ inputs.project_name }}-${{ inputs.cloud_region }}-gcp-storefrontcloud-io/deploy"
method: "PATCH"
customHeaders: '{"Content-Type":"application/json"}'
data: '{ "cloudUserId":"${{ inputs.cloud_username }}", "cloudUserPassword":"${{ inputs.cloud_password }}", "dockerImageHash":"${{ inputs.version || github.sha }}" }'
timeout: 60000

- name: Deploy Middleware (${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io)
uses: fjogeleit/http-request-action@v1
with:
url: "${{ inputs.console_api_url }}/cloud/instances/${{ inputs.project_name }}-${{ inputs.cloud_region }}-gcp-storefrontcloud-io/deploy"
method: "PATCH"
customHeaders: '{"Content-Type":"application/json"}'
data: '{ "middleware":true, "cloudUserId":"${{ inputs.cloud_username }}", "cloudUserPassword":"${{ inputs.cloud_password }}", "dockerImageHash":"${{ inputs.version || github.sha }}", "middlewareData":{ "path":"/api/", "port":4000, "has_base_path":false } }'
timeout: 60000
- name: Deploy on ${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed this because this code is what the SA's are familiar with. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployments must be triggered via the Console API. The firing it through the Farmer is a deprecated way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Triggering it via Farmer, erases the environment variables and there's lack of deployment markers in the Console β€” so part of their functionality is broken.

shell: bash
run: |
tmpfile=$(mktemp /tmp/farmer-output.XXXXXXX)
FARMERCODE=`curl --write-out '%{http_code}' --silent --output "$tmpfile" \
-H 'X-User-Id: ${{ inputs.cloud_username }}' \
-H 'X-Api-Key: ${{ inputs.cloud_password }}' \
-H 'Content-Type: application/json' \
-X PATCH -d '{
"front_version":"${{ github.sha }}",
"additional_apps":{
"enabled": true,
"apps":[{
"name":"middleware",
"tag":"${{ github.sha }}",
"image":"${{ inputs.docker_registry_url }}/${{ inputs.project_name }}-storefrontcloud-io/vue-storefront-middleware",
"path":"/api/",
"port":"4000",
"has_base_path":false,
"health_check": {
"enabled": true,
"path": "/healthz"
}
}]
}
}' https://farmer.storefrontcloud.io/instance/${{ inputs.project_name }}-${{ inputs.cloud_region }}-gcp-storefrontcloud-io`
cat "$tmpfile"
rm "$tmpfile"
if [[ $FARMERCODE<"400" && $FARMERCODE -ge "200" ]]
then
echo "farmer deploy: SUCCESS";
else
echo "farmer deploy FAILED ❌ with code $FARMERCODE";
exit 1;
fi

- name: Update deployment status (success)
if: success()
uses: chrnorm/deployment-status@releases/v2
with:
token: '${{ github.token }}'
token: "${{ github.token }}"
environment-url: https://${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io
state: 'success'
state: "success"
description: Congratulations! The deploy is done.
deployment-id: ${{ steps.deployment.outputs.deployment_id }}

- name: Update deployment status (failure)
if: failure()
uses: chrnorm/deployment-status@releases/v2
with:
token: '${{ github.token }}'
token: "${{ github.token }}"
environment-url: https://${{ inputs.project_name }}.${{ inputs.cloud_region }}.gcp.storefrontcloud.io
description: Unfortunately, the instance hasn't been updated.
state: 'failure'
state: "failure"
deployment-id: ${{ steps.deployment.outputs.deployment_id }}