Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cicd): setup CI/CD #1

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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