Skip to content

Commit

Permalink
Modularize Github Action usage with shared workflows (#200)
Browse files Browse the repository at this point in the history
* Modularize Github Action usage with shared workflows.

* Prefix shared workflows with "_", since we can't have subdirectories in .github/workflows

* Remove shared _setup.yml workflow and duplicate its steps in the terraform and validate workflows (we can't reused the outputs across jobs)
  • Loading branch information
danielnaab authored Jun 18, 2024
1 parent a5c02fd commit fd6941b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 152 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/_apply-terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Apply Terraform CDK configuration

on:
workflow_call:

jobs:
apply-terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Read node version from `.nvmrc` file
id: nvmrc
shell: bash
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)

- name: Install required node.js version
uses: actions/setup-node@v3
with:
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}

- name: Install pnpm
uses: pnpm/action-setup@v2
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Initialize Terraform CDK configuration
shell: bash
run: |
cd infra
pnpm cdktf get
pnpm build:tsc
- name: Install CloudFoundry CLI
run: |
curl -L "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=v8&source=github" | sudo tar -zx --directory=/usr/local/bin
cf --version
- name: Apply Terraform CDK configuration
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
shell: bash
run: |
cd infra
cf api https://api.fr.cloud.gov
DEPLOY_ENV=${TAG_NAME} pnpm cdktf deploy --auto-approve
43 changes: 43 additions & 0 deletions .github/workflows/_build-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build Docker image

on:
workflow_call:
inputs:
app-name:
required: true
type: string
repo-name:
type: string
default: ${{ github.repository }}

env:
TAG_NAME: ${{ github.ref_name }}
APP_DIR: ${{ inputs.app-name }}
REGISTRY_PATH: ghcr.io/${{ inputs.repo-name }}/${{ inputs.app-name }}

jobs:
setup:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build Docker image
run: |
docker build . --platform linux/amd64 --target app --build-arg APP_DIR=${APP_DIR} --tag ${REGISTRY_PATH}:${github.sha}
- name: Tag Docker image
run: |
docker tag ${REGISTRY_PATH}:${github.sha} ${REGISTRY_PATH}:${TAG_NAME}
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to container registry
run: |
docker push --all-tags ${REGISTRY_PATH}
69 changes: 69 additions & 0 deletions .github/workflows/_validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Validate

on:
workflow_call:

jobs:
run-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Read node version from `.nvmrc` file
id: nvmrc
shell: bash
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)

- name: Install required node.js version
uses: actions/setup-node@v3
with:
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}

- name: Install pnpm
uses: pnpm/action-setup@v2
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Lint source code
shell: bash
run: pnpm lint

- name: Run test suite
shell: bash
run: pnpm test

- name: Initialize Terraform CDK configuration
shell: bash
run: |
cd infra
pnpm cdktf get
pnpm build:tsc
- name: Typecheck source code
shell: bash
run: pnpm typecheck

#- name: Vitest Coverage Report
# if: always()
# uses: davelosert/[email protected]
98 changes: 13 additions & 85 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,18 @@ env:
REGISTRY: ghcr.io/gsa-tts/atj-platform

jobs:
build-image:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build container image
run: |
docker build . --platform linux/amd64 --target app --build-arg APP_DIR=doj-demo --tag ${REGISTRY}/doj-demo:${TAG_NAME}
docker build . --platform linux/amd64 --target app --build-arg APP_DIR=spotlight --tag ${REGISTRY}/spotlight:${TAG_NAME}
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to container registry
run: |
docker push ${REGISTRY}/doj-demo:${TAG_NAME}
docker push ${REGISTRY}/spotlight:${TAG_NAME}
build-image-doj:
uses: ./.github/workflows/_build-docker-image.yml
secrets: inherit
with:
app-name: doj-demo

build-image-spotlight:
uses: ./.github/workflows/_build-docker-image.yml
secrets: inherit
with:
app-name: spotlight

deploy:
runs-on: ubuntu-latest
needs: [build-image]
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Read node version from `.nvmrc` file
id: nvmrc
shell: bash
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)

- name: Install required node.js version
uses: actions/setup-node@v3
with:
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}

- name: Install pnpm
uses: pnpm/action-setup@v2
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Initialize Terraform CDK configuration
shell: bash
run: |
cd infra
pnpm cdktf get
pnpm build:tsc
- name: Install CloudFoundry CLI
run: |
curl -L "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=v8&source=github" | sudo tar -zx --directory=/usr/local/bin
cf --version
- name: Apply Terraform CDK configuration
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
CF_USER: ${{ secrets.CF_USER }}
CF_PASSWORD: ${{ secrets.CF_PASSWORD }}
shell: bash
run: |
cd infra
cf api https://api.fr.cloud.gov
DEPLOY_ENV=${TAG_NAME} pnpm cdktf deploy --auto-approve
uses: ./.github/workflows/_apply-terraform.yml
secrets: inherit
68 changes: 1 addition & 67 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,4 @@ on:

jobs:
run-tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
arch: [x86_64]

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Read node version from `.nvmrc` file
id: nvmrc
shell: bash
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)

- name: Install required node.js version
uses: actions/setup-node@v3
with:
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}

- name: Install pnpm
uses: pnpm/action-setup@v2
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Lint source code
shell: bash
run: pnpm lint

- name: Run test suite
shell: bash
run: pnpm test

- name: Initialize Terraform CDK configuration
shell: bash
run: |
cd infra
pnpm cdktf get
pnpm build:tsc
- name: Typecheck source code
shell: bash
run: pnpm typecheck

#- name: Vitest Coverage Report
# if: always()
# uses: davelosert/[email protected]
uses: ./.github/workflows/_validate.yml

0 comments on commit fd6941b

Please sign in to comment.