Skip to content

Commit 797cbbc

Browse files
JasragsCopilot
andauthored
Modular GitHub actions and rework of workflows (#397)
* Base rework of github actions, WIP * Added actions/checkout@v4 before other steps * Added actions/checkout@v4 before other steps * Added permissions block * Added pull-requests: read to test workflow * Added actions/checkout@v4 before other steps * Testing auto-tag * Testing auto-tag * Testing auto-tag #patch * Testing auto-tag #patch * Cleaning up workflow files * Updating notify discord * Update branch triggers for auto-tag * Break out to use the discord-notify action only on PR open and refactor run-tests to only run tests * removed unused notification code * Update .github/workflows/auto-tag.yml Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent cd220f7 commit 797cbbc

File tree

8 files changed

+135
-89
lines changed

8 files changed

+135
-89
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: "Codegen and Test"
2+
description: "Run go generate and go test"
3+
runs:
4+
using: "composite"
5+
steps:
6+
- run: go generate ./...
7+
shell: bash
8+
- run: go test ./...
9+
shell: bash
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "Discord Webhook"
2+
description: "Send PR info to Discord"
3+
inputs:
4+
webhook-url:
5+
description: "Discord webhook URL"
6+
required: true
7+
pr-title:
8+
description: "Title of the pull request"
9+
required: false
10+
pr-body:
11+
description: "Body of the pull request"
12+
required: false
13+
pr-url:
14+
description: "URL of the pull request"
15+
required: false
16+
runs:
17+
using: "composite"
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: tsickert/[email protected]
21+
with:
22+
webhook-url: ${{ inputs.webhook-url }}
23+
embed-title: ${{ inputs.pr-title }}
24+
embed-description: ${{ inputs.pr-body }}
25+
embed-url: ${{ inputs.pr-url }}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: "Setup Go"
2+
description: "Checkout and setup Go using go.mod"
3+
runs:
4+
using: "composite"
5+
steps:
6+
- uses: actions/checkout@v4
7+
- uses: actions/setup-go@v5
8+
with:
9+
go-version-file: 'go.mod'

.github/workflows/auto-tag.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Auto Tag
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- '**'
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
tag:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0 # Needed to access all tags
19+
20+
- name: Create tag using commit message
21+
id: tagger
22+
uses: mathieudutour/[email protected]
23+
with:
24+
tag_prefix: 'v'
25+
default_bump: 'patch'
26+
default_prerelease_bump: 'prerelease'
27+
dry_run: false # Set to false for actual tagging in production
28+
github_token: ${{ secrets.GITHUB_TOKEN }}
29+
Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# This workflow will: build gomud for multiple os/architectures
2-
# archive the binaries and create a new release for users to easily download
3-
41
name: Build and release
52

63
on:
74
push:
8-
tags: ['v*.*.*']
5+
branches:
6+
- master
97

108
permissions:
119
contents: write
@@ -18,33 +16,15 @@ jobs:
1816
test:
1917
runs-on: ubuntu-latest
2018
steps:
21-
- name: Checkout repository
22-
uses: actions/checkout@v4
23-
24-
- name: Show version
25-
run: echo 'Releasing version $RELEASE_VERSION'
26-
27-
- name: Set up Go
28-
uses: actions/setup-go@v5
29-
with:
30-
go-version-file: 'go.mod'
31-
32-
- name: Run code generation
33-
run: go generate ./...
34-
35-
- name: Run tests
36-
run: go test ./...
19+
- uses: actions/checkout@v4
20+
- uses: ./.github/actions/setup-go
21+
- uses: ./.github/actions/codegen-and-test
3722

3823
build:
3924
runs-on: ubuntu-latest
40-
needs: 'test'
25+
needs: test
4126
steps:
42-
- uses: actions/checkout@v4
43-
44-
- name: Set up Go
45-
uses: actions/setup-go@v5
46-
with:
47-
go-version-file: 'go.mod'
27+
- uses: ./.github/actions/setup-go
4828

4929
- name: Create bin directory
5030
run: mkdir -p bin/
@@ -75,7 +55,7 @@ jobs:
7555

7656
release:
7757
runs-on: ubuntu-latest
78-
needs: "build"
58+
needs: build
7959
steps:
8060
- uses: actions/checkout@v4
8161

@@ -88,8 +68,7 @@ jobs:
8868
- name: Set short git commit SHA
8969
id: vars
9070
run: |
91-
calculatedSha=$(git rev-parse --short ${{ github.sha }})
92-
echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
71+
echo "COMMIT_SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
9372
9473
- name: Archive release
9574
run: zip -r bin/${{ env.RELEASE_FILENAME }}-${{ env.RELEASE_VERSION }}.zip bin/
@@ -102,26 +81,3 @@ jobs:
10281
fail_on_unmatched_files: true
10382
env:
10483
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105-
106-
message:
107-
runs-on: ubuntu-latest
108-
steps:
109-
- uses: actions/github-script@v6
110-
id: get_pr_data
111-
with:
112-
script: |
113-
return (
114-
await github.rest.repos.listPullRequestsAssociatedWithCommit({
115-
commit_sha: context.sha,
116-
owner: context.repo.owner,
117-
repo: context.repo.repo,
118-
})
119-
).data[0];
120-
- name: Discord Webhook Action
121-
uses: tsickert/[email protected]
122-
with:
123-
webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
124-
embed-title: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).title || '🎉 New update on `master` branch' }}
125-
embed-description: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).body || 'No description provided.' }}
126-
embed-url: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).html_url || github.event.compare }}
127-
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Notify Discord
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
notify-discord:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Get PR Metadata
16+
if: github.event_name == 'pull_request'
17+
uses: actions/github-script@v6
18+
id: pr_meta
19+
with:
20+
script: |
21+
core.setOutput('title', context.payload.pull_request.title || '')
22+
core.setOutput('body', context.payload.pull_request.body || '')
23+
core.setOutput('url', context.payload.pull_request.html_url || '')
24+
25+
- name: Get Release Metadata
26+
if: github.event_name == 'release'
27+
uses: actions/github-script@v6
28+
id: release_meta
29+
with:
30+
script: |
31+
core.setOutput('title', context.payload.release.name || context.payload.release.tag_name || '')
32+
core.setOutput('body', context.payload.release.body || '')
33+
core.setOutput('url', context.payload.release.html_url || '')
34+
35+
- name: Send Discord Message
36+
uses: ./.github/actions/discord-webhook
37+
with:
38+
webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
39+
pr-title: ${{ steps.pr_meta.outputs.title }}${{ steps.release_meta.outputs.title }}
40+
pr-body: ${{ steps.pr_meta.outputs.body }}${{ steps.release_meta.outputs.body }}
41+
pr-url: ${{ steps.pr_meta.outputs.url }}${{ steps.release_meta.outputs.url }}
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,50 @@
1-
# https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images
2-
31
name: Docker Package
42

