Skip to content

Commit

Permalink
feat: PLT-583 push docker image to GAR in single workflow (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktslau authored Dec 11, 2023
1 parent 53f2329 commit e8ceb71
Showing 1 changed file with 82 additions and 27 deletions.
109 changes: 82 additions & 27 deletions .github/workflows/cron_deploy-from-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:
image_repo:
required: true
type: string
description: "Your container registry repository - like eu.gcr.io/ultimateai-169214"
default: europe-docker.pkg.dev/common-main-cfc4/eu.gcr.io
description: "Your container registry repository - like europe-docker.pkg.dev/common-main-cfc4/eu.gcr.io"
app_squad:
required: true
type: string
Expand Down Expand Up @@ -58,9 +59,6 @@ on:
description: "Npm version used in the pipeline, defaults to LTS version"





env:
IMAGE_REPO: ${{ inputs.image_repo }}
APP_SQUAD: ${{ inputs.app_squad }}
Expand All @@ -74,6 +72,8 @@ env:
NPM_INSTALL_COMMAND: ${{ inputs.npm_install_command }}
NODE_VERSION: ${{ inputs.node_version }}
NPM_VERSION: ${{ inputs.npm_version }}
GCR_ENABLED: ${{ vars.GCR_ENABLED || 'true' }}
GAR_ENABLED: ${{ vars.GAR_ENABLED || 'true' }}


jobs:
Expand All @@ -84,13 +84,19 @@ jobs:
contents: "read"
id-token: "write"
steps:
- name: Validation
if: ${{ env.GCR_ENABLED != 'true' && env.GAR_ENABLED != 'true' }}
run: |
echo "At least one of GCR_ENABLED or GAR_ENABLED must be set to true.'
exit 1
- name: Checkout to selected branch
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ env.BRANCH_NAME }}

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

Expand All @@ -99,10 +105,31 @@ jobs:
run: npm i -g npm@${{ env.NPM_VERSION }} --registry=https://registry.npmjs.org

- id: "image_name"
name: "Get repo name and update IMAGE_NAME"
name: "Extract repo name and update IMAGE_NAME"
run: |-
IMAGE_NAME=$IMAGE_REPO"/"${{ github.event.repository.name }}
echo "image_name=$IMAGE_NAME" >> $GITHUB_OUTPUT
REPO_DOMAIN=$(echo $IMAGE_REPO | cut -d '/' -f 1)
case $REPO_DOMAIN in
*gcr.io)
GCR_DOMAIN=$REPO_DOMAIN
GAR_DOMAIN=europe-docker.pkg.dev
GCR_IMAGE_REPO=$IMAGE_REPO
GAR_IMAGE_REPO=europe-docker.pkg.dev/common-main-cfc4/eu.gcr.io ;;
*pkg.dev)
GCR_DOMAIN=eu.gcr.io
GAR_DOMAIN=$REPO_DOMAIN
GCR_IMAGE_REPO=eu.gcr.io/ultimateai-169214
GAR_IMAGE_REPO=$IMAGE_REPO ;;
esac
GCR_IMAGE_NAME=$GCR_IMAGE_REPO"/"${{ github.event.repository.name }}
GAR_IMAGE_NAME=$GAR_IMAGE_REPO"/"${{ github.event.repository.name }}
echo "gcr_domain=$GCR_DOMAIN" >> $GITHUB_OUTPUT
echo "gar_domain=$GAR_DOMAIN" >> $GITHUB_OUTPUT
echo "gcr_image_repo=$GCR_IMAGE_REPO" >> $GITHUB_OUTPUT
echo "gar_image_repo=$GAR_IMAGE_REPO" >> $GITHUB_OUTPUT
echo "gcr_image_name=$GCR_IMAGE_NAME" >> $GITHUB_OUTPUT
echo "gar_image_name=$GAR_IMAGE_NAME" >> $GITHUB_OUTPUT
- name: Get .env file and scheduler
run: |-
Expand All @@ -129,7 +156,7 @@ jobs:
### Authentings with Google Cloud to Push Image to GCR ###
- id: "auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v0"
uses: "google-github-actions/auth@v2"
with:
token_format: "access_token"
workload_identity_provider: "${{ secrets.WIF_PROVIDER_NAME }}"
Expand All @@ -155,27 +182,47 @@ jobs:
run: $BUILD_COMMAND

