Skip to content

Commit

Permalink
Change build-push-image action
Browse files Browse the repository at this point in the history
  • Loading branch information
jcobarreto committed May 14, 2024
1 parent a2ea2bc commit 50220eb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
36 changes: 36 additions & 0 deletions build-push-image/Dockerfile.gha-deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Create the image with Nginx environment.
FROM nginx:alpine

# Copy results from the dist folder to the image.
ARG DIST_FOLDER
RUN [ -z "$DIST_FOLDER" ] && echo "DIST_FOLDER is required" && exit 1 || true
COPY $DIST_FOLDER /usr/share/nginx/html

# Remove source maps from the production build
RUN find /usr/share/nginx/html -name "*.map" -type f -delete

# Copy the entrypoint.
ARG ENV_RUNTIME_ENTRYPOINT
RUN [ -z "$ENV_RUNTIME_ENTRYPOINT" ] && echo "ENV_RUNTIME_ENTRYPOINT is required" && exit 1 || true
COPY $ENV_RUNTIME_ENTRYPOINT /usr/local/bin/env-runtime.entrypoint.sh
RUN chmod +x /usr/local/bin/env-runtime.entrypoint.sh

# version is used in build process, so the value won't be available here otherwise
ARG VERSION
RUN [ -z "$VERSION" ] && echo "VERSION is required" && exit 1 || true
ENV DAVINCI_RELEASE_VERSION $VERSION

RUN echo $VERSION > /usr/share/nginx/html/REVISION.txt

# Copy configuration file for Nginx and replace the default one.
ARG NGINX_CONFIG
RUN [ -z "$NGINX_CONFIG" ] && echo "NGINX_CONFIG is required" && exit 1 || true
COPY $NGINX_CONFIG /etc/nginx/conf.d/default.conf.templ

ARG HTML_CDN_CACHE_TAG="davinci-app"
ENV HTML_CDN_CACHE_TAG=${HTML_CDN_CACHE_TAG}

# Entry point to provide Run Time Environment Variables for the App.
ENTRYPOINT ["/usr/local/bin/env-runtime.entrypoint.sh"]

CMD ["nginx", "-g", "daemon off;"]
25 changes: 12 additions & 13 deletions build-push-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ This GH Action builds a Docker image and pushes to google cloud.

The list of arguments, that are used in GH Action:

| name | type | required | default | description |
| ---------------- | ----------------------------------------------------------- | -------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| `sha` | string || | Commit hash that will be used as a tag for the Docker image |
| `image-name` | string || | Name of the Docker image. Might be used in the next steps (for ex.: deploy a Docker image) |
| `environment` | enum<<br/>`temploy`,<br/>`staging`,<br/>`production`,<br/>> | | staging | Determines additional procedures while creating a Docker image. |
| `build-args` | string || | Multiline string to describe build arguments that will be used during dockerization |
| `docker-file` | string | | ./davinci/packages/ci/src/configs/docker/Dockerfile.gha-deploy | pathname to Docker file |
| `davinci-branch` | string | | master | Custom davinci branch |
| `node-version` | string | | 18 | Node.js version used. The action is guaranteed to work only with Node.js@18 (default value) |
| name | type | required | default | description |
| ------------- | ----------------------------------------------------------- | -------- | ---------- | ------------------------------------------------------------------------------------------ |
| `sha` | string || | Commit hash that will be used as a tag for the Docker image |
| `image-name` | string || | Name of the Docker image. Might be used in the next steps (for ex.: deploy a Docker image) |
| `environment` | enum<<br/>`temploy`,<br/>`staging`,<br/>`production`,<br/>> | | staging | Determines additional procedures while creating a Docker image. |
| `build-args` | string || | Multiline string to describe build arguments that will be used during dockerization |
| `docker-file` | string | | Dockerfile | pathname to Dockerfile |

### Outputs

Expand All @@ -31,10 +29,11 @@ Not specified
All ENV Variables, defined in a GH Workflow are also passed to a GH Action. It means, the might be reused as is.
This is a list of ENV Variables that are used in GH Action:

