Implementation of Github Actions Workflow for Docker Image. #1
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Docker Build and Push | |
on: | |
schedule: | |
- cron: '13 13 * * *' | |
push: | |
branches: [ "master" ] | |
pull_request: | |
branches: [ "master" ] | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Get latest commit ID from nginx/nginx | |
id: nginx_commit | |
run: | | |
LATEST_COMMIT=$(curl -s https://api.github.com/repos/nginx/nginx/commits/master | jq -r '.sha') | |
echo "Latest commit in nginx/nginx is $LATEST_COMMIT" | |
echo "::set-output name=commit_id::$LATEST_COMMIT" | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.DOCKER_TOKEN }} | |
- name: Pull the latest image for comparison | |
id: pull_latest_image | |
run: | | |
docker pull ghcr.io/nginx/nginx-quic-qns:latest || true | |
- name: Get the commit ID tag of the latest image | |
id: latest_image_commit | |
run: | | |
LATEST_IMAGE_COMMIT=$(docker inspect ghcr.io/nginx/nginx-quic-qns:latest --format '{{ index .Config.Labels "commit_id" }}' 2> /dev/null || echo "none") | |
echo "Latest image commit ID is $LATEST_IMAGE_COMMIT" | |
echo "::set-output name=latest_image_commit::$LATEST_IMAGE_COMMIT" | |
- name: Compare commit IDs | |
id: compare_commits | |
run: | | |
if [ "${{ steps.nginx_commit.outputs.commit_id }}" != "${{ steps.latest_image_commit.outputs.latest_image_commit }}" ]; then | |
echo "Commit IDs are different. Triggering build." | |
echo "::set-output name=trigger_build::true" | |
else | |
echo "Commit IDs are the same. No build needed." | |
echo "::set-output name=trigger_build::false" | |
fi | |
- name: Build and Push Docker Image | |
if: steps.compare_commits.outputs.trigger_build == 'true' | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: Dockerfile | |
push: true | |
tags: | | |
ghcr.io/nginx/nginx-quic-qns:latest | |
ghcr.io/nginx/nginx-quic-qns:${{ steps.nginx_commit.outputs.commit_id }} | |
labels: | | |
commit_id=${{ steps.nginx_commit.outputs.commit_id }} |