- name: "Authenticating docker to push to gcr"
uses: docker/login-action@v2.1.0
if: ${{ env.GCR_ENABLED == 'true' }}
uses: docker/login-action@v3
with:
registry: "${{ steps.image_name.outputs.GCR_DOMAIN }}"
username: "oauth2accesstoken"
password: "${{ steps.auth.outputs.access_token }}"

- name: "Authenticating docker to push to gar"
if: ${{ env.GAR_ENABLED == 'true' }}
uses: docker/login-action@v3
with:
registry: "eu.gcr.io"
registry: "${{ steps.image_name.outputs.GAR_DOMAIN }}"
username: "oauth2accesstoken"
password: "${{ steps.auth.outputs.access_token }}"

- name: Docker build
run: |-
docker build -t "${IMAGE_NAME}:${TAGS}" .
docker build -t "${GCR_IMAGE_NAME}:${TAGS}" -t "${GAR_IMAGE_NAME}:${TAGS}" .
shell: bash
env:
TAGS: ${{ steps.get_image_tag.outputs.image_tag }}
GCR_IMAGE_NAME: ${{ steps.image_name.outputs.gcr_image_name }}
GAR_IMAGE_NAME: ${{ steps.image_name.outputs.gar_image_name }}

- name: Docker push to GCR
if: ${{ env.GCR_ENABLED == 'true' }}
run: |-
docker push "${IMAGE_NAME}:${TAGS}"
shell: bash
env:
TAGS: ${{ steps.get_image_tag.outputs.image_tag }}
IMAGE_NAME: ${{ steps.image_name.outputs.image_name }}
IMAGE_NAME: ${{ steps.image_name.outputs.gcr_image_name }}

- name: Docker push
- name: Docker push to GAR
if: ${{ env.GAR_ENABLED == 'true' }}
run: |-
docker push "${IMAGE_NAME}:${TAGS}"
shell: bash
env:
TAGS: ${{ steps.get_image_tag.outputs.image_tag }}
IMAGE_NAME: ${{ steps.image_name.outputs.image_name }}
IMAGE_NAME: ${{ steps.image_name.outputs.gar_image_name }}

##Deploy to dev
- name: "Checkout k8s manifests"
Expand All @@ -186,18 +233,26 @@ jobs:

- name: Update the Image tags for Dev Environment
run: |-
cd $APP_SQUAD/${{ github.event.repository.name }}/overlay/${K8S_ENV_NAME}
cat $HOME/dev-copy.env > dev.env
scheduler=$(cat $HOME/dev-scheduler-copy)
yq ".spec.schedule = \"$scheduler\"" -i cronjob.yaml
kustomize edit set image app=${IMAGE_NAME}:${TAG}
cat kustomization.yaml
git config --local user.email $GITHUB_EMAIL
git config --local user.name $GITHUB_USER
git commit -m "update image to ${IMAGE_NAME}:${TAG}" -a
git push ${REMOTE_REPO} HEAD:main --force
if [ "$GAR_ENABLED" == "true" ]
then
IMAGE_NAME=$GAR_IMAGE_NAME
else
IMAGE_NAME=$GCR_IMAGE_NAME
fi
cd $APP_SQUAD/${{ github.event.repository.name }}/overlay/${K8S_ENV_NAME}
cat $HOME/dev-copy.env > dev.env
scheduler=$(cat $HOME/dev-scheduler-copy)
yq ".spec.schedule = \"$scheduler\"" -i cronjob.yaml
kustomize edit set image app=${IMAGE_NAME}:${TAG}
cat kustomization.yaml
git config --local user.email $GITHUB_EMAIL
git config --local user.name $GITHUB_USER
git commit -m "update image to ${IMAGE_NAME}:${TAG}" -a
git push ${REMOTE_REPO} HEAD:main --force
env:
TAG: ${{ steps.get_image_tag.outputs.image_tag }}
REMOTE_REPO: https://${{ secrets.ULTIMATE_GITHUB_PAT }}@github.com/${{ env.K8S_MANIFESTS_REPO }}.git
K8S_ENV_NAME: development
IMAGE_NAME: ${{ steps.image_name.outputs.image_name }}
GCR_IMAGE_NAME: ${{ steps.image_name.outputs.gcr_image_name }}
GAR_IMAGE_NAME: ${{ steps.image_name.outputs.gar_image_name }}

0 comments on commit e8ceb71

Please sign in to comment.