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 28, 2024
1 parent 1278bed commit 252bb76
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
28 changes: 18 additions & 10 deletions build-push-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ 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 | | ${{ github.sha }} | 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) |
| `build-args` | string | | | Multiline string to describe build arguments that will be used during dockerization |
| `environment` | enum<<br/>`temploy`,<br/>`staging`,<br/>`production`,<br/>> | | staging | Determines additional procedures while creating a Docker image. |
| `docker-file` | string | | Dockerfile | pathname to Dockerfile |
| `davinci-branch` | string | | master | Custom davinci branch |
| `tags` | string | | | Additional tags for the Docker image |
| name | type | required | default | description |
| ------------------ | ----------------------------------------------------------- | -------- | ------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| `sha` | string | | ${{ github.sha }} | 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) |
| `build-args` | string | | | Multiline string to describe build arguments that will be used during dockerization |
| `environment` | enum<<br/>`temploy`,<br/>`staging`,<br/>`production`,<br/>> | | staging | Determines additional procedures while creating a Docker image. |
| `docker-file` | string | | Dockerfile | pathname to Dockerfile |
| `davinci-branch` | string | | master | Custom davinci branch |
| `labels` | string | | | List of metadata for the Docker image |
| `context` | string | | . | Build context |
| `push` | string | | false | Push the image to the registry |
| `platforms` | string | | linux/amd64 | List of target platforms for build |
| `tags` | string | | | Additional tags for the Docker image |
| `target` | string | | | Sets the target stage to build |
| `checkout-davinci` | string | | false | Checkout davinci repository |
| `registry-name` | string | | us-central1-docker.pkg.dev/toptal-hub/containers | Registry to push the builded image |

### Outputs

Expand All @@ -33,8 +40,9 @@ 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 |
| `DOCKER_BUILDX_ENDPOINT` | Docker buildx endpoint |
| `GCR_ACCOUNT_KEY` | Necessary token to push image to Google cloud |
| `GITHUB_TOKEN` | GitHub token. Is used to checkout `davinci` branch |
| `TOPTAL_BUILD_BOT_SSH_KEY` | SSH key to access Google cloud |

### Usage
Expand Down
67 changes: 56 additions & 11 deletions build-push-image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ description: |
Builds release image of a project and pushes to cloud
****
envInputs:
GITHUB_TOKEN: GitHub token. Is used to checkout `davinci` branch
DOCKER_BUILDX_ENDPOINT: Docker buildx endpoint
GCR_ACCOUNT_KEY: Necessary token to push image to Google cloud
GITHUB_TOKEN: GitHub token. Is used to checkout `davinci` branch
TOPTAL_BUILD_BOT_SSH_KEY: SSH key to access Google cloud
inputs:
Expand All @@ -27,43 +28,83 @@ inputs:
description: 'pathname to Dockerfile'
default: Dockerfile
davinci-branch:
description: 'Custom davinci branch'
required: false
description: 'Custom davinci branch'
default: 'master'
labels:
required: false
description: 'List of metadata for the Docker image'
default: ''
context:
required: false
description: 'Build context'
default: '.'
push:
required: false
description: 'Push the image to the registry'
default: 'false'
platforms:
required: false
description: 'List of target platforms for build'
default: 'linux/amd64'
tags:
required: false
description: 'Additional tags for the Docker image'
target:
required: false
description: 'Sets the target stage to build'
checkout-davinci:
required: false
description: 'Checkout davinci repository'
default: 'false'
registry-name:
required: false
description: 'Registry to push the builded image'
default: 'us-central1-docker.pkg.dev/toptal-hub/containers'

runs:
using: composite
steps:
- name: Check out davinci
uses: actions/checkout@v3
if: ${{ inputs.checkout-davinci == 'true' }}
uses: actions/checkout@v4
with:
repository: toptal/davinci
token: ${{ env.GITHUB_TOKEN }}
path: davinci
sparse-checkout: Dockerfile.gha-deploy
sparse-checkout: |
/packages/ci/src/configs/docker/Dockerfile.gha-deploy
/packages/ci/src/configs/docker/env-runtime.entrypoint.sh
/packages/davinci/docker/nginx-vhost.conf
sparse-checkout-cone-mode: false
ref: ${{ inputs.davinci-branch }}

- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
# - name: Env variables
# shell: bash
# env:
# AR: ${{ inputs.registry-name }}
# run: |
# echo registry-region=$(echo $AR | awk -F'/' '{print $1}') >> $GITHUB_OUTPUT

- id: meta-latest
shell: bash
env:
ENVIRONMENT: ${{ inputs.environment }}
REGISTRY: ${{ inputs.registry-name }}
run: |
latest=$(if [[ $ENVIRONMENT == 'temploy' ]]; then echo false; else echo true; fi)
echo latest=$latest >> $GITHUB_OUTPUT
echo REGISTRY_REGION=$(echo $REGISTRY | awk -F'/' '{print $1}') >> $GITHUB_ENV
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3

- name: Docker meta
uses: docker/metadata-action@v5
id: meta
with:
images: |
us-central1-docker.pkg.dev/toptal-hub/containers/${{ inputs.image-name }}
${{ inputs.registry-name }}/${{ inputs.image-name }}
tags: |
type=raw,enable=true,priority=200,prefix=,suffix=,value=${{ inputs.sha }}
${{ inputs.tags }}
Expand All @@ -78,7 +119,8 @@ runs:
- name: Login to Google Artifact Registry - GAR
uses: docker/login-action@v3
with:
registry: us-central1-docker.pkg.dev
# registry: us-central1-docker.pkg.dev
registry: ${{ steps.meta-latest.outputs.REGISTRY_REGION }}
username: _json_key
password: ${{ env.GCR_ACCOUNT_KEY }}

Expand All @@ -87,7 +129,7 @@ runs:
uses: docker/setup-buildx-action@v3
with:
driver: remote
endpoint: 'tcp://10.154.0.46:1234'
endpoint: ${{ env.DOCKER_BUILDX_ENDPOINT }}

- name: Set up Docker Buildx - GH runners
if: "!contains(runner.name, 'inf-gha-runners-runners')"
Expand All @@ -97,9 +139,12 @@ runs:
uses: docker/build-push-action@v5
with:
provenance: false
platforms: ${{ inputs.platforms }}
tags: ${{ steps.meta.outputs.tags }}
push: true
context: .
target: ${{ inputs.target }}
push: ${{ inputs.push }}
context: ${{ inputs.context }}
labels: ${{ inputs.labels }}
file: ${{ inputs.docker-file }}
build-args: ${{ inputs.build-args }}
ssh: default

0 comments on commit 252bb76

Please sign in to comment.