Add CI/CD for auto-deployment of blog
All checks were successful
Build and Push Docker Image / build (push) Successful in 3m23s
All checks were successful
Build and Push Docker Image / build (push) Successful in 3m23s
This commit is contained in:
60
.gitea/workflows/build.yml
Normal file
60
.gitea/workflows/build.yml
Normal file
@@ -0,0 +1,60 @@
|
||||
name: Build and Push Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
env:
|
||||
REGISTRY: git.cbraaten.dev
|
||||
IMAGE_NAME: git.cbraaten.dev/caleb/blog
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Get short SHA
|
||||
id: sha
|
||||
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Print Secrets
|
||||
run: |
|
||||
if [ -z "${{ secrets.REGISTRY_USERNAME }}" ]; then
|
||||
echo "ERROR: REGISTRY_USERNAME secret is not set"
|
||||
exit 1
|
||||
else
|
||||
echo "✓ REGISTRY_USERNAME is set"
|
||||
fi
|
||||
if [ -z "${{ secrets.REGISTRY_PASSWORD }}" ]; then
|
||||
echo "ERROR: REGISTRY_PASSWORD secret is not set"
|
||||
exit 1
|
||||
else
|
||||
echo "✓ REGISTRY_PASSWORD is set"
|
||||
fi
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.REGISTRY_USERNAME }}
|
||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
|
||||
- name: Build and Push Docker Image
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: |
|
||||
${{ env.IMAGE_NAME }}:latest
|
||||
${{ env.IMAGE_NAME }}:${{ steps.sha.outputs.short_sha }}
|
||||
46
.gitea/workflows/deploy.yml
Normal file
46
.gitea/workflows/deploy.yml
Normal file
@@ -0,0 +1,46 @@
|
||||
name: Deploy to Nomad
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Build and Push Docker Image"]
|
||||
types:
|
||||
- completed
|
||||
|
||||
env:
|
||||
NOMAD_ADDR: ${{ secrets.NOMAD_ADDR }}
|
||||
NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Get image tag from git SHA
|
||||
id: tag
|
||||
run: |
|
||||
image_tag=$(git rev-parse --short HEAD)
|
||||
echo "image_tag=$image_tag" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Update image tag in job definition
|
||||
run: |
|
||||
# Replace the image tag placeholder with actual tag
|
||||
jq '.Job.TaskGroups[0].Tasks[0].Config.image |= sub(":.*"; ":${{ steps.tag.outputs.image_tag }}")' \
|
||||
blog.nomad.json > blog.nomad.final.json
|
||||
|
||||
- name: Submit job via Nomad API
|
||||
run: |
|
||||
curl -s -X POST \
|
||||
-H "X-Nomad-Token: ${{ env.NOMAD_TOKEN }}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @blog.nomad.final.json \
|
||||
"${{ env.NOMAD_ADDR }}/v1/jobs"
|
||||
|
||||
- name: Verify deployment
|
||||
run: |
|
||||
echo "Waiting for deployment to stabilize..."
|
||||
sleep 5
|
||||
curl -X GET \
|
||||
-H "X-Nomad-Token: ${{ env.NOMAD_TOKEN }}" \
|
||||
"${{ env.NOMAD_ADDR }}/v1/job/blog"
|
||||
Reference in New Issue
Block a user