Skip to content

Commit 0220791

Browse files
authored
chore: secure github actions (#31)
based on learnings from https://snarky.ca/how-to-publish-to-pypi-using-github-actions-securely/
1 parent 438b398 commit 0220791

5 files changed

Lines changed: 142 additions & 96 deletions

File tree

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: monthly
7+
cooldown:
8+
default-days: 7

.github/workflows/docker-edge.yml

Lines changed: 0 additions & 63 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ on:
55
tags:
66
- 'v*.*.*'
77

8+
permissions: {}
9+
810
env:
911
GO_VERSION: '1.26.2'
1012

1113
jobs:
1214
build:
1315
runs-on: ${{ matrix.os }}
16+
permissions:
17+
contents: read
1418
strategy:
1519
matrix:
1620
include:
@@ -29,7 +33,9 @@ jobs:
2933
cc: clang
3034
steps:
3135
- name: Checkout
32-
uses: actions/checkout@v6
36+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
37+
with:
38+
persist-credentials: false
3339

3440
- name: Install dependencies (Ubuntu)
3541
if: ${{ matrix.os == 'ubuntu-latest' && matrix.arch == 'amd64' }}
@@ -70,15 +76,14 @@ jobs:
7076
brew install sqlite
7177
7278
- name: Set up Go
73-
uses: actions/setup-go@v6
79+
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
7480
with:
7581
go-version: ${{ env.GO_VERSION }}
76-
cache: true
77-
cache-dependency-path: go.sum
78-
82+
# Disabled to avoid cache poisoning on publish workflows.
83+
cache: false
7984

8085
- name: Install esbuild
81-
run: npm install -g esbuild
86+
run: npm install -g esbuild@0.28.1
8287

8388
- name: Build assets
8489
run: make assets
@@ -87,40 +92,48 @@ jobs:
8792
run: |
8893
mkdir -p build
8994
go build \
90-
-ldflags="-s -w -X main.version=${{ github.ref_name }} -X main.commit=${{ github.sha }} -X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
91-
-o build/mango-go-${{ matrix.os }}-${{ matrix.arch }} \
95+
-ldflags="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
96+
-o "build/mango-go-${OS_NAME}-${ARCH}" \
9297
.
9398
env:
99+
VERSION: ${{ github.ref_name }}
100+
COMMIT: ${{ github.sha }}
101+
OS_NAME: ${{ matrix.os }}
102+
ARCH: ${{ matrix.arch }}
94103
GOOS: ${{ matrix.os == 'ubuntu-latest' && 'linux' || 'darwin' }}
95104
CGO_ENABLED: 1
96105
GOARCH: ${{ matrix.arch }}
97106
CC: ${{ matrix.cc }}
98107

99108
- name: Create release archive
100109
run: |
101-
tar -czf build/mango-go-${{ matrix.os }}-${{ matrix.arch }}.tar.gz \
102-
build/mango-go-${{ matrix.os }}-${{ matrix.arch }} \
110+
tar -czf "build/mango-go-${OS_NAME}-${ARCH}.tar.gz" \
111+
"build/mango-go-${OS_NAME}-${ARCH}" \
103112
README.md LICENSE
113+
env:
114+
OS_NAME: ${{ matrix.os }}
115+
ARCH: ${{ matrix.arch }}
104116

105117
- name: Upload artifacts
106-
uses: actions/upload-artifact@v7
118+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
107119
with:
108120
name: mango-go-${{ matrix.os }}-${{ matrix.arch }}
109121
path: build/mango-go-${{ matrix.os }}-${{ matrix.arch }}.tar.gz
110122
retention-days: 7
111123

112124
release:
113125
runs-on: ubuntu-latest
126+
needs: build
114127
permissions:
115128
contents: write
116-
packages: write
117-
needs: build
118129
steps:
119130
- name: Checkout
120-
uses: actions/checkout@v6
131+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
132+
with:
133+
persist-credentials: false
121134

122135
- name: Download all artifacts
123-
uses: actions/download-artifact@v8
136+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
124137

125138
- name: Create checksums and rename files
126139
run: |
@@ -155,15 +168,15 @@ jobs:
155168
done
156169
157170
- name: Create Release
158-
uses: softprops/action-gh-release@v2
159-
with:
160-
files: |
161-
renamed/*.tar.gz
162-
checksums.txt
163-
generate_release_notes: true
164-
tag_name: ${{ github.ref_name }}
165-
draft: false
166-
prerelease: false
171+
env:
172+
GH_TOKEN: ${{ github.token }}
173+
TAG_NAME: ${{ github.ref_name }}
174+
run: |
175+
gh release create "$TAG_NAME" \
176+
renamed/*.tar.gz \
177+
checksums.txt \
178+
--generate-notes \
179+
--title "$TAG_NAME"
167180
168181
docker:
169182
runs-on: ubuntu-latest
@@ -173,32 +186,34 @@ jobs:
173186
packages: write
174187
steps:
175188
- name: Checkout
176-
uses: actions/checkout@v6
189+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
190+
with:
191+
persist-credentials: false
177192

178193
- name: Set up QEMU
179-
uses: docker/setup-qemu-action@v4
194+
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0
180195

181196
- name: Set up Docker Buildx
182-
uses: docker/setup-buildx-action@v4
197+
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
183198

184199
- name: Log in to GitHub Container Registry
185-
uses: docker/login-action@v4
200+
uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # v4.4.0
186201
with:
187202
registry: ghcr.io
188203
username: ${{ github.actor }}
189204
password: ${{ secrets.GITHUB_TOKEN }}
190205

191206
- name: Extract metadata
192207
id: meta
193-
uses: docker/metadata-action@v6
208+
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
194209
with:
195210
images: ghcr.io/${{ github.repository }}
196211
tags: |
197212
type=semver,pattern={{version}}
198213
type=raw,value=latest
199214
200215
- name: Build and push Docker image
201-
uses: docker/build-push-action@v7
216+
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
202217
with:
203218
context: .
204219
platforms: linux/amd64,linux/arm64

.github/workflows/test.yml

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ on:
66
pull_request:
77
branches: [ "master" ]
88

9+
permissions: {}
10+
911
jobs:
1012
test:
1113
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
1216

1317
steps:
1418
- name: Checkout repository
15-
uses: actions/checkout@v6
19+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
20+
with:
21+
persist-credentials: false
1622

1723
- name: Set up Go
18-
uses: actions/setup-go@v6
24+
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
1925
with:
2026
go-version: '1.26.2'
2127

2228
- name: Install esbuild
23-
run: npm install -g esbuild
29+
run: npm install -g esbuild@0.28.1
2430

2531
- name: Install dependencies
2632
run: |
@@ -31,3 +37,57 @@ jobs:
3137

3238
- name: Run tests
3339
run: go test ./...
40+
41+
# Publishes the :dev edge image after tests pass on master.
42+
# Kept in this workflow (instead of workflow_run) to avoid the dangerous trigger.
43+
release-edge-image:
44+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
45+
needs: test
46+
runs-on: ubuntu-latest
47+
permissions:
48+
contents: read
49+
packages: write
50+
51+
steps:
52+
- name: Checkout
53+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
54+
with:
55+
persist-credentials: false
56+
57+
- name: Set up QEMU
58+
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0
59+
with:
60+
platforms: linux/amd64,linux/arm64
61+
62+
- name: Set up Docker Buildx
63+
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
64+
65+
- name: Log in to GitHub Container Registry
66+
uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # v4.4.0
67+
with:
68+
registry: ghcr.io
69+
username: ${{ github.actor }}
70+
password: ${{ secrets.GITHUB_TOKEN }}
71+
72+
- name: Extract metadata
73+
id: meta
74+
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
75+
with:
76+
images: ghcr.io/${{ github.repository }}
77+
tags: |
78+
type=raw,value=dev
79+
80+
- name: Build and push Docker image
81+
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
82+
with:
83+
context: .
84+
platforms: linux/amd64,linux/arm64
85+
push: true
86+
tags: ${{ steps.meta.outputs.tags }}
87+
labels: ${{ steps.meta.outputs.labels }}
88+
cache-from: |
89+
type=gha
90+
type=registry,ref=ghcr.io/${{ github.repository }}:cache
91+
cache-to: |
92+
type=gha,mode=max
93+
type=registry,ref=ghcr.io/${{ github.repository }}:cache,mode=max

.github/workflows/zizmor.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: GitHub Actions Security Analysis with zizmor
2+
3+
on:
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
branches: ["**"]
8+
9+
permissions: {}
10+
11+
jobs:
12+
zizmor:
13+
name: Run zizmor
14+
runs-on: ubuntu-latest
15+
permissions:
16+
security-events: write
17+
contents: read
18+
actions: read
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
22+
with:
23+
persist-credentials: false
24+
25+
- name: Run zizmor
26+
uses: zizmorcore/zizmor-action@192e21d79ab29983730a13d1382995c2307fbcaa # v0.5.7

0 commit comments

Comments
 (0)