Skip to content

Commit f48fab6

Browse files
authored
feat(ci): run build and push workflow on labelled pull requests, exclude prs from forks (#13)
Updated CI workflow to build images for Pull Requests
2 parents c90d118 + 97f4d72 commit f48fab6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

.github/workflows/build-and-push-images.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Go Demo -- Build and Push Container Image to GAR
22

33
on:
4+
pull_request:
5+
types: [labeled, unlabeled, synchronize]
46
push:
57
branches:
68
- main
@@ -16,6 +18,14 @@ env:
1618

1719
jobs:
1820
build-and-push:
21+
if: >
22+
github.event_name == 'push' ||
23+
github.event_name == 'workflow_dispatch' ||
24+
(
25+
github.event_name == 'pull_request' &&
26+
contains(github.event.pull_request.labels.*.name, 'preview') &&
27+
github.event.pull_request.head.repo.full_name == github.repository
28+
)
1929
runs-on: ubuntu-latest
2030
permissions:
2131
contents: read
@@ -25,6 +35,7 @@ jobs:
2535
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
2636
with:
2737
fetch-tags: true
38+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }}
2839
- uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
2940
- id: gcp-auth
3041
name: Google authentication
@@ -57,12 +68,14 @@ jobs:
5768
else
5869
tag="$(git rev-parse --short=10 HEAD)"
5970
fi
71+
6072
# append metadata if present
6173
if [[ -n "${{ env.METADATA }}" ]]; then
6274
tag="${tag}--${{ env.METADATA }}"
6375
fi
76+
6477
echo "Setting IMAGE_TAG=${tag} as output"
65-
echo "IMAGE_TAG=${tag}" >> "$GITHUB_OUTPUT"
78+
echo "IMAGE_TAG=${tag}" >> "$GITHUB_OUTPUT"
6679
- name: Docker meta
6780
id: meta
6881
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804
@@ -83,3 +96,4 @@ jobs:
8396
push: true
8497
cache-from: type=gha
8598
cache-to: type=gha,mode=max
99+

go-demo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
})
4444

4545
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
46-
w.Write([]byte("Welcome to the cicd demo web app! Visit /__version__ for version information."))
46+
w.Write([]byte("Welcome to the cicd demo go web app! Visit /__version__ for version information. Adding test string here"))
4747
})
4848

4949
port := "8000"

0 commit comments

Comments
 (0)