Skip to content

Commit

Permalink
chore(cicd): setup CI/CD (#1)
Browse files Browse the repository at this point in the history
* chore: check on pr

* chore: add check on pr

* fix: fix location

* chore: set up deploy ci

* fix: skip steps when nothing to check

* perf: remove debugging echo
  • Loading branch information
henrylee97 authored Nov 15, 2023
1 parent feeb7ad commit 8cae563
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build an image

on:
workflow_call:
inputs:
configuration:
required: true
type: string
registry:
required: true
type: string
repo:
required: true
type: string

env:
REGISTRY: ${{ inputs.registry }}
REPO: ${{ inputs.repo }}

jobs:
build:
permissions:
contents: read
packages: write

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Extract configurations
id: extract-configurations
run: |
echo "context=$(cat ${{ inputs.configuration }} | jq -r .context)" >> ${GITHUB_OUTPUT}
echo "name=$(cat ${{ inputs.configuration }} | jq -r .name)" >> ${GITHUB_OUTPUT}
echo "tag=$(cat ${{ inputs.configuration }} | jq -r .tag)" >> ${GITHUB_OUTPUT}
export build_args=$(cat ${{ inputs.configuration }} | jq -r .build_args)
{
echo "buildargs<<EOF"
for key in $(cat ${{ inputs.configuration }} | jq -r '.build_args | keys | .[]'); do
echo "${key}=$(cat ${{ inputs.configuration }} | jq -r ".build_args.${key}")"
done
echo "EOF"
} >> ${GITHUB_OUTPUT}
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v5
with:
context: ${{ steps.extract-configurations.outputs.context }}
file: ${{ steps.extract-configurations.outputs.context }}/Dockerfile
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REPO }}/${{ steps.extract-configurations.outputs.name }}:${{ steps.extract-configurations.outputs.tag }}
build-args: ${{ steps.extract-configurations.outputs.buildargs }}
labels: |
org.opencontainers.image.source="https://github.com/${{ env.REPO }}"
74 changes: 74 additions & 0 deletions .github/workflows/check-on-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Check configurations

on:
pull_request:
branches:
- main

jobs:

find-configurations:
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Find configurations
id: find-targets
run: |
configures=$(git diff --name-only origin/main...HEAD -- configures | grep -e "\.json" | xargs -I{e} echo \"{e}\" | tr '\n' ',' | sed 's/,$//')
echo "configures=[${configures}]" >> ${GITHUB_OUTPUT}
outputs:
configures: ${{ steps.find-targets.outputs.configures }}

check-configurations:
needs:
- find-configurations

if: needs.find-configurations.outputs.configures != '[]'

strategy:
matrix:
configure: ${{ fromJson(needs.find-configurations.outputs.configures) }}

runs-on: ubuntu-latest

permissions:
contents: read

steps:
- uses: actions/checkout@v4
- name: Check the configuration file exists
run: test -f ${{ matrix.configure }} && [[ -s ${{ matrix.configure }} ]]
- name: Check the configuration file is valid JSON
run: jq . ${{ matrix.configure }} > /dev/null

check-all-test-passed:
if: always()

needs:
- check-configurations

permissions:
pull-requests: write

runs-on: ubuntu-latest

steps:
- run: exit 1
if: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: ':mega: All tests passed!'
})
42 changes: 42 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Build engine images and publish

on:
push:
branches:
- main

jobs:

find-configurations:
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Find configurations
id: find-targets
run: |
configures=$(git diff --name-only HEAD^...HEAD -- configures | grep -e "\.json" | xargs -I{e} echo \"{e}\" | tr '\n' ',' | sed 's/,$//')
echo "configures=[${configures}]" >> ${GITHUB_OUTPUT}
outputs:
configures: ${{ steps.find-targets.outputs.configures }}

build-and-publish:
needs:
- find-configurations

if: needs.find-configurations.outputs.configures != '[]'

strategy:
matrix:
configure: ${{ fromJson(needs.find-configurations.outputs.configures) }}
uses: ./.github/workflows/build.yaml
with:
configuration: ${{ matrix.configure }}
registry: ghcr.io
repo: kupl/kaprese-engines

0 comments on commit 8cae563

Please sign in to comment.