| name | description |
| ----------------- | -------------------------------------------------- |
| `GITHUB_TOKEN` | GitHub token. Is used to checkout `davinci` branch |
| `GCR_ACCOUNT_KEY` | Necessary token to push image to Google cloud |
| name | description |
| -------------------------- | -------------------------------------------------- |
| `GITHUB_TOKEN` | GitHub token. Is used to checkout `davinci` branch |
| `GCR_ACCOUNT_KEY` | Necessary token to push image to Google cloud |
| `TOPTAL_BUILD_BOT_SSH_KEY` | SSH key to access Google cloud |

### Usage

Expand Down
56 changes: 28 additions & 28 deletions build-push-image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description: |
envInputs:
GITHUB_TOKEN: GitHub token. Is used to checkout `davinci` branch
GCR_ACCOUNT_KEY: Necessary token to push image to Google cloud
TOPTAL_BUILD_BOT_SSH_KEY: SSH key to access Google cloud
inputs:
sha:
Expand All @@ -21,52 +22,43 @@ inputs:
required: true
description: 'Multiline string to describe build arguments that will be used during dockerization'
docker-file:
description: 'pathname to Docker file'
description: 'pathname to Dockerfile'
required: false
default: ./davinci/packages/ci/src/configs/docker/Dockerfile.gha-deploy
davinci-branch:
description: 'Custom davinci branch'
required: false
default: 'master'
node-version:
required: false
default: 18
description: 'Node.js version used. The action is guaranteed to work only with Node.js@18 (default value)'
default: Dockerfile

runs:
using: composite
steps:
- name: Check out davinci
uses: actions/checkout@v3
with:
repository: toptal/davinci
token: ${{ env.GITHUB_TOKEN }}
path: davinci
ref: ${{ inputs.davinci-branch }}

- name: Set up node
uses: actions/[email protected]
with:
node-version: ${{ inputs.node-version }}

- id: meta-latest
shell: bash
env:
ENVIRONMENT: ${{ inputs.environment }}
run: |
latest=$(if [[ $ENVIRONMENT == 'temploy' ]]; then echo false; else echo true; fi)
echo latest=$latest >> $GITHUB_OUTPUT
echo "APP_VERSION=$(git describe --tags --always)" >> $GITHUB_ENV
echo "COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV
- name: Docker meta
uses: docker/metadata-action@v5
id: meta
uses: docker/[email protected]
with:
images: |
us-central1-docker.pkg.dev/toptal-hub/containers/${{ inputs.image-name }}
tags: |
type=raw,enable=true,priority=200,prefix=,suffix=,value=${{ inputs.sha }}
type=sha,enable=true,format=long
type=raw,enable=true,value=${{ github.head_ref }}
type=raw,enable=true,value=${{ env.APP_VERSION }}
type=raw,enable=true,value=${{ env.COMMIT }}
type=raw,enable=true,value=master,enable=${{ github.head_ref == 'ci-3421-move-build-image-gha' }}
flavor: |
latest=${{ steps.meta-latest.outputs.latest }}
# latest=false
- name: Use SSH key
uses: toptal/[email protected]
with:
ssh-private-key: ${{ env.TOPTAL_BUILD_BOT_SSH_KEY }}

- name: Login to Google Artifact Registry - GAR
uses: docker/login-action@v3
Expand All @@ -75,16 +67,24 @@ runs:
username: _json_key
password: ${{ env.GCR_ACCOUNT_KEY }}

- name: Set up Docker Buildx
id: buildx
- name: Set up Dcoker Buildx - CI custom runners
if: contains(runner.name, 'inf-gha-runners-runners')
uses: docker/setup-buildx-action@v3
with:
driver: remote
endpoint: 'tcp://10.154.0.46:1234'

- name: Set up Docker Buildx - GH runners
if: "!contains(runner.name, 'inf-gha-runners-runners')"
uses: docker/setup-buildx-action@v3

- name: Build and push release image
uses: docker/build-push-action@v5.1.0
uses: docker/build-push-action@v5
with:
provenance: false
tags: ${{ steps.meta.outputs.tags }}
push: true
context: .
file: ${{ inputs.docker-file }}
build-args: ${{ inputs.build-args }}
ssh: default

0 comments on commit 50220eb

Please sign in to comment.