From 8b855870a0786e8d1a7c1bb5080e386c3316d933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gosta=C5=84ski?= <67002661+grixu@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:11:16 +0200 Subject: [PATCH] feat: custom docker buildargs (#28) * feat: add custom envs in build-frontend action * feat: add custom envs for middleware * refactor: use one resolve step * docs: update README.md * fix: README.md * fix: bugs * fix: bug * ci: fix reading build-args * fix: string encoding --- README.md | 18 ++++++--- build-frontend/action.yml | 80 +++++++++++++++++++++++++------------ build-middleware/action.yml | 52 ++++++++++++++++++------ 3 files changed, 108 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index ab4e52d..2910f87 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ With those four you can easily compose the deployment script for your needs. ### build-frontend ```yaml -uses: vuestorefront/storefront-deployment/build-frontend@v4.4.0 +uses: vuestorefront/storefront-deployment/build-frontend@v4.5.0 with: project_name: ${{ secrets.PROJECT_NAME }} cloud_username: ${{ secrets.CLOUD_USERNAME }} @@ -28,6 +28,9 @@ with: npm_registry: 'https://registrynpm.storefrontcloud.io' version: '2.3.0' image_provider: 'ipx' + docker_buildargs: | + EXTRA_ENV_FOR_DOCKERFILE=somevalue-hardcoded + ANOTHER_EXTRA_ENV=${{ secrets.SOME_VALUE }} ``` **Input Parameters:** @@ -43,13 +46,14 @@ with: - `npm_registry`: URL to the private NPM registry. Optional field. Defaults to `https://registrynpm.storefrontcloud.io`. - `version`: Version that will be used for docker image tag. Example: 2.3.0, 3.1.0-rc.1. If not passed, github.sha will be used - `image_provider`: Select image provider for Nuxt Image. Optional field. Only for Nuxt. +- `docker_buildargs`: Multi-line input field, for passing in the additional build args. Optional field. Any environment variables needed by an application is needed to be set in the Alokai Console. ### build-middleware ```yaml -uses: vuestorefront/storefront-deployment/build-middleware@v4.4.0 +uses: vuestorefront/storefront-deployment/build-middleware@v4.5.0 with: project_name: ${{ secrets.PROJECT_NAME }} cloud_username: ${{ secrets.CLOUD_USERNAME }} @@ -60,6 +64,9 @@ with: docker_registry_url: 'registry.vuestorefront.cloud' npm_registry: 'https://registrynpm.storefrontcloud.io' version: 2.3.0 + docker_buildargs: | + EXTRA_ENV_FOR_DOCKERFILE=somevalue-hardcoded + ANOTHER_EXTRA_ENV=${{ secrets.SOME_VALUE }} ``` **Input Parameters:** @@ -73,11 +80,12 @@ with: - `docker_registry_url`: URL to the Docker image registry. Optional field. Defaults to `registry.vuestorefront.cloud`. - `npm_registry`: URL to the private NPM registry. Optional field. Defaults to `https://registrynpm.storefrontcloud.io`. - `version`: Version that will be used for docker image tag. Example: 2.3.0, 3.1.0-rc.1. If not passed, github.sha will be used +- `docker_buildargs`: Multi-line input field, for passing in the additional build args. Optional field. ### build-stripe-ct ```yaml -uses: vuestorefront/storefront-deployment/build-stripe-ct@v4.4.0 +uses: vuestorefront/storefront-deployment/build-stripe-ct@v4.5.0 with: project_name: ${{ secrets.PROJECT_NAME }} cloud_username: ${{ secrets.CLOUD_USERNAME }} @@ -107,7 +115,7 @@ with: ### deploy ```yaml -uses: vuestorefront/storefront-deployment/deploy@v4.4.0 +uses: vuestorefront/storefront-deployment/deploy@v4.5.0 with: project_name: ${{ secrets.PROJECT_NAME }} cloud_username: ${{ secrets.CLOUD_USERNAME }} @@ -133,7 +141,7 @@ with: ### deploy/stripe-ct ```yaml -uses: vuestorefront/storefront-deployment/deploy/stripe-ct@v4.4.0 +uses: vuestorefront/storefront-deployment/deploy/stripe-ct@v4.5.0 with: project_name: ${{ secrets.PROJECT_NAME }} cloud_username: ${{ secrets.CLOUD_USERNAME }} diff --git a/build-frontend/action.yml b/build-frontend/action.yml index 70cd781..f4c44e3 100644 --- a/build-frontend/action.yml +++ b/build-frontend/action.yml @@ -38,6 +38,9 @@ inputs: description: 'Image provider (only for Nuxt)' required: false default: 'ipx' + docker_buildargs: + description: 'List of additional ENVs that should be passed at Docker image build-time' + required: false runs: using: 'composite' steps: @@ -52,27 +55,59 @@ runs: username: ${{ inputs.cloud_username }} password: ${{ inputs.cloud_password }} - - name: Resolve values of the versions, docker ref & dockerfile path + - name: Resolve docker buildargs id: resolve - shell: bash - run: | - # Set the version of the app based on user input or use git commit hash - echo "version=${{ inputs.version || github.sha }}" >> $GITHUB_OUTPUT - # Set the reference address for app in docker registry - echo "ref=${{ inputs.docker_registry_url }}/${{ inputs.project_name }}-storefrontcloud-io" >> $GITHUB_OUTPUT - # Set the path to the Dockerfile base on selected frontend framework - if [[ $FRONTEND == "next" ]]; then - echo "path=.vuestorefrontcloud/docker/nextjs/Dockerfile-frontend" >> $GITHUB_OUTPUT - elif [[ $FRONTEND == "nextjs" ]]; then - echo "path=.vuestorefrontcloud/docker/nextjs/Dockerfile-frontend" >> $GITHUB_OUTPUT - elif [[ $FRONTEND == "nuxt" ]]; then - echo "path=.vuestorefrontcloud/docker/nuxtjs/Dockerfile-frontend" >> $GITHUB_OUTPUT - else - echo "Error: Unsupported FRONTEND value. Valid options are 'next' or 'nuxt'." - exit 1 - fi + uses: actions/github-script@v7 env: - FRONTEND: ${{ inputs.frontend }} + INPUT_NPM_EMAIL: ${{ inputs.npm_email }} + INPUT_NPM_PASS: ${{ inputs.npm_pass }} + INPUT_NPM_USER: ${{ inputs.npm_user }} + INPUT_NPM_REGISTRY: ${{ inputs.npm_registry }} + INPUT_IMAGE_PROVIDER: ${{ inputs.image_provider }} + INPUT_DOCKER_BUILDARGS: ${{ inputs.docker_buildargs }} + INPUT_FRONTEND: ${{ inputs.frontend }} + INPUT_VERSION: ${{ inputs.version }} + INPUT_DOCKER_REGISTRY_URL: ${{ inputs.docker_registry_url }} + INPUT_PROJECT_NAME: ${{ inputs.project_name }} + with: + result-encoding: string + script: | + const base = { + 'NPM_EMAIL': core.getInput('npm_email'), + 'NPM_PASS': core.getInput('npm_pass'), + 'NPM_USER': core.getInput('npm_user'), + 'NPM_REGISTRY': core.getInput('npm_registry'), + 'NUXT_IMAGE_PROVIDER': core.getInput('image_provider'), + }; + + const transformedBaseArgs = Object.entries(base) + .map(([key, value]) => `${key}=${value}`); + + const extraArgs = core.getMultilineInput('docker_buildargs', { required: false }); + const mergedArgs = [ + ...transformedBaseArgs, + ...extraArgs, + ]; + + core.setOutput('docker_args', mergedArgs.join('\n')); + + const versionInput = core.getInput('version'); + core.setOutput('version', versionInput || context.sha); + + core.setOutput('ref', `${core.getInput('docker_registry_url')}/${core.getInput('project_name')}-storefrontcloud-io`); + + const frontendInput = core.getInput('frontend'); + + switch (frontendInput) { + case "next": + core.setOutput('path', '.vuestorefrontcloud/docker/nextjs/Dockerfile-frontend'); + break; + case "nuxt": + core.setOutput('path', '.vuestorefrontcloud/docker/nuxtjs/Dockerfile-frontend'); + break; + default: + core.setOutput('path', '.vuestorefrontcloud/docker/nextjs/Dockerfile-frontend') + } - name: Check for existing image id: check-existing-image @@ -92,9 +127,4 @@ runs: tags: ${{ steps.resolve.outputs.ref }}/vue-storefront:${{ steps.resolve.outputs.version }} cache-from: "type=registry,ref=${{ steps.resolve.outputs.ref }}/vue-storefront:buildcache" cache-to: "type=registry,ref=${{ steps.resolve.outputs.ref }}/vue-storefront:buildcache,mode=max" - build-args: | - NPM_EMAIL=${{ inputs.npm_email }} - NPM_PASS=${{ inputs.npm_pass }} - NPM_USER=${{ inputs.npm_user }} - NPM_REGISTRY=${{ inputs.npm_registry }} - NUXT_IMAGE_PROVIDER=${{ inputs.image_provider }} + build-args: ${{ steps.resolve.outputs.docker_args }} diff --git a/build-middleware/action.yml b/build-middleware/action.yml index a5c258f..87d1b32 100644 --- a/build-middleware/action.yml +++ b/build-middleware/action.yml @@ -30,6 +30,9 @@ inputs: version: description: 'Version of the app' required: false + docker_buildargs: + description: 'List of additional ENVs that should be passed at Docker image build-time' + required: false runs: using: "composite" steps: @@ -44,14 +47,43 @@ runs: username: ${{ inputs.cloud_username }} password: ${{ inputs.cloud_password }} - - name: Resolve values of the versions, docker ref & dockerfile path + - name: Resolve docker buildargs id: resolve - shell: bash - run: | - # Set the version of the app based on user input or use git commit hash - echo "version=${{ inputs.version || github.sha }}" >> $GITHUB_OUTPUT - # Set the reference address for app in docker registry - echo "ref=${{ inputs.docker_registry_url }}/${{ inputs.project_name }}-storefrontcloud-io" >> $GITHUB_OUTPUT + uses: actions/github-script@v7 + env: + INPUT_NPM_EMAIL: ${{ inputs.npm_email }} + INPUT_NPM_PASS: ${{ inputs.npm_pass }} + INPUT_NPM_USER: ${{ inputs.npm_user }} + INPUT_NPM_REGISTRY: ${{ inputs.npm_registry }} + INPUT_DOCKER_BUILDARGS: ${{ inputs.docker_buildargs }} + INPUT_VERSION: ${{ inputs.version }} + INPUT_DOCKER_REGISTRY_URL: ${{ inputs.docker_registry_url }} + INPUT_PROJECT_NAME: ${{ inputs.project_name }} + with: + result-encoding: string + script: | + const base = { + 'NPM_EMAIL': core.getInput('npm_email'), + 'NPM_PASS': core.getInput('npm_pass'), + 'NPM_USER': core.getInput('npm_user'), + 'NPM_REGISTRY': core.getInput('npm_registry'), + }; + + const transformedBaseArgs = Object.entries(base) + .map(([key, value]) => `${key}=${value}`); + + const extraBuildArgs = core.getMultilineInput('docker_buildargs', { required: false }); + const mergedBuildArgs = [ + ...transformedBaseArgs, + ...extraBuildArgs, + ]; + + core.setOutput('docker_args', mergedBuildArgs.join('\n')); + + const versionInput = core.getInput('version'); + core.setOutput('version', versionInput || context.sha); + + core.setOutput('ref', `${core.getInput('docker_registry_url')}/${core.getInput('project_name')}-storefrontcloud-io`); - name: Check for existing image id: check-existing-image @@ -71,8 +103,4 @@ runs: tags: ${{ steps.resolve.outputs.ref }}/vue-storefront-middleware:${{ steps.resolve.outputs.version }} cache-from: "type=registry,ref=${{ steps.resolve.outputs.ref }}/vue-storefront-middleware:buildcache" cache-to: "type=registry,ref=${{ steps.resolve.outputs.ref }}/vue-storefront-middleware:buildcache,mode=max" - build-args: | - NPM_EMAIL=${{ inputs.npm_email }} - NPM_PASS=${{ inputs.npm_pass }} - NPM_USER=${{ inputs.npm_user }} - NPM_REGISTRY=${{ inputs.npm_registry }} + build-args: ${{ steps.resolve.outputs.docker_args }}