Skip to content

Instantly share code, notes, and snippets.

@psteinroe
Last active June 20, 2022 02:01
Show Gist options
  • Save psteinroe/4628bcf86c8ca24279b2a57663088659 to your computer and use it in GitHub Desktop.
Save psteinroe/4628bcf86c8ca24279b2a57663088659 to your computer and use it in GitHub Desktop.
cd.yaml
on:
# push:
# branches:
# - prod
# - staging
workflow_dispatch:
name: CD
env:
TAG: ${{ github.sha }}
GKE_CLUSTER: main
GKE_ZONE: europe-west3
jobs:
cd:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get branch names
id: branch-name
uses: tj-actions/branch-names@v4
- name: Fetch
run: git fetch origin staging
- name: Install dependencies
run: yarn
- name: Get last successful commit
uses: nrwl/last-successful-commit-action@v1
id: last_successful_commit
with:
branch: ${{ steps.branch-name.outputs.current_branch }}
workflow_id: 'cd.yaml'
github_token: ${{ secrets.GH_PERSONAL_TOKEN }}
- name: Check which apps are affected
id: check-affected-apps
run: |-
affected="$(npx nx affected:apps --base=${{ steps.last_successful_commit.outputs.commit_hash }} --plain)"
has_affected_apps=false
for run in {1..10}; do
app_name=$( echo "$affected" | cut -d' ' -f$run )
if [ -n "$app_name" ]; then
has_affected_apps=true
echo "Affected: $app_name"
echo "affected_${app_name}=true" >> $GITHUB_ENV
fi
done
echo $has_affected_apps
echo "::set-output name=has_affected_apps::$has_affected_apps"
- name: Get project id from branch name
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: echo "PROJECT_ID=mateo-${{ steps.branch-name.outputs.current_branch }}" >> $GITHUB_ENV
- name: Setup gcloud cli
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
uses: google-github-actions/setup-gcloud@v0.2.0
with:
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
project_id: ${{ env.PROJECT_ID }}
- name: Configure docker
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: gcloud --quiet auth configure-docker
- name: Get GKE Credentials
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
uses: google-github-actions/get-gke-credentials@v0.2.1
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
credentials: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
project_id: ${{ env.PROJECT_ID }}
- name: Build Base Image
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: yarn base:dockerize
- name: Publish Base Image
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: yarn base:push
- name: Build Affected
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: yarn affected:dockerize --base=${{ steps.last_successful_commit.outputs.commit_hash }} --parallel
- name: Publish Affected
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
run: yarn affected:push --base=${{ steps.last_successful_commit.outputs.commit_hash }} --parallel
- name: Setup Helmfile
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
uses: mamezou-tech/setup-helmfile@v0.8.0
- name: Deploy
if: steps.check-affected-apps.outputs.has_affected_apps == 'true'
working-directory: ./deploy
run: helmfile -e ${{ steps.branch-name.outputs.current_branch }} apply
"dockerize": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": [
"docker build -t eu.gcr.io/$PROJECT_ID/<your-app-name-here>:$TAG -f service.Dockerfile . --build-arg SERVICE_NAME=network --build-arg PROJECT_ID=$PROJECT_ID --build-arg TAG=$TAG"
]
}
},
"push": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": [
"docker push eu.gcr.io/$PROJECT_ID/<your-app-name-here>:$TAG"
]
}
},
FROM node:fermium-alpine AS builder
WORKDIR /app/builder
COPY . .
# https://github.com/nodejs/docker-node/issues/384#issuecomment-305208112
RUN apk --no-cache add --virtual native-deps \
git g++ gcc libgcc libstdc++ linux-headers make python && \
yarn global add --silent node-gyp &&\
yarn --silent && \
apk del native-deps
ARG PROJECT_ID
ARG TAG
FROM eu.gcr.io/$PROJECT_ID/base:$TAG AS builder
ENV NODE_ENV production
ARG SERVICE_NAME
WORKDIR /app/builder
COPY . .
RUN yarn build $SERVICE_NAME --prod
FROM node:fermium-alpine
ENV NODE_ENV production
ARG SERVICE_NAME
WORKDIR /app
COPY --from=builder /app/builder ./
ENV SERVICE_NAME $SERVICE_NAME
CMD ["sh", "-c", "node ./dist/apps/${SERVICE_NAME}/main.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment