Skip to content

Commit

Permalink
setup workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanBulmer committed Nov 9, 2023
0 parents commit 1b28233
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/docker-build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Build and Publish Docker Image

on:
workflow_dispatch:
workflow_call:
secrets:
GITHUB_TOKEN:
required: true

jobs:
# define job to build and publish docker image
build-and-publish:
name: Build and Publish Docker image
# run only when code is compiling and tests are passing
runs-on: ubuntu-latest

# steps to perform in job
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
# list of Docker images to use as base name for tags
images: |
ghcr.io/${{ github.repository }}
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
type=raw,value=latest,enable={{is_default_branch}}
# setup Docker buld action
- name: Set up Docker Buildx
id: docker_build
uses: docker/setup-buildx-action@v2

# - name: Login to DockerHub
# uses: docker/login-action@v2
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to Github Packages
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Publish to GitHub Container Registry
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ github.event.release.tag_name }}
RELEASE_TIME=${{ github.event.release.created_at }}
GIT_REPO=${{ github.repository }}
GIT_COMMIT_SHA=${{ github.sha }}
GIT_COMMIT_TIME=${{ github.event.push.head_commit.timestamp }}
GIT_REF=${{ github.ref_name }}
GIT_WORKFLOW_SHA=${{ github.workflow_sha }}
cache-from: type=registry,ref=${{ github.ref_name }}
cache-to: type=inline

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
22 changes: 22 additions & 0 deletions .github/workflows/node-build-artifact.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build NodeJS Artifact

on:
workflow_dispatch:
workflow_call:

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
cache: "yarn"
- run: yarn
- run: yarn build
- uses: actions/upload-artifact@v3
with:
name: dist
path: dist/
31 changes: 31 additions & 0 deletions .github/workflows/node-publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish Package to NPM

on:
workflow_dispatch:
workflow_call:
secrets:
NPM_ACCESS_TOKEN:
required: true

jobs:
publish-npm:
if: github.event_name == 'release' && github.event.action == 'created'
name: Publish to NPM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
cache: "yarn"
registry-url: https://registry.npmjs.org
# Defaults to the user or organization that owns the workflow file
scope: "@codrjs"
- run: yarn
- uses: actions/download-artifact@v3
with:
name: dist
path: dist/
- run: cd dist && yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_ACCESS_TOKEN }}
22 changes: 22 additions & 0 deletions .github/workflows/node-test-artifact.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test NodeJS Artifact

on:
workflow_dispatch:
workflow_call:

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
cache: "yarn"
- uses: actions/download-artifact@v3
with:
name: dist
path: dist/
- run: yarn
- run: yarn test
36 changes: 36 additions & 0 deletions .github/workflows/node-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test NodeJS Code

on:
workflow_dispatch:
workflow_call:
secrets:
MONGO_URI:
required: true
JWT_SECRET:
required: true
inputs:
JWT_ALGORITHM:
required: true
type: string
JWT_ISSUER:
required: true
type: string

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"
cache: "yarn"
- run: yarn
- run: yarn build
- env:
MONGO_URI: ${{ secrets.MONGO_URI }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
JWT_ALGORITHM: ${{ inputs.JWT_ALGORITHM }}
JWT_ISSUER: ${{ inputs.JWT_ISSUER }}
run: yarn test
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Codr CI/CD

This repository contains reusable CI/CD workflows to be reused throughout the rest of Codr's code respositories.

Please refer to the `.github/workflow-templates/` directory for examples of how the pre-built workflow files. These workflows can be called by any Codr repository and should be used to reduce code duplication and excessive maintenance keeping all workflows the same across dozens of repos.

## Workflows

`node-test.yml` - This workflow builds and tests the nodejs code without an artifact. It's designed for testing the microservice code and requires some secrets and environment variables in order to work.

`node-build-artifact` - Builds the code and uploads the artifact to be reused in future steps.

`node-test-artifact` - Pulls the built code artifact and runs unit tests against it.

`node-publish-npm` - Pulls the built code artifact and publishes it to the npm registry.

`docker-build-and-publish` - Builds and publishes a microservice docker image to the `ghcr.io` registry.

## Example Usage

```yaml
name: Build and Publish NPM Module

on:
workflow_dispatch:
push:
branches:
- "**"
pull_request:
branches:
- $default-branch
release:
types:
- created

jobs:
build:
uses: CodrJS/cicd/.github/workflows/node-build-artifact.yml@main
test:
uses: CodrJS/cicd/.github/workflows/node-test-artifact.yml@main
needs: build
publish:
uses: CodrJS/cicd/.github/workflows/node-publish-npm.yml@main
needs: test
secrets: inherit

```

0 comments on commit 1b28233

Please sign in to comment.