diff --git a/.github/actions/codegen-and-test/action.yml b/.github/actions/codegen-and-test/action.yml new file mode 100644 index 00000000..8edf58f5 --- /dev/null +++ b/.github/actions/codegen-and-test/action.yml @@ -0,0 +1,9 @@ +name: "Codegen and Test" +description: "Run go generate and go test" +runs: + using: "composite" + steps: + - run: go generate ./... + shell: bash + - run: go test ./... + shell: bash diff --git a/.github/actions/discord-webhook/action.yml b/.github/actions/discord-webhook/action.yml new file mode 100644 index 00000000..bebdde7e --- /dev/null +++ b/.github/actions/discord-webhook/action.yml @@ -0,0 +1,25 @@ +name: "Discord Webhook" +description: "Send PR info to Discord" +inputs: + webhook-url: + description: "Discord webhook URL" + required: true + pr-title: + description: "Title of the pull request" + required: false + pr-body: + description: "Body of the pull request" + required: false + pr-url: + description: "URL of the pull request" + required: false +runs: + using: "composite" + steps: + - uses: actions/checkout@v4 + - uses: tsickert/discord-webhook@v7.0.0 + with: + webhook-url: ${{ inputs.webhook-url }} + embed-title: ${{ inputs.pr-title }} + embed-description: ${{ inputs.pr-body }} + embed-url: ${{ inputs.pr-url }} diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.yml new file mode 100644 index 00000000..57252aea --- /dev/null +++ b/.github/actions/setup-go/action.yml @@ -0,0 +1,9 @@ +name: "Setup Go" +description: "Checkout and setup Go using go.mod" +runs: + using: "composite" + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml new file mode 100644 index 00000000..5dcc029f --- /dev/null +++ b/.github/workflows/auto-tag.yml @@ -0,0 +1,29 @@ +name: Auto Tag + +on: + push: + branches: + - master + - '**' + +permissions: + contents: write + +jobs: + tag: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Needed to access all tags + + - name: Create tag using commit message + id: tagger + uses: mathieudutour/github-tag-action@v6.1 + with: + tag_prefix: 'v' + default_bump: 'patch' + default_prerelease_bump: 'prerelease' + dry_run: false # Set to false for actual tagging in production + github_token: ${{ secrets.GITHUB_TOKEN }} + diff --git a/.github/workflows/build-and-release.yml b/.github/workflows/build-and-release.yml index 4164fbef..777214e7 100644 --- a/.github/workflows/build-and-release.yml +++ b/.github/workflows/build-and-release.yml @@ -1,11 +1,9 @@ -# This workflow will: build gomud for multiple os/architectures -# archive the binaries and create a new release for users to easily download - name: Build and release on: push: - tags: ['v*.*.*'] + branches: + - master permissions: contents: write @@ -18,33 +16,15 @@ jobs: test: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Show version - run: echo 'Releasing version $RELEASE_VERSION' - - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' - - - name: Run code generation - run: go generate ./... - - - name: Run tests - run: go test ./... + - uses: actions/checkout@v4 + - uses: ./.github/actions/setup-go + - uses: ./.github/actions/codegen-and-test build: runs-on: ubuntu-latest - needs: 'test' + needs: test steps: - - uses: actions/checkout@v4 - - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' + - uses: ./.github/actions/setup-go - name: Create bin directory run: mkdir -p bin/ @@ -75,7 +55,7 @@ jobs: release: runs-on: ubuntu-latest - needs: "build" + needs: build steps: - uses: actions/checkout@v4 @@ -88,8 +68,7 @@ jobs: - name: Set short git commit SHA id: vars run: | - calculatedSha=$(git rev-parse --short ${{ github.sha }}) - echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV + echo "COMMIT_SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV - name: Archive release run: zip -r bin/${{ env.RELEASE_FILENAME }}-${{ env.RELEASE_VERSION }}.zip bin/ @@ -102,26 +81,3 @@ jobs: fail_on_unmatched_files: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - message: - runs-on: ubuntu-latest - steps: - - uses: actions/github-script@v6 - id: get_pr_data - with: - script: | - return ( - await github.rest.repos.listPullRequestsAssociatedWithCommit({ - commit_sha: context.sha, - owner: context.repo.owner, - repo: context.repo.repo, - }) - ).data[0]; - - name: Discord Webhook Action - uses: tsickert/discord-webhook@v7.0.0 - with: - webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }} - embed-title: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).title || '🎉 New update on `master` branch' }} - embed-description: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).body || 'No description provided.' }} - embed-url: ${{ steps.get_pr_data.outputs.result && fromJson(steps.get_pr_data.outputs.result).html_url || github.event.compare }} - diff --git a/.github/workflows/discord-notify.yml b/.github/workflows/discord-notify.yml new file mode 100644 index 00000000..ba3671b2 --- /dev/null +++ b/.github/workflows/discord-notify.yml @@ -0,0 +1,41 @@ +name: Notify Discord + +on: + pull_request: + types: + - opened + +permissions: + contents: read + +jobs: + notify-discord: + runs-on: ubuntu-latest + steps: + - name: Get PR Metadata + if: github.event_name == 'pull_request' + uses: actions/github-script@v6 + id: pr_meta + with: + script: | + core.setOutput('title', context.payload.pull_request.title || '') + core.setOutput('body', context.payload.pull_request.body || '') + core.setOutput('url', context.payload.pull_request.html_url || '') + + - name: Get Release Metadata + if: github.event_name == 'release' + uses: actions/github-script@v6 + id: release_meta + with: + script: | + core.setOutput('title', context.payload.release.name || context.payload.release.tag_name || '') + core.setOutput('body', context.payload.release.body || '') + core.setOutput('url', context.payload.release.html_url || '') + + - name: Send Discord Message + uses: ./.github/actions/discord-webhook + with: + webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }} + pr-title: ${{ steps.pr_meta.outputs.title }}${{ steps.release_meta.outputs.title }} + pr-body: ${{ steps.pr_meta.outputs.body }}${{ steps.release_meta.outputs.body }} + pr-url: ${{ steps.pr_meta.outputs.url }}${{ steps.release_meta.outputs.url }} diff --git a/.github/workflows/docker-package.yml b/.github/workflows/docker-package.yml index df6e0a1d..9a9eed02 100644 --- a/.github/workflows/docker-package.yml +++ b/.github/workflows/docker-package.yml @@ -1,5 +1,3 @@ -# https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images - name: Docker Package on: @@ -7,61 +5,46 @@ on: push: branches: - master - tags: - - 'v*.*.*' pull_request: branches: - master env: - # Use docker.io for Docker Hub if empty REGISTRY: ghcr.io - # github.repository as / IMAGE_NAME: ${{ github.repository }} RELEASE_VERSION: ${{ github.ref_name }} jobs: package: runs-on: ubuntu-latest - - # Sets the permissions granted to the GITHUB_TOKEN for the actions in this job. permissions: contents: read packages: write attestations: write id-token: write - steps: - - name: Checkout repository - uses: actions/checkout@v4 + - uses: actions/checkout@v4 + - uses: ./.github/actions/setup-go - # https://github.com/docker/login-action - name: Log in to the Container registry ${{ env.REGISTRY }} if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} - # https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication password: ${{ secrets.GITHUB_TOKEN }} - # https://github.com/docker/metadata-action - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - # generate Docker tags based on the following events/attributes tags: | - # set latest tag for master branch type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }} type=ref,event=tag type=ref,event=pr type=sha - # https://github.com/docker/build-push-action - # For pull request, only ensures that the docker build succeeds, does not push the image. - # See: https://github.com/docker/build-push-action/issues/751 - name: Build and push Docker image id: push uses: docker/build-push-action@v6 @@ -72,12 +55,10 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - # https://github.com/actions/attest-build-provenance - name: Generate artifact attestation if: github.event_name != 'pull_request' uses: actions/attest-build-provenance@v2 with: - subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} subject-digest: ${{ steps.push.outputs.digest }} - # https://github.com/actions/attest-build-provenance/issues/71#issuecomment-2108140285 push-to-registry: false diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 13d6b8f2..3afc34b4 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,23 +1,19 @@ -name: run-tests +name: Run Tests on: pull_request: - branches: [master] - workflow_dispatch: + branches: + - master + - '**' + +permissions: + contents: read + pull-requests: read jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' - - - name: Run code generation - run: go generate ./... - - - name: Run tests - run: make test + - uses: ./.github/actions/setup-go + - uses: ./.github/actions/codegen-and-test