|
| 1 | +name: Docker Build and Push |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '13 13 * * *' |
| 6 | + push: |
| 7 | + branches: [ "master" ] |
| 8 | + pull_request: |
| 9 | + branches: [ "master" ] |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Get latest commit ID from nginx/nginx |
| 20 | + run: | |
| 21 | + LATEST_COMMIT=$(curl -s https://api.github.com/repos/nginx/nginx/commits/master | jq -r '.sha') |
| 22 | + echo "commit_id=$LATEST_COMMIT" >> $GITHUB_ENV |
| 23 | +
|
| 24 | + - name: Install Skopeo |
| 25 | + run: | |
| 26 | + sudo apt-get update |
| 27 | + sudo apt-get install -y skopeo |
| 28 | +
|
| 29 | + - name: Get the commit ID tag of the latest image with Skopeo |
| 30 | + run: | |
| 31 | + LATEST_IMAGE_COMMIT=$(skopeo inspect docker://ghcr.io/nginx/nginx-quic-qns:latest | jq -r '.Labels.commit_id' || echo "none") |
| 32 | + echo "latest_image_commit=$LATEST_IMAGE_COMMIT" >> $GITHUB_ENV |
| 33 | +
|
| 34 | + - name: Compare commit IDs |
| 35 | + run: | |
| 36 | + if [ "${{ env.commit_id }}" != "${{ env.latest_image_commit }}" ]; then |
| 37 | + echo "Commit IDs are different. Triggering build." |
| 38 | + echo "trigger_build=true" >> $GITHUB_ENV |
| 39 | + else |
| 40 | + echo "Commit IDs are the same. No build needed." |
| 41 | + echo "trigger_build=false" >> $GITHUB_ENV |
| 42 | + fi |
| 43 | +
|
| 44 | + - name: Login to GitHub Container Registry |
| 45 | + if: env.trigger_build == 'true' |
| 46 | + uses: docker/login-action@v3 |
| 47 | + with: |
| 48 | + registry: ghcr.io |
| 49 | + username: ${{ github.actor }} |
| 50 | + password: ${{ secrets.DOCKER_TOKEN }} |
| 51 | + |
| 52 | + - name: Build and Push Docker Image |
| 53 | + if: env.trigger_build == 'true' |
| 54 | + uses: docker/build-push-action@v5 |
| 55 | + with: |
| 56 | + context: . |
| 57 | + file: Dockerfile |
| 58 | + push: true |
| 59 | + tags: | |
| 60 | + ghcr.io/nginx/nginx-quic-qns:latest |
| 61 | + ghcr.io/nginx/nginx-quic-qns:${{ env.commit_id }} |
| 62 | + labels: | |
| 63 | + commit_id=${{ env.commit_id }} |
0 commit comments