Skip to content

Build and Push Docker Image #54

Build and Push Docker Image

Build and Push Docker Image #54

name: Build and Push Docker Image
on:
schedule:
# Runs at 00:00 UTC on the 1st and 15th of every month
- cron: '0 0 1,15 * *'
workflow_dispatch:
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write # Changed from 'read' to allow dummy commits
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set lowercase owner
run: echo "OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: |
ghcr.io/${{ env.OWNER }}/github-runner:latest
ghcr.io/${{ env.OWNER }}/github-runner:${{ github.sha }}
- name: Send Success Slack notification
if: success()
uses: slackapi/slack-github-action@v1.24.0
with:
channel-id: 'C0830SADR0X' #devops-github-actions Slack Channel ID
payload: |
{
"text": "Docker Image Build Success",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "✅ *Docker Image Build Successful*\n*Repository:* ${{ github.repository }}\n*Image:* ${{ env.REGISTRY }}/${{ env.REPO }}:${{ github.sha }}\n<!channel>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
- name: Send Failure Slack notification
if: failure()
uses: slackapi/slack-github-action@v1.24.0
with:
channel-id: 'C0830SADR0X' #devops-github-actions Slack Channel ID
payload: |
{
"text": "Docker Image Build Failed",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "❌ *Docker Image Build Failed*\n*Repository:* ${{ github.repository }}\n*Workflow:* ${{ github.workflow }}\n*Error:* Build and push operation failed\n\n*Check the logs:* ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n<!channel>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
- name: Keep workflow active
if: always() && github.event_name == 'schedule'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
echo "Last workflow run: $(date -u)" > .github/.workflow-keep-alive
git add .github/.workflow-keep-alive
git diff --staged --quiet || git commit -m "chore: keep workflow active [skip ci]"
git push