Skip to content

Build and Push Docker Image #21

Build and Push Docker Image

Build and Push Docker Image #21

name: Build and Push Docker Image
on:
schedule:
- cron: '0 0 * * 0' # Runs every Sunday at midnight
workflow_dispatch: # Allows manual trigger
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest snmpd version
id: get-version
run: |
latest_version=$(curl -s "https://sourceforge.net/projects/net-snmp/files/net-snmp/" | grep -Po 'href="\/projects\/net-snmp\/files\/net-snmp\/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -1)
echo "latest_version=${latest_version}" >> $GITHUB_ENV
major_version=$(echo $latest_version | cut -d'.' -f1)
minor_version=$(echo $latest_version | cut -d'.' -f1-2)
echo "major_version=${major_version}" >> $GITHUB_ENV
echo "minor_version=${minor_version}" >> $GITHUB_ENV
- name: Build Docker image
run: |
repo_name=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
docker build --build-arg SNMPD_VERSION=${{ env.latest_version }} -t ghcr.io/$repo_name/snmpd:${{ env.latest_version }} .
- name: Tag Docker image
run: |
repo_name=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
docker tag ghcr.io/$repo_name/snmpd:${{ env.latest_version }} ghcr.io/$repo_name/snmpd:latest
docker tag ghcr.io/$repo_name/snmpd:${{ env.latest_version }} ghcr.io/$repo_name/snmpd:${{ env.major_version }}
docker tag ghcr.io/$repo_name/snmpd:${{ env.latest_version }} ghcr.io/$repo_name/snmpd:${{ env.minor_version }}
- name: Push Docker image
run: |
repo_name=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
docker push ghcr.io/$repo_name/snmpd:${{ env.latest_version }}
docker push ghcr.io/$repo_name/snmpd:latest
docker push ghcr.io/$repo_name/snmpd:${{ env.major_version }}
docker push ghcr.io/$repo_name/snmpd:${{ env.minor_version }}
- name: Tag the repository with the latest version
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git tag ${latest_version}
git push origin ${latest_version}