Skip to content

Merge pull request #273 from jonfairbanks/fixes/clean-up #8

Merge pull request #273 from jonfairbanks/fixes/clean-up

Merge pull request #273 from jonfairbanks/fixes/clean-up #8

name: "CI - Development"
on:
push:
branches:
- "develop"
env:
TERM: 'xterm'
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
jobs:
configure:
name: Configure
runs-on: ubuntu-latest
steps:
- name: Setup deployment variables
id: setup-vars
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "Branch: $BRANCH_NAME"
if [ "$BRANCH_NAME" = "develop" ]; then
echo "Using Development"
echo "environment=development" >> $GITHUB_OUTPUT
echo "aws_key=${{ secrets.aws_access_key_id }}" >> $GITHUB_OUTPUT
echo "aws_secret=${{ secrets.aws_secret_access_key }}" >> $GITHUB_OUTPUT
elif [ "$BRANCH_NAME" = "main" ]; then
echo "Using Production"
echo "environment=production" >> $GITHUB_OUTPUT
echo "aws_key=${{ secrets.aws_access_key_id }}" >> $GITHUB_OUTPUT
echo "aws_secret=${{ secrets.aws_secret_access_key }}" >> $GITHUB_OUTPUT
fi
node-lint:
name: Lint Node.js
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- name: Install dependencies
run: npm ci
working-directory: src
- name: Lint Code
run: npm run lint
working-directory: src
node-audit:
name: Critical Vulnerability Check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- name: Install dependencies
run: npm ci
working-directory: src
- name: Check for critical vulnerabilities
run: npm audit --audit-level=critical
working-directory: src
docker-build:
name: Docker Build
environment: development
needs: [node-lint, node-audit]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Define sha_short
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
- name: Set up QEMU
id: setup-qemu
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
# - name: Cache Docker layers
# uses: actions/cache@v4
# with:
# path: /tmp/.buildx-cache/${{ runner.os }}-${{ steps.setup-buildx.outputs.name }}-${{ hashFiles('**/Dockerfile') }}
# key: ${{ runner.os }}-buildx-${{ steps.vars.outputs.sha_short }}-{{ hashFiles('**/Dockerfile') }}
# restore-keys: |
# ${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.aws_access_key_id }}
aws-secret-access-key: ${{ secrets.aws_secret_access_key }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: ecr-login
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: 'true'
- name: Get Repository Name
id: repo-name
run: echo "REPO_NAME=$(basename $GITHUB_REPOSITORY)" >> $GITHUB_ENV
- name: Create ECR Repository if not exists
env:
AWS_REGION: us-east-1
REPO_NAME: ${{ env.REPO_NAME }}
run: |
aws ecr describe-repositories --repository-names $REPO_NAME --region $AWS_REGION || \
aws ecr create-repository --repository-name $REPO_NAME --region $AWS_REGION
- name: Build & Push Docker image(s)
id: docker-build
uses: docker/build-push-action@v6
with:
builder: ${{ steps.buildx.outputs.name }}
context: ./src
platforms: linux/amd64 # TODO: Re-add `linux/arm64`
file: ./src/Dockerfile
push: true
provenance: false
tags: |
${{ github.repository }}:develop
${{ steps.ecr-login.outputs.registry }}/jonfairbanks/yo-api:develop
# cache-from: type=local,src=/tmp/.buildx-cache
# cache-to: type=local,dest=/tmp/.buildx-cache
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Notify Slack
uses: act10ns/slack@v2
with:
status: ${{ job.status }}
steps: ${{ toJson(steps) }}
if: always()