Skip to content

Commit 2e4240a

Browse files
authored
Improve GitHub actions (#23)
* Add branch tag(+untag) + stale issue * Add support for arm64 (#22 , thanks @satyapraneet63) * Added workflow_dispatch to docker publish
1 parent afeb3ea commit 2e4240a

File tree

3 files changed

+140
-40
lines changed

3 files changed

+140
-40
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Untag docker image on branch deletion
2+
3+
on:
4+
delete:
5+
branches:
6+
- '*'
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
untag-image:
14+
runs-on: ubuntu-latest
15+
16+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
17+
# (required)
18+
permissions:
19+
contents: read
20+
packages: write
21+
22+
steps:
23+
- name: Remove Docker tag
24+
uses: rafalkk/remove-dockertag-action@v1
25+
with:
26+
tag_name: ${{ github.event.ref }}
27+
github_token: ${{ secrets.GITHUB_TOKEN }}
28+
is_organization: false
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Close inactive issues
2+
on:
3+
schedule:
4+
- cron: "30 1 * * *"
5+
6+
jobs:
7+
close-issues:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
issues: write
11+
pull-requests: write
12+
steps:
13+
- uses: actions/stale@v5
14+
with:
15+
days-before-issue-stale: 30
16+
days-before-issue-close: 44
17+
stale-issue-label: "stale"
18+
stale-issue-message: "This issue has been open for 30 days with no activity. Marked as stale."
19+
close-issue-message: "This issue has been inactive for 14 days since being marked as stale. Closing."
20+
days-before-pr-stale: -1
21+
days-before-pr-close: -1
22+
repo-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/docker-publish.yml

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Docker
1+
name: Build and push Docker image
22

33
# This workflow uses actions that are not certified by GitHub.
44
# They are provided by a third-party and are governed by
@@ -7,75 +7,125 @@ name: Docker
77

88
on:
99
push:
10-
branches: [ "main" ]
11-
# Publish semver tags as releases.
12-
tags: [ 'v*.*.*' ]
13-
pull_request:
14-
branches: [ "main" ]
10+
branches: [ '*' ] # Trigger on push to any branch
11+
tags: [ 'v*.*.*' ] # Trigger on push to any tag matching the pattern
12+
workflow_dispatch:
13+
inputs:
14+
Force_build:
15+
description: 'Force build without checking files.'
16+
required: true
17+
default: 'warning'
18+
type: choice
19+
options:
20+
- true
21+
- false
1522

16-
env:
17-
# Use docker.io for Docker Hub if empty
18-
REGISTRY: ghcr.io
19-
# github.repository as <account>/<repo>
20-
IMAGE_NAME: ${{ github.repository }}
2123

24+
env:
25+
REGISTRY: ghcr.io # Use GitHub Container Registry
26+
IMAGE_NAME: ${{ github.repository }} # Set IMAGE_NAME to the repository name
27+
BRANCH_TAG: ${{ github.ref_name }} # Set BRANCH_TAG to the branch name
28+
FORCE: ${{ inputs.Force_build }}
2229

2330
jobs:
24-
build:
25-
31+
build_and_push:
2632
runs-on: ubuntu-latest
2733
permissions:
28-
contents: read
29-
packages: write
30-
# This is used to complete the identity challenge
31-
# with sigstore/fulcio when running outside of PRs.
32-
id-token: write
34+
contents: read # Read access to repository contents
35+
packages: write # Write access to packages
36+
# This is used to complete the identity challenge with sigstore/fulcio when running outside of PRs.
37+
id-token: write # Write access to id-token for identity challenge with sigstore/fulcio
3338

3439
steps:
35-
- name: Checkout repository
36-
uses: actions/checkout@v3
40+
- name: Checkout repository.
41+
uses: actions/checkout@v3 # Checkout the repository
42+
43+
# List modified files : If only files present in .dockerignore have changed, the workflow will not run.
44+
- name: Compare modified files to .dockerignore.
45+
id: changes
46+
run: |
47+
git fetch --unshallow
48+
last_commit=$(git rev-parse HEAD)
49+
is_merge_commit=$(git log -1 --pretty=%P "${last_commit}" | wc -w)
50+
if [ ${is_merge_commit} -gt 1 ]; then
51+
parent_commits=$(git log -1 --pretty=%P "${last_commit}")
52+
changed_files=$(git diff-tree --no-commit-id --name-only -r ${parent_commits})
53+
else
54+
changed_files=$(git diff-tree --no-commit-id --name-only -r "${last_commit}")
55+
fi
56+
echo "Changed files : ${changed_files}"
57+
echo "Force build : ${FORCE:-false}"
58+
if [ "${FORCE}" = "true" ]; then
59+
continue="true"
60+
else
61+
ignore_patterns=$(grep -v '^#' .dockerignore | grep -v '^$')
62+
continue="false"
63+
# Check if any of the changed files is not ignored in .dockerignore.
64+
for file in ${changed_files}; do
65+
matched="false"
66+
for pattern in ${ignore_patterns}; do
67+
if echo "${file}" | grep -qE "^${pattern}(/|$)"; then
68+
matched="true"
69+
break
70+
fi
71+
done
72+
if [ "${matched}" = "false" ]; then
73+
continue="true"
74+
break
75+
fi
76+
done
77+
fi
78+
echo "continue=${continue}" >> ${GITHUB_ENV}
79+
echo "continue=${continue}"
3780
38-
# Install the cosign tool except on PR
81+
# Install the cosign tool
3982
# https://github.com/sigstore/cosign-installer
40-
- name: Install cosign
41-
if: github.event_name != 'pull_request'
42-
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
83+
- name: Install cosign.
84+
if: env.continue == 'true'
85+
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1 # Install cosign tool except on pull requests
4386
with:
44-
cosign-release: 'v2.1.1'
45-
87+
cosign-release: 'v2.1.1' # Specify cosign version
88+
4689
# Set up BuildKit Docker container builder to be able to build
4790
# multi-platform images and export cache
4891
# https://github.com/docker/setup-buildx-action
49-
- name: Set up Docker Buildx
50-
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
92+
- name: Set up Docker Buildx.
93+
if: env.continue == 'true'
94+
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 # Set up Docker Buildx for building multi-platform images
5195

52-
# Login against a Docker registry except on PR
96+
# Login against a Docker registry
5397
# https://github.com/docker/login-action
54-
- name: Log into registry ${{ env.REGISTRY }}
55-
if: github.event_name != 'pull_request'
56-
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
98+
- name: Log into registry ${{ env.REGISTRY }}.
99+
if: env.continue == 'true'
100+
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 # Log into Docker registry except on pull requests
57101
with:
58102
registry: ${{ env.REGISTRY }}
59103
username: ${{ github.actor }}
60104
password: ${{ secrets.GITHUB_TOKEN }}
61-
105+
62106
# Extract metadata (tags, labels) for Docker
63107
# https://github.com/docker/metadata-action
64-
- name: Extract Docker metadata
108+
- name: Extract Docker metadata.
109+
if: env.continue == 'true'
65110
id: meta
66-
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
111+
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 # Extract metadata for Docker images
67112
with:
68113
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
114+
tags: |
115+
${{ github.ref == 'refs/heads/main' && 'latest' || '' }}
116+
${{ env.BRANCH_TAG }}
69117
70-
# Build and push Docker image with Buildx (don't push on PR)
118+
# Build and push Docker image with Buildx
71119
# https://github.com/docker/build-push-action
72-
- name: Build and push Docker image
120+
- name: Build and push Docker image.
121+
if: env.continue == 'true'
73122
id: build-and-push
74-
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
123+
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 # Build and push Docker image with Buildx
75124
with:
76125
context: .
77126
push: ${{ github.event_name != 'pull_request' }}
78127
tags: ${{ steps.meta.outputs.tags }}
79128
labels: ${{ steps.meta.outputs.labels }}
80-
cache-from: type=gha
81-
cache-to: type=gha,mode=max
129+
cache-from: type=gha # Use GitHub Actions cache for Docker layers
130+
platforms: linux/amd64,linux/arm64
131+
cache-to: type=gha,mode=max # Use GitHub Actions cache for Docker layers

0 commit comments

Comments
 (0)