53
on:
64
workflow_dispatch:
75
push:
86
branches:
97
- master
10-
tags:
11-
- 'v*.*.*'
128
pull_request:
139
branches:
1410
- master
1511

1612
env:
17-
# Use docker.io for Docker Hub if empty
1813
REGISTRY: ghcr.io
19-
# github.repository as <account>/<repo>
2014
IMAGE_NAME: ${{ github.repository }}
2115
RELEASE_VERSION: ${{ github.ref_name }}
2216

2317
jobs:
2418
package:
2519
runs-on: ubuntu-latest
26-
27-
# Sets the permissions granted to the GITHUB_TOKEN for the actions in this job.
2820
permissions:
2921
contents: read
3022
packages: write
3123
attestations: write
3224
id-token: write
33-
3425
steps:
35-
- name: Checkout repository
36-
uses: actions/checkout@v4
26+
- uses: actions/checkout@v4
27+
- uses: ./.github/actions/setup-go
3728

38-
# https://github.com/docker/login-action
3929
- name: Log in to the Container registry ${{ env.REGISTRY }}
4030
if: github.event_name != 'pull_request'
4131
uses: docker/login-action@v3
4232
with:
4333
registry: ${{ env.REGISTRY }}
4434
username: ${{ github.actor }}
45-
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication
4635
password: ${{ secrets.GITHUB_TOKEN }}
4736

48-
# https://github.com/docker/metadata-action
4937
- name: Extract metadata (tags, labels) for Docker
5038
id: meta
5139
uses: docker/metadata-action@v5
5240
with:
5341
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
54-
# generate Docker tags based on the following events/attributes
5542
tags: |
56-
# set latest tag for master branch
5743
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
5844
type=ref,event=tag
5945
type=ref,event=pr
6046
type=sha
6147
62-
# https://github.com/docker/build-push-action
63-
# For pull request, only ensures that the docker build succeeds, does not push the image.
64-
# See: https://github.com/docker/build-push-action/issues/751
6548
- name: Build and push Docker image
6649
id: push
6750
uses: docker/build-push-action@v6
@@ -72,12 +55,10 @@ jobs:
7255
tags: ${{ steps.meta.outputs.tags }}
7356
labels: ${{ steps.meta.outputs.labels }}
7457

75-
# https://github.com/actions/attest-build-provenance
7658
- name: Generate artifact attestation
7759
if: github.event_name != 'pull_request'
7860
uses: actions/attest-build-provenance@v2
7961
with:
80-
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
62+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
8163
subject-digest: ${{ steps.push.outputs.digest }}
82-
# https://github.com/actions/attest-build-provenance/issues/71#issuecomment-2108140285
8364
push-to-registry: false

.github/workflows/run-tests.yml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
name: run-tests
1+
name: Run Tests
22

33
on:
44
pull_request:
5-
branches: [master]
6-
workflow_dispatch:
5+
branches:
6+
- master
7+
- '**'
8+
9+
permissions:
10+
contents: read
11+
pull-requests: read
712

813
jobs:
914
test:
1015
runs-on: ubuntu-latest
1116
steps:
1217
- uses: actions/checkout@v4
13-
14-
- name: Set up Go
15-
uses: actions/setup-go@v5
16-
with:
17-
go-version-file: 'go.mod'
18-
19-
- name: Run code generation
20-
run: go generate ./...
21-
22-
- name: Run tests
23-
run: make test
18+
- uses: ./.github/actions/setup-go
19+
- uses: ./.github/actions/codegen-and-test

0 commit comments

Comments
 (0)