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

[doc]: Deployment on eg. Google Cloud Run - using --build-arg for dockerfiles #3566

Open
1 of 2 tasks
rasmuswikman opened this issue Nov 19, 2021 · 4 comments
Open
1 of 2 tasks
Labels
documentation This pertains to documentation. Progress: ready for dev

Comments

@rasmuswikman
Copy link

Describe the request

Currently there is a Dockerfile called prod.dockerfile that uses the environment file docker/.env.docker.prod.

If I understand correctly, the only way to change these variables is by editing the docker/.env.docker.prod directly when deploying to production. Either manually or by variable substitution in a CI/CD pipeline.

Possible solutions

By using --build-args in docker build command, the variables can can be specified instead of only using redacted as now in the docker/.env.docker.prod file.

Example of using --build-args in a Dockerfile:

# specified as --build-arg
ARG MAGENTO_BACKEND_URL
ENV MAGENTO_BACKEND_URL=$MAGENTO_BACKEND_URL
ARG MAGENTO_BACKEND_EDITION
ENV MAGENTO_BACKEND_EDITION=$MAGENTO_BACKEND_EDITION

Example of using --build-args in a GitHub workflow:

- name: Build and Push Container
  run: |-
    docker build \
    --build-arg MAGENTO_BACKEND_URL=${{ secrets.MAGENTO_BACKEND_URL }} \
    --build-arg MAGENTO_BACKEND_EDITION=${{ secrets.MAGENTO_BACKEND_EDITION }} \
    -t gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_SERVICE }}:${{ github.sha }} \
    -f gcr.dockerfile .
    docker push gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_SERVICE }}:${{ github.sha }}

Please let us know whether this is a new topic or a topic change request:

  • New Topic Request (ie. missing entire topic/section)
  • Topic Change Request (ie. spelling, organization)

Full working files for deployment to Google Cloud Run

Dockerfile:

FROM node:12.16.3-alpine as build
# working directory
WORKDIR /usr/src/app

# global environment setup : yarn + dependencies needed to support node-gyp
RUN apk --no-cache --virtual add \
    python \
    make \
    g++ \
    yarn

# set env variable for CI
ENV CI=true

# copy root dependency files and configs needed for install
COPY package.json yarn.lock babel.config.js magento-compatibility.js ./
COPY scripts/monorepo-introduction.js ./scripts/monorepo-introduction.js

# copy over the packages
COPY packages ./packages

# copy configuration env file from host file system to venia-concept .env for build
COPY ./docker/.env.docker.prod ./packages/venia-concept/.env

# specified as --build-arg
ARG MAGENTO_BACKEND_URL
ENV MAGENTO_BACKEND_URL=$MAGENTO_BACKEND_URL
ARG MAGENTO_BACKEND_EDITION
ENV MAGENTO_BACKEND_EDITION=$MAGENTO_BACKEND_EDITION

# install dependencies with yarn
RUN yarn install --frozen-lockfile

ENV NODE_ENV=production
# build the app
RUN yarn run build

# MULTI-STAGE BUILD
FROM node:12.16.3-alpine
# working directory
WORKDIR /usr/src/app
# node:alpine comes with a configured user and group
RUN chown -R node:node /usr/src/app
# copy build from previous stage
COPY --from=build /usr/src/app .
USER node
EXPOSE 8080
ENV NODE_ENV=production
# command to run application
CMD [ "yarn", "stage:venia" ]

Deployment file:

# ${{ secrets.GCP_PROJECT }} eg. venia-123456
# ${{ secrets.GCP_SA_KEY }} service account key in JSON format
# ${{ secrets.GCP_REGION }} eg. europe-north1
# ${{ secrets.GCP_SERVICE }} eg. venia
# ${{ secrets.MAGENTO_BACKEND_URL }} eg. https://magento.mydomain.com
# ${{ secrets.MAGENTO_BACKEND_EDITION }} eg. EE or CE

name: Google Cloud Run Deploy
on:
  push:
    branches:
      - google-cloud-run

jobs:
  deploy-gcr:
    name: Deploy to GCR
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repo
        uses: actions/checkout@main

      - name: Setup Cloud SDK
        uses: google-github-actions/setup-gcloud@v0.2.0
        with:
          project_id: ${{ secrets.GCP_PROJECT }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}

      - name: Authorize Docker push
        run: gcloud auth configure-docker

      - name: Build and Push Container
        run: |-
          docker build \
          --build-arg MAGENTO_BACKEND_URL=${{ secrets.MAGENTO_BACKEND_URL }} \
          --build-arg MAGENTO_BACKEND_EDITION=${{ secrets.MAGENTO_BACKEND_EDITION }} \
          -t gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_SERVICE }}:${{ github.sha }} \
          -f gcr.dockerfile .
          docker push gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_SERVICE }}:${{ github.sha }}

      - name: Deploy to Cloud Run
        run: |-
          gcloud run deploy ${{ secrets.GCP_SERVICE }} \
            --region ${{ secrets.GCP_REGION }} \
            --image gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_SERVICE }}:${{ github.sha }} \
            --platform "managed" \
            --quiet \
            --allow-unauthenticated \
            --set-env-vars "MAGENTO_BACKEND_URL=${{ secrets.MAGENTO_BACKEND_URL }}" \
            --set-env-vars "MAGENTO_BACKEND_EDITION=${{ secrets.MAGENTO_BACKEND_EDITION }}"
@rasmuswikman rasmuswikman added the documentation This pertains to documentation. label Nov 19, 2021
@m2-assistant
Copy link

m2-assistant bot commented Nov 19, 2021

Hi @rasmuswikman. Thank you for your report.
To speed up processing of this issue, make sure that you provided sufficient information.

Add a comment to assign the issue: @magento I am working on this


@anthoula
Copy link
Contributor

@magento export issue to JIRA project PWA as Story

@anthoula anthoula added the needs-triage A pull request or issue that needs to be triaged prior to being synced to JIRA label May 16, 2022
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.magento.com/browse/PWA-2842 is successfully created for this GitHub issue.

@anthoula anthoula added Progress: ready for grooming and removed needs-triage A pull request or issue that needs to be triaged prior to being synced to JIRA labels May 16, 2022
@github-jira-sync-bot github-jira-sync-bot changed the title [doc]: Deployment on eg. Google Cloud Run [doc]: Deployment on eg. Google Cloud Run - using --build-arg for dockerfiles May 19, 2022
@OneCricketeer
Copy link
Contributor

Hi @rasmuswikman is this ticket still necessary?

I see you have set runtime variables

--set-env-vars "MAGENTO_BACKEND_URL=${{ secrets.MAGENTO_BACKEND_URL }}" \
--set-env-vars "MAGENTO_BACKEND_EDITION=${{ secrets.MAGENTO_BACKEND_EDITION }}"

So, what is the use-case for providing build time variables with --build-arg?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This pertains to documentation. Progress: ready for dev
Projects
None yet
Development

No branches or pull requests

4 participants