Skip to content

Commit

Permalink
Harden GitHub Actions workflows and actions (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbaldry authored Jan 14, 2025
1 parent 274083c commit cf72765
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 118 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/add-to-docs-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ jobs:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: grafana/shared-workflows/actions/get-vault-secrets@main
- uses: grafana/shared-workflows/actions/get-vault-secrets@97c6f45f01d4bca8a3b1acfe397113ce88858a81 # get-vault-secrets-v1.0.1
with:
common_secrets: |
ISSUE_COMMANDS_APP_ID=docs-team/issue-commands:app-id
ISSUE_COMMANDS_PRIVATE_KEY=docs-team/issue-commands:key
- uses: actions/create-github-app-token@v1
- uses: actions/create-github-app-token@c1a285145b9d317df6ced56c09f525b5c2b6f755 # v1.11.1
id: app-token
with:
app-id: ${{ env.ISSUE_COMMANDS_APP_ID }}
owner: grafana
private-key: ${{ env.ISSUE_COMMANDS_PRIVATE_KEY }}

- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- uses: actions/setup-node@v4
- uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
cache: npm
node-version-file: "add-to-docs-project/package.json"
Expand All @@ -45,7 +47,7 @@ jobs:
- name: Send Slack Message
id: slack
if: ${{ steps.add-to-docs-project.outputs.added != ''}}
uses: grafana/shared-workflows/actions/send-slack-message@7b628e7352c2dea057c565cc4fcd5564d5f396c0 #v1.0.0
uses: grafana/shared-workflows/actions/send-slack-message@7b628e7352c2dea057c565cc4fcd5564d5f396c0 # v1.0.0
with:
channel-id: C05V6A36MB7
payload: |
Expand All @@ -64,7 +66,7 @@ jobs:
- name: Notify failure
if: failure()
uses: grafana/shared-workflows/actions/send-slack-message@7b628e7352c2dea057c565cc4fcd5564d5f396c0 #v1.0.0
uses: grafana/shared-workflows/actions/send-slack-message@7b628e7352c2dea057c565cc4fcd5564d5f396c0 # v1.0.0
with:
channel-id: C05V6A36MB7
payload: |
Expand Down
39 changes: 0 additions & 39 deletions .github/workflows/backport.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/deploy-pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ on:

jobs:
deploy-pr-preview:
if: ! github.event.pull_request.head.repo.fork
uses: grafana/writers-toolkit/.github/workflows/deploy-preview.yml@main
if: ${{ ! github.event.pull_request.head.repo.fork }}
with:
sha: ${{ github.event.pull_request.head.sha }}
branch: ${{ github.head_ref }}
Expand Down
27 changes: 18 additions & 9 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ on:
env:
CLOUD_RUN_REGION: us-south1

permissions:
id-token: write # Needed for authentication.
statuses: write # Needed to send deploy preview link as a commit status.
pull-requests: write # Needed to add/update a comment with the deploy preview link.

concurrency:
group: ${{ github.workflow }}-${{ inputs.repo }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
deploy-preview:
permissions: write-all
permissions:
contents: read
id-token: write # Fetch Vault secrets.
pull-requests: write # Create or update PR comments.
statuses: write # Update GitHub status check with deploy preview link.
runs-on: ubuntu-latest
steps:
- name: Find comment
Expand All @@ -70,6 +69,7 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
if: github.event.action == 'opened' || github.event.action == 'synchronize'
with:
persist-credentials: false
sparse-checkout-cone-mode: false # exclude root files
sparse-checkout: docs

Expand All @@ -81,6 +81,7 @@ jobs:
repository: "grafana/writers-toolkit"
ref: "main"
path: deploy-preview-files
persist-credentials: false
sparse-checkout: |
deploy-preview
Expand All @@ -89,11 +90,13 @@ jobs:
- name: Keep only necessary files
if: github.event.action == 'opened' || github.event.action == 'synchronize'
shell: bash
env:
SOURCE_DIRECTORY: ${{ inputs.source_directory }}
run: |
shopt -s extglob
rm -rf !(docs|deploy-preview-files|.git)
ls -al
ls -al ${{ inputs.source_directory }}
ls -al "${SOURCE_DIRECTORY}"
ls -al deploy-preview-files
- name: Build website
Expand All @@ -110,8 +113,10 @@ jobs:
- name: Print build header value
if: github.event.action == 'opened' || github.event.action == 'synchronize'
shell: bash
env:
SHA: ${{ inputs.sha }}
run: |
printf "%s" "add_header 'Build' '"${{ inputs.sha }}"';" > build.conf
printf "%s" "add_header 'Build' '"${SHA}"';" > build.conf
- uses: google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f # v2.1.7
if: github.event.action == 'opened' || github.event.action == 'synchronize'
Expand Down Expand Up @@ -144,6 +149,7 @@ jobs:
if: github.event.action == 'opened' || github.event.action == 'synchronize'
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
with:
cache-binary: false
driver: docker-container

- name: Build the container
Expand Down Expand Up @@ -181,8 +187,11 @@ jobs:

- name: Delete deploy preview
if: github.event.action == 'closed'
env:
REPO: ${{ inputs.repo }}
EVENT_NUMBER: ${{ inputs.event_number }}
run: |
SERVICE_NAME=deploy-preview-${{ inputs.repo }}-${{ inputs.event_number }}
SERVICE_NAME="deploy-preview-${REPO}-${EVENT_NUMBER}"
if gcloud run services describe $SERVICE_NAME --region=${{ env.CLOUD_RUN_REGION }} --project=grafanalabs-dev > /dev/null 2>&1; then
gcloud run services delete $SERVICE_NAME --region=${{ env.CLOUD_RUN_REGION }} --project=grafanalabs-dev --quiet
else
Expand Down
12 changes: 4 additions & 8 deletions .github/workflows/dictionaries.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
name: Rebuild the dictionaries on a branch
on:
workflow_dispatch:
inputs:
trace:
default: false
description: Print command traces?
required: false
type: boolean
jobs:
main:
if: ${{ github.repository == 'grafana/writers-toolkit' }}
container:
image: bitnami/jsonnet@sha256:3d8b084da1b74f5d38bc35e1ebf02f4a57c0410ec59d4edbc25dd2fec5f5541c
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- working-directory: vale/dictionaries
run: |
set -euf -o pipefail
if ${{ inputs.trace }}; then
if [[ -n "${RUNNER_DEBUG+x}" ]]; then
set -x
fi
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ on:
branches:
- main
workflow_dispatch:
permissions:
contents: read
id-token: write

jobs:
build:
if: ${{ github.repository == 'grafana/writers-toolkit' }}
permissions:
contents: read
id-token: write
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- id: push-to-dockerhub
uses: grafana/shared-workflows/actions/build-push-to-dockerhub@main
uses: grafana/shared-workflows/actions/build-push-to-dockerhub@45747d11c20341064eab8c186e00a46d20ba4e73 # build-push-to-dockerhub-v0.1.0
with:
context: vale
platforms: linux/amd64,linux/arm64
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
name: Run `prettier` on a branch
on:
workflow_dispatch:
inputs:
trace:
default: false
description: Print command traces?
required: false
type: boolean
jobs:
prettier:
if: ${{ github.repository == 'grafana/writers-toolkit' }}
runs-on: ubuntu-latest
steps:
- uses: grafana/writers-toolkit/prettier@main
- uses: grafana/writers-toolkit/prettier@62fe122eb653707d1c49bc0e41cf4b9704c9064b # prettier/v2.0.0
with:
branch: ${{ env.GITHUB_REF }}
trace: ${{ inputs.trace }}
4 changes: 3 additions & 1 deletion .github/workflows/publish-technical-documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: ./publish-technical-documentation
with:
website_directory: content/docs/writers-toolkit
8 changes: 5 additions & 3 deletions .github/workflows/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
with:
go-version: 1.21

Expand All @@ -24,7 +26,7 @@ jobs:
run: ./tools/review ./docs/sources | head -n 3 > .to-review.txt

- name: Create issues
uses: actions/github-script@v7
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ jobs:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
persist-credentials: false
- uses: ./publish-technical-documentation-release
with:
release_tag_regexp: "^test/publish-technical-documentation-release/v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/vale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
container:
image: grafana/vale:latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
# Not a separate file because then it doesn't have to wait for a checkout to be run.
script: |
Expand All @@ -19,16 +19,19 @@ jobs:
comment_id: context.payload.comment.id,
content: 'eyes',
});
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
ref: refs/pull/${{ github.event.issue.number }}/head
- uses: actions/github-script@v7

- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
id: from-comment
with:
script: |
const script = require('./.github/workflows/from-comment.js');
await script({ core, context, github });
- name: Run linter
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
Loading

0 comments on commit cf72765

Please sign in to comment.