Skip to content

Update app.py

Update app.py #19

Workflow file for this run

name: Build, Publish, and Update Manifest
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
packages: write # Grant write permissions for packages
env:
APP_NAME: "my-app" # Replace with your app name or pass it as an input
ENVIRONMENT: "production" # Set different values for different environments if needed
IMAGE_TAG: ${{ github.sha }} # Use the commit SHA as the image tag for versioning
jobs:
build-publish-and-setup:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set environment variables
run: |
REPO_OWNER=$(echo '${{ github.repository_owner }}' | awk '{print tolower($0)}')
echo "REPO_OWNER=$REPO_OWNER" >> $GITHUB_ENV
echo "REGISTRY=ghcr.io/$REPO_OWNER" >> $GITHUB_ENV
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Docker image
run: |
docker build -t $REGISTRY/$APP_NAME:${{ env.IMAGE_TAG }} .
- name: Push Docker image
run: |
docker push $REGISTRY/$APP_NAME:${{ env.IMAGE_TAG }}
- name: Clone manifests repository
run: |
git clone https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/TakeshiKovacs/git-ops.git manifests-repo
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- name: Set up new app manifests
run: |
# Define paths and new app folder
TEMPLATE_DIR="manifests-repo/kubernetes/_templates/app"
NEW_APP_DIR="manifests-repo/kubernetes/$APP_NAME"
# Copy the template directory to the new app folder
mkdir -p $NEW_APP_DIR
cp $TEMPLATE_DIR/* $NEW_APP_DIR
# Replace placeholders in deployment.yaml
sed -i "s|{{APP_NAME}}|$APP_NAME|g" $NEW_APP_DIR/deployment.yaml
sed -i "s|{{ENVIRONMENT}}|$ENVIRONMENT|g" $NEW_APP_DIR/deployment.yaml
sed -i "s|{{REGISTRY}}|$REGISTRY|g" $NEW_APP_DIR/deployment.yaml
sed -i "s|{{IMAGE_TAG}}|${{ env.IMAGE_TAG }}|g" $NEW_APP_DIR/deployment.yaml
# Replace placeholders in service.yaml
sed -i "s|{{APP_NAME}}|$APP_NAME|g" $NEW_APP_DIR/service.yaml
sed -i "s|{{ENVIRONMENT}}|$ENVIRONMENT|g" $NEW_APP_DIR/service.yaml
# (Optional) Log the changes for verification
echo "Generated manifests for $APP_NAME:"
cat $NEW_APP_DIR/deployment.yaml
cat $NEW_APP_DIR/service.yaml
cat $NEW_APP_DIR/kustomization.yaml
- name: Commit and Push Changes to Manifests Repo
run: |
cd manifests-repo
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add kubernetes/$APP_NAME
git commit -m "Add manifests for $APP_NAME based on template"
git push
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}