forked from cartsnitch/cartsnitch
9e23e39e5f
Co-Authored-By: Paperclip <noreply@paperclip.ing>
197 lines
6.0 KiB
YAML
197 lines
6.0 KiB
YAML
name: CI
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
concurrency:
|
|
group: ci-${{ github.ref }}
|
|
cancel-in-progress: true
|
|
|
|
permissions:
|
|
contents: write
|
|
packages: write
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
IMAGE_NAME: cartsnitch/cartsnitch
|
|
AUTH_IMAGE_NAME: cartsnitch/auth
|
|
|
|
jobs:
|
|
lint:
|
|
runs-on: runners-cartsnitch
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: "20"
|
|
cache: npm
|
|
- run: npm ci
|
|
- name: ESLint
|
|
run: npx eslint .
|
|
- name: Type check
|
|
run: npx tsc --noEmit
|
|
|
|
test:
|
|
runs-on: runners-cartsnitch
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: "20"
|
|
cache: npm
|
|
- run: npm ci
|
|
- name: Run tests
|
|
run: npx vitest run
|
|
|
|
build-and-push:
|
|
runs-on: runners-cartsnitch
|
|
needs: [lint, test]
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Generate CalVer tag
|
|
id: calver
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
run: |
|
|
DATE_TAG=$(date -u +%Y.%m.%d)
|
|
EXISTING=$(git tag -l "v${DATE_TAG}*" | sort -V | tail -1)
|
|
if [ -z "$EXISTING" ]; then
|
|
VERSION="$DATE_TAG"
|
|
elif [ "$EXISTING" = "v${DATE_TAG}" ]; then
|
|
VERSION="${DATE_TAG}.2"
|
|
else
|
|
BUILD_NUM=$(echo "$EXISTING" | sed "s/v${DATE_TAG}\.//")
|
|
VERSION="${DATE_TAG}.$((BUILD_NUM + 1))"
|
|
fi
|
|
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
|
|
echo "CalVer tag: $VERSION"
|
|
|
|
- name: Log in to Docker Hub
|
|
uses: docker/login-action@v3
|
|
with:
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
|
|
- name: Log in to GHCR
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Extract metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
tags: |
|
|
type=sha,prefix=sha-
|
|
type=raw,value=${{ steps.calver.outputs.version }},enable=${{ github.ref == 'refs/heads/main' }}
|
|
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
|
|
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
target: prod
|
|
|
|
- name: Create git tag
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
run: |
|
|
git tag "v${{ steps.calver.outputs.version }}"
|
|
git push origin "v${{ steps.calver.outputs.version }}"
|
|
|
|
build-and-push-auth:
|
|
runs-on: runners-cartsnitch
|
|
needs: [lint, test]
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Generate CalVer tag
|
|
id: calver
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
run: |
|
|
DATE_TAG=$(date -u +%Y.%m.%d)
|
|
EXISTING=$(git tag -l "v${DATE_TAG}*" | sort -V | tail -1)
|
|
if [ -z "$EXISTING" ]; then
|
|
VERSION="$DATE_TAG"
|
|
elif [ "$EXISTING" = "v${DATE_TAG}" ]; then
|
|
VERSION="${DATE_TAG}.2"
|
|
else
|
|
BUILD_NUM=$(echo "$EXISTING" | sed "s/v${DATE_TAG}\.//")
|
|
VERSION="${DATE_TAG}.$((BUILD_NUM + 1))"
|
|
fi
|
|
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Log in to GHCR
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Extract metadata (auth)
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.AUTH_IMAGE_NAME }}
|
|
tags: |
|
|
type=sha,prefix=sha-
|
|
type=raw,value=${{ steps.calver.outputs.version }},enable=${{ github.ref == 'refs/heads/main' }}
|
|
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
|
|
|
|
- name: Build and push auth Docker image
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: ./auth
|
|
file: ./auth/Dockerfile
|
|
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
|
|
deploy-dev:
|
|
runs-on: runners-cartsnitch
|
|
needs: [build-and-push, build-and-push-auth]
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
steps:
|
|
- name: Checkout infra repo
|
|
uses: actions/checkout@v4
|
|
with:
|
|
repository: cartsnitch/infra
|
|
path: infra
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Install kubectl
|
|
uses: azure/setup-kubectl@v4
|
|
|
|
- name: Install kustomize
|
|
uses: imranismail/setup-kustomize@v2
|
|
|
|
- name: Update dev overlay image tag
|
|
run: |
|
|
cd infra
|
|
kustomize edit set image \
|
|
ghcr.io/cartsnitch/cartsnitch=cartsnitch/cartsnitch:${{ github.sha }} \
|
|
ghcr.io/cartsnitch/auth=cartsnitch/auth:${{ github.sha }}
|
|
|
|
- name: Commit and push
|
|
run: |
|
|
cd infra
|
|
git config user.name "github-actions[bot]"
|
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
git add apps/overlays/dev/kustomization.yaml
|
|
git diff --staged --quiet || git commit -m "chore: update dev image tags to ${{ github.sha }}"
|
|
git push
|