mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-08-06 08:54:21 +02:00
Compare commits
163 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34bfa19150 | ||
|
|
96d9f491db | ||
|
|
14ec00b66e | ||
|
|
68c6a5b4f1 | ||
|
|
0cd0e52a24 | ||
|
|
47d5b5ad03 | ||
|
|
2398d4a527 | ||
|
|
b7a3bf98bc | ||
|
|
da4037899a | ||
|
|
e4fe49dba4 | ||
|
|
41c72216bf | ||
|
|
3f7fd16ea1 | ||
|
|
0192861155 | ||
|
|
e6c7ba3a15 | ||
|
|
61f0cfa951 | ||
|
|
fc0e03e5a9 | ||
|
|
333eb17d19 | ||
|
|
c3b39e0d99 | ||
|
|
78a74f78f8 | ||
|
|
8c519ce318 | ||
|
|
de43c84203 | ||
|
|
3c5ef1721a | ||
|
|
94ab020204 | ||
|
|
b4a64b97dd | ||
|
|
26f9fb12af | ||
|
|
c9c4957e38 | ||
|
|
b1a02cdd5d | ||
|
|
0fd8602ac3 | ||
|
|
6133d64270 | ||
|
|
c43cbe87ca | ||
|
|
7bec310002 | ||
|
|
8af385d147 | ||
|
|
46f22c78d2 | ||
|
|
068afc3996 | ||
|
|
0e8896c52a | ||
|
|
89467c9dd0 | ||
|
|
0c08b0f2da | ||
|
|
aa7a29a157 | ||
|
|
ad967330a8 | ||
|
|
60177008a5 | ||
|
|
58c5eb8d21 | ||
|
|
d6882b3df5 | ||
|
|
7e7e3ef1a6 | ||
|
|
16357a34b2 | ||
|
|
d53538ac38 | ||
|
|
554b3b7671 | ||
|
|
65756d60b3 | ||
|
|
be9b4502d6 | ||
|
|
1d74ae636a | ||
|
|
b12d02c25f | ||
|
|
f2e0cf9131 | ||
|
|
0ee4643d4a | ||
|
|
e774003c18 | ||
|
|
eeb479ea89 | ||
|
|
eba33e178d | ||
|
|
3396021e0f | ||
|
|
745b0ab6e4 | ||
|
|
b7f6b6d90a | ||
|
|
cdcea87a45 | ||
|
|
3c4bcf3ebf | ||
|
|
e22d3fa263 | ||
|
|
99bc50d538 | ||
|
|
8f72c60afa | ||
|
|
4e7fd1c68a | ||
|
|
bd41a367fe | ||
|
|
c566013db4 | ||
|
|
40e021309a | ||
|
|
d3b3519dea | ||
|
|
6bdcb54828 | ||
|
|
007717956a | ||
|
|
df0370f8bf | ||
|
|
5f0636faad | ||
|
|
4997f33b5f | ||
|
|
2963716953 | ||
|
|
3996d6d032 | ||
|
|
205af7cd01 | ||
|
|
33e6d1d8ff | ||
|
|
56979e6ab8 | ||
|
|
bf99e6a758 | ||
|
|
740a3d4db4 | ||
|
|
822af5029f | ||
|
|
526c46b485 | ||
|
|
355289bc54 | ||
|
|
e583b0706b | ||
|
|
8ad84cd96a | ||
|
|
0a2f28244d | ||
|
|
443b0e336c | ||
|
|
53c4db6a4b | ||
|
|
1073c8bfec | ||
|
|
ff7d9ca8d0 | ||
|
|
984b47c716 | ||
|
|
c749e52bb7 | ||
|
|
f17b6b9fc3 | ||
|
|
c7c4bd600a | ||
|
|
abec931d98 | ||
|
|
270ea41232 | ||
|
|
0b9f251b6a | ||
|
|
273f6b4247 | ||
|
|
47ee45412a | ||
|
|
38b69bb214 | ||
|
|
1c62c0635f | ||
|
|
0e0c54b272 | ||
|
|
d6fbe75721 | ||
|
|
b30204aa94 | ||
|
|
7b5ebe9618 | ||
|
|
4317662a38 | ||
|
|
2208e7ec63 | ||
|
|
fab9714f9a | ||
|
|
10475db58a | ||
|
|
9e738c203c | ||
|
|
6023928876 | ||
|
|
014ce438c1 | ||
|
|
cf7e29c10d | ||
|
|
8a99506fed | ||
|
|
5873b8b054 | ||
|
|
5464d33eef | ||
|
|
3c5f03ff8f | ||
|
|
880e9755d9 | ||
|
|
8d7cf48a6f | ||
|
|
f23605c614 | ||
|
|
00b7fec80f | ||
|
|
dda5841af8 | ||
|
|
32bed52686 | ||
|
|
a7e972d8de | ||
|
|
763b38ece3 | ||
|
|
a1f13cb970 | ||
|
|
1e3ab0c40a | ||
|
|
295eecb9af | ||
|
|
ef6ca957b5 | ||
|
|
8088df52b9 | ||
|
|
3ea7d39690 | ||
|
|
861d351845 | ||
|
|
cce8543d06 | ||
|
|
75643645f0 | ||
|
|
dff63b3ecc | ||
|
|
a5d9fe9651 | ||
|
|
d607f3b342 | ||
|
|
5e59402fb2 | ||
|
|
dfeb463904 | ||
|
|
594c9ade7c | ||
|
|
2a4d56c650 | ||
|
|
a22119cf88 | ||
|
|
b68ecf2580 | ||
|
|
d1434237c2 | ||
|
|
35c65e2b14 | ||
|
|
c45a4e6d32 | ||
|
|
68d9fc45c9 | ||
|
|
b1c873a66b | ||
|
|
1d6e7879c8 | ||
|
|
13dc9386fe | ||
|
|
8e6b3be96a | ||
|
|
e5e53c732e | ||
|
|
2516573592 | ||
|
|
35834bf817 | ||
|
|
11a5dc8936 | ||
|
|
f09fafcb0a | ||
|
|
801e5cf4d5 | ||
|
|
3f05040438 | ||
|
|
59d90bff26 | ||
|
|
5edc4ba550 | ||
|
|
547a0ff297 | ||
|
|
f2b4dbf05f | ||
|
|
bad4239d18 |
@@ -40,7 +40,7 @@ cpu.out
|
|||||||
*.db
|
*.db
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
/act_runner
|
/gitea-runner
|
||||||
/debug
|
/debug
|
||||||
|
|
||||||
/bin
|
/bin
|
||||||
|
|||||||
27
.gitea/workflows/pull-pr-title.yml
Normal file
27
.gitea/workflows/pull-pr-title.yml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
name: pr-title
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types:
|
||||||
|
- opened
|
||||||
|
- edited
|
||||||
|
- reopened
|
||||||
|
- synchronize
|
||||||
|
- ready_for_review
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint-pr-title:
|
||||||
|
if: github.event.pull_request.draft == false
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 5
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
|
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||||
|
with:
|
||||||
|
node-version: 24
|
||||||
|
- run: make lint-pr-title
|
||||||
|
env:
|
||||||
|
PR_TITLE: ${{ github.event.pull_request.title }}
|
||||||
@@ -17,14 +17,26 @@ jobs:
|
|||||||
goreleaser:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- uses: actions/setup-go@v6
|
# Custom publishers (the R2 mirror below) run as the very last
|
||||||
|
# step of goreleaser's publish pipeline, after the Gitea release
|
||||||
|
# has already been created and every artifact already uploaded
|
||||||
|
# to S3. Fail here instead, before anything is built or
|
||||||
|
# published, if the R2 secrets are missing.
|
||||||
|
- name: check R2 configuration
|
||||||
|
run: sh scripts/upload-r2.sh --check-config
|
||||||
|
env:
|
||||||
|
R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}
|
||||||
|
R2_BUCKET: ${{ secrets.R2_BUCKET }}
|
||||||
|
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
||||||
|
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
||||||
|
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
|
||||||
with:
|
with:
|
||||||
go-version-file: "go.mod"
|
go-version-file: "go.mod"
|
||||||
- name: goreleaser
|
- name: goreleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7
|
||||||
with:
|
with:
|
||||||
distribution: goreleaser-pro
|
distribution: goreleaser-pro
|
||||||
args: release --nightly
|
args: release --nightly
|
||||||
@@ -35,6 +47,10 @@ jobs:
|
|||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||||
S3_REGION: ${{ secrets.AWS_REGION }}
|
S3_REGION: ${{ secrets.AWS_REGION }}
|
||||||
S3_BUCKET: ${{ secrets.AWS_BUCKET }}
|
S3_BUCKET: ${{ secrets.AWS_BUCKET }}
|
||||||
|
R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}
|
||||||
|
R2_BUCKET: ${{ secrets.R2_BUCKET }}
|
||||||
|
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
||||||
|
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
||||||
GORELEASER_FORCE_TOKEN: "gitea"
|
GORELEASER_FORCE_TOKEN: "gitea"
|
||||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
@@ -43,43 +59,54 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
variant:
|
variant:
|
||||||
|
# The basic image is built from source and can target any arch the
|
||||||
|
# toolchain supports. The dind variants are limited to the arches the
|
||||||
|
# docker:dind base image publishes.
|
||||||
- target: basic
|
- target: basic
|
||||||
tag_suffix: ""
|
tag_suffix: ""
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/riscv64,linux/s390x
|
||||||
- target: dind
|
- target: dind
|
||||||
tag_suffix: "-dind"
|
tag_suffix: "-dind"
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
- target: dind-rootless
|
- target: dind-rootless
|
||||||
tag_suffix: "-dind-rootless"
|
tag_suffix: "-dind-rootless"
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4
|
||||||
|
|
||||||
- name: Set up Docker BuildX
|
- name: Set up Docker BuildX
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@abd2ef45e78c5afb21d64d4ca52ee8550d9572c7 # v4
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
- name: Echo the tag
|
- name: Echo the tag
|
||||||
run: echo "${{ env.DOCKER_ORG }}/act_runner:nightly${{ matrix.variant.tag_suffix }}"
|
run: echo "${{ env.DOCKER_ORG }}/runner:nightly${{ matrix.variant.tag_suffix }}"
|
||||||
|
|
||||||
|
- name: Get Meta
|
||||||
|
id: meta
|
||||||
|
run: |
|
||||||
|
echo REPO_VERSION=$(git describe --tags --always | sed 's/-/+/' | sed 's/^v//') >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
target: ${{ matrix.variant.target }}
|
target: ${{ matrix.variant.target }}
|
||||||
platforms: |
|
platforms: ${{ matrix.variant.platforms }}
|
||||||
linux/amd64
|
|
||||||
linux/arm64
|
|
||||||
push: true
|
push: true
|
||||||
tags: |
|
tags: |
|
||||||
${{ env.DOCKER_ORG }}/act_runner:nightly${{ matrix.variant.tag_suffix }}
|
${{ env.DOCKER_ORG }}/runner:nightly${{ matrix.variant.tag_suffix }}
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.meta.outputs.REPO_VERSION }}
|
||||||
|
|||||||
@@ -9,21 +9,33 @@ jobs:
|
|||||||
goreleaser:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
- uses: actions/setup-go@v6
|
# Custom publishers (the R2 mirror below) run as the very last
|
||||||
|
# step of goreleaser's publish pipeline, after the Gitea release
|
||||||
|
# has already been created and every artifact already uploaded
|
||||||
|
# to S3. Fail here instead, before anything is built or
|
||||||
|
# published, if the R2 secrets are missing.
|
||||||
|
- name: check R2 configuration
|
||||||
|
run: sh scripts/upload-r2.sh --check-config
|
||||||
|
env:
|
||||||
|
R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}
|
||||||
|
R2_BUCKET: ${{ secrets.R2_BUCKET }}
|
||||||
|
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
||||||
|
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
||||||
|
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
|
||||||
with:
|
with:
|
||||||
go-version-file: "go.mod"
|
go-version-file: "go.mod"
|
||||||
- name: Import GPG key
|
- name: Import GPG key
|
||||||
id: import_gpg
|
id: import_gpg
|
||||||
uses: crazy-max/ghaction-import-gpg@v6
|
uses: crazy-max/ghaction-import-gpg@2dc316deee8e90f13e1a351ab510b4d5bc0c82cd # v7
|
||||||
with:
|
with:
|
||||||
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
|
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
|
||||||
passphrase: ${{ secrets.PASSPHRASE }}
|
passphrase: ${{ secrets.PASSPHRASE }}
|
||||||
fingerprint: CC64B1DB67ABBEECAB24B6455FC346329753F4B0
|
fingerprint: CC64B1DB67ABBEECAB24B6455FC346329753F4B0
|
||||||
- name: goreleaser
|
- name: goreleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7
|
||||||
with:
|
with:
|
||||||
distribution: goreleaser-pro
|
distribution: goreleaser-pro
|
||||||
args: release
|
args: release
|
||||||
@@ -34,6 +46,10 @@ jobs:
|
|||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||||
S3_REGION: ${{ secrets.AWS_REGION }}
|
S3_REGION: ${{ secrets.AWS_REGION }}
|
||||||
S3_BUCKET: ${{ secrets.AWS_BUCKET }}
|
S3_BUCKET: ${{ secrets.AWS_BUCKET }}
|
||||||
|
R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}
|
||||||
|
R2_BUCKET: ${{ secrets.R2_BUCKET }}
|
||||||
|
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
||||||
|
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
||||||
GORELEASER_FORCE_TOKEN: "gitea"
|
GORELEASER_FORCE_TOKEN: "gitea"
|
||||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
|
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
|
||||||
@@ -42,12 +58,18 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
variant:
|
variant:
|
||||||
|
# The basic image is built from source and can target any arch the
|
||||||
|
# toolchain supports. The dind variants are limited to the arches the
|
||||||
|
# docker:dind base image publishes.
|
||||||
- target: basic
|
- target: basic
|
||||||
tag_suffix: ""
|
tag_suffix: ""
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/riscv64,linux/s390x
|
||||||
- target: dind
|
- target: dind
|
||||||
tag_suffix: "-dind"
|
tag_suffix: "-dind"
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
- target: dind-rootless
|
- target: dind-rootless
|
||||||
tag_suffix: "-dind-rootless"
|
tag_suffix: "-dind-rootless"
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
container:
|
container:
|
||||||
image: catthehacker/ubuntu:act-latest
|
image: catthehacker/ubuntu:act-latest
|
||||||
env:
|
env:
|
||||||
@@ -55,33 +77,28 @@ jobs:
|
|||||||
DOCKER_LATEST: latest
|
DOCKER_LATEST: latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4
|
||||||
|
|
||||||
- name: Set up Docker BuildX
|
- name: Set up Docker BuildX
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@abd2ef45e78c5afb21d64d4ca52ee8550d9572c7 # v4
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
- name: Repo Meta
|
|
||||||
id: repo_meta
|
|
||||||
run: |
|
|
||||||
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: "Docker meta"
|
- name: "Docker meta"
|
||||||
id: docker_meta
|
id: docker_meta
|
||||||
uses: https://github.com/docker/metadata-action@v5
|
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6
|
||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
${{ env.DOCKER_ORG }}/${{ steps.repo_meta.outputs.REPO_NAME }}
|
${{ env.DOCKER_ORG }}/runner
|
||||||
tags: |
|
tags: |
|
||||||
type=semver,pattern={{major}}.{{minor}}.{{patch}}
|
type=semver,pattern={{major}}.{{minor}}.{{patch}}
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
@@ -91,13 +108,13 @@ jobs:
|
|||||||
suffix=${{ matrix.variant.tag_suffix }},onlatest=true
|
suffix=${{ matrix.variant.tag_suffix }},onlatest=true
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
target: ${{ matrix.variant.target }}
|
target: ${{ matrix.variant.target }}
|
||||||
platforms: |
|
platforms: ${{ matrix.variant.platforms }}
|
||||||
linux/amd64
|
|
||||||
linux/arm64
|
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.docker_meta.outputs.tags }}
|
tags: ${{ steps.docker_meta.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.docker_meta.outputs.version }}
|
||||||
|
|||||||
@@ -1,20 +1,50 @@
|
|||||||
name: checks
|
name: checks
|
||||||
on:
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
lint:
|
lint:
|
||||||
name: check and test
|
name: check and test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
# The runner image ships a stale docker.io login; point docker at an empty config so
|
||||||
|
# image pulls go straight to anonymous instead of attempting (and failing) that auth
|
||||||
|
# first. The path must be a literal: the `runner` context is unavailable in job-level
|
||||||
|
# env, so `${{ runner.temp }}` would resolve to empty and config.Dir() would fall back
|
||||||
|
# to ~/.docker with the stale credentials.
|
||||||
|
DOCKER_CONFIG: /tmp/docker-noauth
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
- uses: actions/setup-go@v6
|
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
|
||||||
with:
|
with:
|
||||||
go-version-file: 'go.mod'
|
go-version-file: 'go.mod'
|
||||||
|
- name: prepare anonymous docker config
|
||||||
|
run: mkdir -p "$DOCKER_CONFIG" && echo '{}' > "$DOCKER_CONFIG/config.json"
|
||||||
|
# Pre-pull act/runner's two largest base images so a slow pull can't dominate `make test`;
|
||||||
|
# the rest (alpine/ubuntu) pull on demand, absorbed by the make-test -timeout. The host
|
||||||
|
# daemon retains them between runs, so this is usually a fast manifest re-check.
|
||||||
|
- name: pre-pull test images
|
||||||
|
run: |
|
||||||
|
for img in node:24-bookworm-slim nginx:alpine; do
|
||||||
|
for try in 1 2 3; do docker pull "$img" && break || sleep 5; done
|
||||||
|
done
|
||||||
- name: lint
|
- name: lint
|
||||||
run: make lint
|
run: make lint
|
||||||
|
- name: checks
|
||||||
|
run: make checks
|
||||||
- name: build
|
- name: build
|
||||||
run: make build
|
run: make build
|
||||||
- name: test
|
- name: test
|
||||||
run: make test
|
run: make test
|
||||||
|
# Build the dind image and run the daemon-facing tests against the docker version it
|
||||||
|
# ships, catching daemon-level regressions (e.g. gitea/runner#981) before release. Runs
|
||||||
|
# after `make test` so the images it needs are already present on the host daemon.
|
||||||
|
- name: test against dind image
|
||||||
|
run: make test-dind
|
||||||
|
- name: coverage report
|
||||||
|
run: |
|
||||||
|
make coverage-report
|
||||||
|
cat .tmp/coverage.md >> "$GITHUB_STEP_SUMMARY"
|
||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,7 +1,10 @@
|
|||||||
/act_runner
|
/gitea-runner
|
||||||
.env
|
.env
|
||||||
|
!/act/runner/testdata/secrets/.env
|
||||||
.runner
|
.runner
|
||||||
|
.runner.lock
|
||||||
coverage.txt
|
coverage.txt
|
||||||
|
.tmp/
|
||||||
/config.yaml
|
/config.yaml
|
||||||
|
|
||||||
# Jetbrains
|
# Jetbrains
|
||||||
@@ -10,4 +13,5 @@ coverage.txt
|
|||||||
.vscode
|
.vscode
|
||||||
__debug_bin
|
__debug_bin
|
||||||
# gorelease binary folder
|
# gorelease binary folder
|
||||||
dist
|
/dist
|
||||||
|
.DS_Store
|
||||||
@@ -11,6 +11,7 @@ linters:
|
|||||||
- dupl
|
- dupl
|
||||||
- errcheck
|
- errcheck
|
||||||
- forbidigo
|
- forbidigo
|
||||||
|
- forcetypeassert
|
||||||
- gocheckcompilerdirectives
|
- gocheckcompilerdirectives
|
||||||
- gocritic
|
- gocritic
|
||||||
- goheader
|
- goheader
|
||||||
@@ -102,6 +103,9 @@ linters:
|
|||||||
- linters:
|
- linters:
|
||||||
- forbidigo
|
- forbidigo
|
||||||
path: cmd
|
path: cmd
|
||||||
|
- linters:
|
||||||
|
- forcetypeassert
|
||||||
|
path: _test\.go
|
||||||
issues:
|
issues:
|
||||||
max-issues-per-linter: 0
|
max-issues-per-linter: 0
|
||||||
max-same-issues: 0
|
max-same-issues: 0
|
||||||
@@ -114,7 +118,7 @@ formatters:
|
|||||||
custom-order: true
|
custom-order: true
|
||||||
sections:
|
sections:
|
||||||
- standard
|
- standard
|
||||||
- prefix(gitea.com/gitea/act_runner)
|
- prefix(gitea.com/gitea/runner)
|
||||||
- blank
|
- blank
|
||||||
- default
|
- default
|
||||||
gofumpt:
|
gofumpt:
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
version: 2
|
version: 2
|
||||||
|
|
||||||
|
project_name: gitea-runner
|
||||||
|
|
||||||
before:
|
before:
|
||||||
hooks:
|
hooks:
|
||||||
- go mod tidy
|
- go mod tidy
|
||||||
@@ -63,7 +65,7 @@ builds:
|
|||||||
flags:
|
flags:
|
||||||
- -trimpath
|
- -trimpath
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w -X gitea.com/gitea/act_runner/internal/pkg/ver.version={{ .Summary }}
|
- -s -w -X gitea.com/gitea/runner/internal/pkg/ver.version={{ .Summary }}
|
||||||
binary: >-
|
binary: >-
|
||||||
{{ .ProjectName }}-
|
{{ .ProjectName }}-
|
||||||
{{- .Version }}-
|
{{- .Version }}-
|
||||||
@@ -86,11 +88,42 @@ blobs:
|
|||||||
provider: s3
|
provider: s3
|
||||||
bucket: "{{ .Env.S3_BUCKET }}"
|
bucket: "{{ .Env.S3_BUCKET }}"
|
||||||
region: "{{ .Env.S3_REGION }}"
|
region: "{{ .Env.S3_REGION }}"
|
||||||
directory: "act_runner/{{.Version}}"
|
directory: "gitea-runner/{{.Version}}"
|
||||||
extra_files:
|
extra_files:
|
||||||
- glob: ./**.xz
|
- glob: ./**.xz
|
||||||
- glob: ./**.sha256
|
- glob: ./**.sha256
|
||||||
|
|
||||||
|
# Mirrors the S3 `blobs:` upload above into Cloudflare R2 during the
|
||||||
|
# parallel S3+R2 period (S3 will be removed once migration completes).
|
||||||
|
# A second `blobs:` entry is impossible here since the blob pipe
|
||||||
|
# authenticates from the global AWS_* env with no per-entry
|
||||||
|
# credentials; `publishers:` supports per-entry `env:` instead, so
|
||||||
|
# it's used to invoke scripts/upload-r2.sh once per artifact. Custom
|
||||||
|
# publishers inherit almost nothing from the environment, hence the
|
||||||
|
# explicit R2_* forwarding below.
|
||||||
|
#
|
||||||
|
# This publisher fires 109 times for 73 distinct keys because
|
||||||
|
# goreleaser's release pipe already registers `release.extra_files`
|
||||||
|
# as UploadableFile artifacts, and `internal/exec`'s filterArtifacts
|
||||||
|
# appends this block's own extra_files with no de-duplication. It
|
||||||
|
# can't be globbed away, since gobwas/glob (via goreleaser/fileglob)
|
||||||
|
# has no substring-exclusion matcher. It's harmless: PUT is
|
||||||
|
# idempotent, and the `./**.xz` glob below is kept deliberately so
|
||||||
|
# this publisher declares its own complete file set rather than
|
||||||
|
# implicitly depending on the `release:` block's globs.
|
||||||
|
publishers:
|
||||||
|
- name: cloudflare-r2
|
||||||
|
checksum: true
|
||||||
|
extra_files:
|
||||||
|
- glob: ./**.xz
|
||||||
|
- glob: ./**.sha256
|
||||||
|
cmd: sh scripts/upload-r2.sh {{ abs .ArtifactPath }} gitea-runner/{{ .Version }}/{{ .ArtifactName }}
|
||||||
|
env:
|
||||||
|
- R2_ENDPOINT={{ index .Env "R2_ENDPOINT" }}
|
||||||
|
- R2_BUCKET={{ index .Env "R2_BUCKET" }}
|
||||||
|
- R2_ACCESS_KEY_ID={{ index .Env "R2_ACCESS_KEY_ID" }}
|
||||||
|
- R2_SECRET_ACCESS_KEY={{ index .Env "R2_SECRET_ACCESS_KEY" }}
|
||||||
|
|
||||||
archives:
|
archives:
|
||||||
- format: binary
|
- format: binary
|
||||||
name_template: "{{ .Binary }}"
|
name_template: "{{ .Binary }}"
|
||||||
|
|||||||
38
Dockerfile
38
Dockerfile
@@ -1,7 +1,7 @@
|
|||||||
### BUILDER STAGE
|
### BUILDER STAGE
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
FROM golang:1.26-alpine AS builder
|
FROM golang:1.26-alpine3.23 AS builder
|
||||||
|
|
||||||
# Do not remove `git` here, it is required for getting runner version when executing `make build`
|
# Do not remove `git` here, it is required for getting runner version when executing `make build`
|
||||||
RUN apk add --no-cache make git
|
RUN apk add --no-cache make git
|
||||||
@@ -9,19 +9,24 @@ RUN apk add --no-cache make git
|
|||||||
ARG GOPROXY
|
ARG GOPROXY
|
||||||
ENV GOPROXY=${GOPROXY:-}
|
ENV GOPROXY=${GOPROXY:-}
|
||||||
|
|
||||||
COPY . /opt/src/act_runner
|
COPY . /opt/src/runner
|
||||||
WORKDIR /opt/src/act_runner
|
WORKDIR /opt/src/runner
|
||||||
|
|
||||||
RUN make clean && make build
|
RUN make clean && make build
|
||||||
|
|
||||||
### DIND VARIANT
|
### DIND VARIANT
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
FROM docker:28-dind AS dind
|
FROM docker:29.6.2-dind AS dind
|
||||||
|
|
||||||
RUN apk add --no-cache s6 bash git tzdata
|
ARG VERSION=dev
|
||||||
|
|
||||||
COPY --from=builder /opt/src/act_runner/act_runner /usr/local/bin/act_runner
|
LABEL org.opencontainers.image.source="https://gitea.com/gitea/runner"
|
||||||
|
LABEL org.opencontainers.image.version="${VERSION}"
|
||||||
|
|
||||||
|
RUN apk add --no-cache s6 bash git tzdata nftables
|
||||||
|
|
||||||
|
COPY --from=builder /opt/src/runner/gitea-runner /usr/local/bin/gitea-runner
|
||||||
COPY scripts/run.sh /usr/local/bin/run.sh
|
COPY scripts/run.sh /usr/local/bin/run.sh
|
||||||
COPY scripts/s6 /etc/s6
|
COPY scripts/s6 /etc/s6
|
||||||
|
|
||||||
@@ -32,12 +37,17 @@ ENTRYPOINT ["s6-svscan","/etc/s6"]
|
|||||||
### DIND-ROOTLESS VARIANT
|
### DIND-ROOTLESS VARIANT
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
FROM docker:28-dind-rootless AS dind-rootless
|
FROM docker:29.6.2-dind-rootless AS dind-rootless
|
||||||
|
|
||||||
|
ARG VERSION=dev
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.source="https://gitea.com/gitea/runner"
|
||||||
|
LABEL org.opencontainers.image.version="${VERSION}"
|
||||||
|
|
||||||
USER root
|
USER root
|
||||||
RUN apk add --no-cache s6 bash git tzdata
|
RUN apk add --no-cache s6 bash git tzdata nftables
|
||||||
|
|
||||||
COPY --from=builder /opt/src/act_runner/act_runner /usr/local/bin/act_runner
|
COPY --from=builder /opt/src/runner/gitea-runner /usr/local/bin/gitea-runner
|
||||||
COPY scripts/run.sh /usr/local/bin/run.sh
|
COPY scripts/run.sh /usr/local/bin/run.sh
|
||||||
COPY scripts/s6 /etc/s6
|
COPY scripts/s6 /etc/s6
|
||||||
|
|
||||||
@@ -53,10 +63,16 @@ ENTRYPOINT ["s6-svscan","/etc/s6"]
|
|||||||
### BASIC VARIANT
|
### BASIC VARIANT
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
FROM alpine AS basic
|
FROM alpine:3.24 AS basic
|
||||||
|
|
||||||
|
ARG VERSION=dev
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.source="https://gitea.com/gitea/runner"
|
||||||
|
LABEL org.opencontainers.image.version="${VERSION}"
|
||||||
|
|
||||||
RUN apk add --no-cache tini bash git tzdata
|
RUN apk add --no-cache tini bash git tzdata
|
||||||
|
|
||||||
COPY --from=builder /opt/src/act_runner/act_runner /usr/local/bin/act_runner
|
COPY --from=builder /opt/src/runner/gitea-runner /usr/local/bin/gitea-runner
|
||||||
COPY scripts/run.sh /usr/local/bin/run.sh
|
COPY scripts/run.sh /usr/local/bin/run.sh
|
||||||
|
|
||||||
VOLUME /data
|
VOLUME /data
|
||||||
|
|||||||
59
Makefile
59
Makefile
@@ -1,25 +1,27 @@
|
|||||||
DIST := dist
|
DIST := dist
|
||||||
EXECUTABLE := act_runner
|
EXECUTABLE := gitea-runner
|
||||||
DIST_DIRS := $(DIST)/binaries $(DIST)/release
|
DIST_DIRS := $(DIST)/binaries $(DIST)/release
|
||||||
GO ?= go
|
GO ?= go
|
||||||
SHASUM ?= shasum -a 256
|
SHASUM ?= shasum -a 256
|
||||||
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
|
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
|
||||||
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
|
XGO_PACKAGE ?= src.techknowlogick.com/xgo@v1.9.0 # renovate: datasource=go
|
||||||
XGO_VERSION := go-1.26.x
|
XGO_VERSION := go-1.26.x
|
||||||
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.10
|
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.15 # renovate: datasource=go
|
||||||
|
|
||||||
LINUX_ARCHS ?= linux/amd64,linux/arm64
|
LINUX_ARCHS ?= linux/amd64,linux/arm64
|
||||||
DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64
|
DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64
|
||||||
WINDOWS_ARCHS ?= windows/amd64
|
WINDOWS_ARCHS ?= windows/amd64
|
||||||
GOFILES := $(shell find . -type f -name "*.go" -o -name "go.mod" ! -name "generated.*")
|
GOFILES := $(shell find . -type f -name "*.go" -o -name "go.mod" ! -name "generated.*")
|
||||||
|
|
||||||
DOCKER_IMAGE ?= gitea/act_runner
|
DOCKER_IMAGE ?= gitea/runner
|
||||||
DOCKER_TAG ?= nightly
|
DOCKER_TAG ?= nightly
|
||||||
DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
|
DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
|
||||||
DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
|
DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
|
||||||
|
|
||||||
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.11.4
|
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2 # renovate: datasource=go
|
||||||
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/govulncheck@v1
|
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/govulncheck@v1.3.0 # renovate: datasource=go
|
||||||
|
|
||||||
|
GOTEST_FLAGS ?= -race -timeout 20m -parallel 8
|
||||||
|
|
||||||
STATIC ?=
|
STATIC ?=
|
||||||
EXTLDFLAGS ?=
|
EXTLDFLAGS ?=
|
||||||
@@ -38,12 +40,15 @@ endif
|
|||||||
ifeq ($(OS), Windows_NT)
|
ifeq ($(OS), Windows_NT)
|
||||||
GOFLAGS := -v -buildmode=exe
|
GOFLAGS := -v -buildmode=exe
|
||||||
EXECUTABLE ?= $(EXECUTABLE).exe
|
EXECUTABLE ?= $(EXECUTABLE).exe
|
||||||
|
GO_ENV_WINDOWS := set GOOS=windows&&
|
||||||
else ifeq ($(OS), Windows)
|
else ifeq ($(OS), Windows)
|
||||||
GOFLAGS := -v -buildmode=exe
|
GOFLAGS := -v -buildmode=exe
|
||||||
EXECUTABLE ?= $(EXECUTABLE).exe
|
EXECUTABLE ?= $(EXECUTABLE).exe
|
||||||
|
GO_ENV_WINDOWS := set GOOS=windows&&
|
||||||
else
|
else
|
||||||
GOFLAGS := -v
|
GOFLAGS := -v
|
||||||
EXECUTABLE ?= $(EXECUTABLE)
|
EXECUTABLE ?= $(EXECUTABLE)
|
||||||
|
GO_ENV_WINDOWS := GOOS=windows
|
||||||
endif
|
endif
|
||||||
|
|
||||||
STORED_VERSION_FILE := VERSION
|
STORED_VERSION_FILE := VERSION
|
||||||
@@ -67,7 +72,7 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
TAGS ?=
|
TAGS ?=
|
||||||
LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=v$(RELASE_VERSION)"
|
LDFLAGS ?= -X "gitea.com/gitea/runner/internal/pkg/ver.version=v$(RELASE_VERSION)"
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: build
|
all: build
|
||||||
@@ -86,13 +91,13 @@ go-check:
|
|||||||
$(eval MIN_GO_VERSION := $(shell printf "%03d%03d" $(shell echo '$(MIN_GO_VERSION_STR)' | tr '.' ' ')))
|
$(eval MIN_GO_VERSION := $(shell printf "%03d%03d" $(shell echo '$(MIN_GO_VERSION_STR)' | tr '.' ' ')))
|
||||||
$(eval GO_VERSION := $(shell printf "%03d%03d" $(shell $(GO) version | grep -Eo '[0-9]+\.[0-9]+' | tr '.' ' ');))
|
$(eval GO_VERSION := $(shell printf "%03d%03d" $(shell $(GO) version | grep -Eo '[0-9]+\.[0-9]+' | tr '.' ' ');))
|
||||||
@if [ "$(GO_VERSION)" -lt "$(MIN_GO_VERSION)" ]; then \
|
@if [ "$(GO_VERSION)" -lt "$(MIN_GO_VERSION)" ]; then \
|
||||||
echo "Act Runner requires Go $(MIN_GO_VERSION_STR) or greater to build. You can get it at https://go.dev/dl/"; \
|
echo "Gitea Runner requires Go $(MIN_GO_VERSION_STR) or greater to build. You can get it at https://go.dev/dl/"; \
|
||||||
exit 1; \
|
exit 1; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
.PHONY: fmt-check
|
.PHONY: fmt-check
|
||||||
fmt-check: fmt
|
fmt-check: fmt
|
||||||
@diff=$$(git diff --color=always); \
|
@diff=$$(git diff --color=always -- '*.go'); \
|
||||||
if [ -n "$$diff" ]; then \
|
if [ -n "$$diff" ]; then \
|
||||||
echo "Please run 'make fmt' and commit the result:"; \
|
echo "Please run 'make fmt' and commit the result:"; \
|
||||||
printf "%s" "$${diff}"; \
|
printf "%s" "$${diff}"; \
|
||||||
@@ -107,19 +112,31 @@ deps-tools: ## install tool dependencies
|
|||||||
$(GO) install $(GOVULNCHECK_PACKAGE) & \
|
$(GO) install $(GOVULNCHECK_PACKAGE) & \
|
||||||
wait
|
wait
|
||||||
|
|
||||||
|
.PHONY: checks
|
||||||
|
checks: tidy-check fmt-check security-check ## run the non-lint source checks
|
||||||
|
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint: lint-go ## lint everything
|
lint: lint-go lint-go-windows ## lint everything
|
||||||
|
|
||||||
.PHONY: lint-go
|
.PHONY: lint-go
|
||||||
lint-go: ## lint go files
|
lint-go: ## lint go files
|
||||||
$(GO) run $(GOLANGCI_LINT_PACKAGE) run
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run
|
||||||
|
|
||||||
|
.PHONY: lint-go-windows
|
||||||
|
lint-go-windows: ## lint Windows go files
|
||||||
|
$(GO) install $(GOLANGCI_LINT_PACKAGE)
|
||||||
|
$(GO_ENV_WINDOWS) golangci-lint run
|
||||||
|
|
||||||
.PHONY: lint-go-fix
|
.PHONY: lint-go-fix
|
||||||
lint-go-fix: ## lint go files and fix issues
|
lint-go-fix: ## lint go files and fix issues
|
||||||
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --fix
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --fix
|
||||||
|
|
||||||
|
.PHONY: lint-pr-title
|
||||||
|
lint-pr-title: ## lint PR title against Conventional Commits (set PR_TITLE=...)
|
||||||
|
@node ./tools/lint-pr-title.ts
|
||||||
|
|
||||||
.PHONY: security-check
|
.PHONY: security-check
|
||||||
security-check: deps-tools
|
security-check:
|
||||||
GOEXPERIMENT= $(GO) run $(GOVULNCHECK_PACKAGE) -show color ./... || true
|
GOEXPERIMENT= $(GO) run $(GOVULNCHECK_PACKAGE) -show color ./... || true
|
||||||
|
|
||||||
.PHONY: tidy
|
.PHONY: tidy
|
||||||
@@ -136,15 +153,25 @@ tidy-check: tidy
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: fmt-check security-check ## test everything
|
test: ## test everything (integration tests self-skip without docker/network)
|
||||||
@$(GO) test -race -short -v -cover -coverprofile coverage.txt ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
@$(GO) test $(GOTEST_FLAGS) -cover -coverprofile coverage.txt ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||||
|
|
||||||
|
.PHONY: coverage-report
|
||||||
|
coverage-report: ## turn coverage.txt from `make test` into .tmp/coverage.md
|
||||||
|
@mkdir -p .tmp
|
||||||
|
@node ./tools/coverage-report.ts -i coverage.txt -o .tmp/coverage.md
|
||||||
|
@echo "Wrote .tmp/coverage.md"
|
||||||
|
|
||||||
|
.PHONY: test-dind
|
||||||
|
test-dind: ## run the daemon-facing tests against the built dind image (TARGET=dind|dind-rootless)
|
||||||
|
@./scripts/test-dind.sh $(TARGET)
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: $(GOFILES) ## install the act_runner binary via `go install`
|
install: $(GOFILES) ## install the runner binary via `go install`
|
||||||
$(GO) install -v -tags '$(TAGS)' -ldflags '-s -w $(EXTLDFLAGS) $(LDFLAGS)'
|
$(GO) install -v -tags '$(TAGS)' -ldflags '-s -w $(EXTLDFLAGS) $(LDFLAGS)'
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build: go-check $(EXECUTABLE) ## build the act_runner binary
|
build: go-check $(EXECUTABLE) ## build the runner binary
|
||||||
|
|
||||||
$(EXECUTABLE): $(GOFILES)
|
$(EXECUTABLE): $(GOFILES)
|
||||||
$(GO) build -v -tags '$(TAGS)' -ldflags '-s -w $(EXTLDFLAGS) $(LDFLAGS)' -o $@
|
$(GO) build -v -tags '$(TAGS)' -ldflags '-s -w $(EXTLDFLAGS) $(LDFLAGS)' -o $@
|
||||||
@@ -202,7 +229,7 @@ docker: ## build the docker image
|
|||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean: ## delete binary and coverage files
|
clean: ## delete binary and coverage files
|
||||||
$(GO) clean -x -i ./...
|
$(GO) clean -x -i ./...
|
||||||
rm -rf coverage.txt $(EXECUTABLE) $(DIST)
|
rm -rf coverage.txt .tmp $(EXECUTABLE) $(DIST)
|
||||||
|
|
||||||
.PHONY: version
|
.PHONY: version
|
||||||
version: ## print the version
|
version: ## print the version
|
||||||
|
|||||||
248
README.md
248
README.md
@@ -1,6 +1,4 @@
|
|||||||
# act runner
|
# Gitea Runner
|
||||||
|
|
||||||
Act runner is a runner for Gitea.
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -10,7 +8,7 @@ Docker Engine Community version is required for docker mode. To install Docker C
|
|||||||
|
|
||||||
### Download pre-built binary
|
### Download pre-built binary
|
||||||
|
|
||||||
Visit [here](https://dl.gitea.com/act_runner/) and download the right version for your platform.
|
Visit [here](https://dl.gitea.com/gitea-runner/) and download the right version for your platform.
|
||||||
|
|
||||||
### Build from source
|
### Build from source
|
||||||
|
|
||||||
@@ -36,7 +34,7 @@ ENABLED=true
|
|||||||
### Register
|
### Register
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./act_runner register
|
./gitea-runner register
|
||||||
```
|
```
|
||||||
|
|
||||||
And you will be asked to input:
|
And you will be asked to input:
|
||||||
@@ -68,7 +66,7 @@ INFO Runner registered successfully.
|
|||||||
You can also register with command line arguments.
|
You can also register with command line arguments.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./act_runner register --instance http://192.168.8.8:3000 --token <my_runner_token> --no-interactive
|
./gitea-runner register --instance http://192.168.8.8:3000 --token <my_runner_token> --no-interactive
|
||||||
```
|
```
|
||||||
|
|
||||||
If the registry succeed, it will run immediately. Next time, you could run the runner directly.
|
If the registry succeed, it will run immediately. Next time, you could run the runner directly.
|
||||||
@@ -76,32 +74,252 @@ If the registry succeed, it will run immediately. Next time, you could run the r
|
|||||||
### Run
|
### Run
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./act_runner daemon
|
./gitea-runner daemon
|
||||||
```
|
```
|
||||||
|
|
||||||
### Run with docker
|
### Run with docker
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -e GITEA_INSTANCE_URL=https://your_gitea.com -e GITEA_RUNNER_REGISTRATION_TOKEN=<your_token> -v /var/run/docker.sock:/var/run/docker.sock --name my_runner gitea/act_runner:nightly
|
docker run -e GITEA_INSTANCE_URL=https://your_gitea.com -e GITEA_RUNNER_REGISTRATION_TOKEN=<your_token> -v /var/run/docker.sock:/var/run/docker.sock --name my_runner gitea/runner:nightly
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Mount a volume on `/data` if you want the registration file and optional config to survive container recreation (see [scripts/run.sh](scripts/run.sh)).
|
||||||
|
|
||||||
|
> **`/data` does not hold the image cache.** It is the runner's working directory and contains only the `.runner` registration file and, optionally, your config file. Images pulled for jobs live in the *Docker daemon's* data root, which for the `dind` flavours is inside the container (`/var/lib/docker`, or `/home/rootless/.local/share/docker` for `dind-rootless`). To keep the image cache across restarts, give that path its own volume as well — otherwise every new container re-pulls the job images. With the `basic` flavour the images live on whichever daemon you point the runner at, so there is nothing extra to persist.
|
||||||
|
|
||||||
|
### Image flavours
|
||||||
|
|
||||||
|
The image is published in three flavours, all built from the single multi-stage [Dockerfile](Dockerfile) in this repository. They differ only in how a Docker daemon is made available to the jobs the runner executes; the `gitea-runner` binary inside them is identical.
|
||||||
|
|
||||||
|
| Tag | Build target | Base image | Docker daemon | Process supervisor | Runs as |
|
||||||
|
| --- | --- | --- | --- | --- | --- |
|
||||||
|
| `latest` (and `<version>`) | `basic` | `alpine` | none — uses an external daemon you provide | [`tini`](https://github.com/krallin/tini) | `root` |
|
||||||
|
| `latest-dind` | `dind` | `docker:dind` | bundled, started inside the container | [`s6`](https://skarnet.org/software/s6/) | `root` (privileged) |
|
||||||
|
| `latest-dind-rootless` | `dind-rootless` | `docker:dind-rootless` | bundled, started rootless inside the container | [`s6`](https://skarnet.org/software/s6/) | `rootless` (UID 1000) |
|
||||||
|
|
||||||
|
#### `latest` — basic
|
||||||
|
|
||||||
|
The default flavour ships only the runner on a minimal Alpine base. It contains **no Docker daemon of its own**: jobs that use `docker://` images need a daemon supplied from outside the container, typically by bind-mounting the host's socket:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -e GITEA_INSTANCE_URL=https://your_gitea.com -e GITEA_RUNNER_REGISTRATION_TOKEN=<your_token> \
|
||||||
|
-v /var/run/docker.sock:/var/run/docker.sock --name my_runner gitea/runner:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
`tini` is the entrypoint (it reaps zombie processes), and it just runs [`scripts/run.sh`](scripts/run.sh), which registers the runner on first start and then execs `gitea-runner daemon`. This flavour does not need `--privileged`. The trade-off is that jobs share the host's daemon, so they can see other containers and images on that daemon.
|
||||||
|
|
||||||
|
#### `latest-dind` — Docker-in-Docker
|
||||||
|
|
||||||
|
This flavour is based on the official `docker:dind` image and bundles its own Docker daemon, so it needs no external socket — only the `--privileged` flag that Docker-in-Docker requires:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --privileged -e GITEA_INSTANCE_URL=https://your_gitea.com -e GITEA_RUNNER_REGISTRATION_TOKEN=<your_token> \
|
||||||
|
--name my_runner gitea/runner:latest-dind
|
||||||
|
```
|
||||||
|
|
||||||
|
Two processes have to run side by side here (the Docker daemon and the runner), so the entrypoint is the [`s6`](https://skarnet.org/software/s6/) supervision tree under [`scripts/s6`](scripts/s6) instead of `tini`. `s6` starts `dockerd`, and the runner service waits for the daemon to come up (`s6-svwait`) before launching [`run.sh`](scripts/run.sh). Each container has a private daemon isolated from the host's, at the cost of running privileged.
|
||||||
|
|
||||||
|
#### `latest-dind-rootless` — rootless Docker-in-Docker
|
||||||
|
|
||||||
|
Same idea as `dind`, but built on `docker:dind-rootless` so the bundled daemon and the runner run as an unprivileged user (`rootless`, UID 1000) rather than `root`. `DOCKER_HOST` is preset to `unix:///run/user/1000/docker.sock` so the runner talks to the rootless daemon. This reduces the blast radius compared to the privileged `dind` flavour, but rootless Docker carries the usual rootless limitations (networking, cgroups, storage drivers, and some operations that need additional host configuration such as `/etc/subuid` / `/etc/subgid` mappings and unprivileged user-namespace support).
|
||||||
|
|
||||||
|
> **The UID is fixed at 1000.** It comes from the `rootless` user baked into the upstream `docker:dind-rootless` base image, and the bundled daemon always listens on `/run/user/1000/docker.sock` inside the container, so running this flavour as a different user (`--user 1001`) does not work. If you need the runner to talk to a *host* rootless daemon that runs under some other UID, use the `basic` flavour instead and bind-mount that daemon's socket (see [examples/vm/rootless-docker.md](examples/vm/rootless-docker.md)); pointing `DOCKER_HOST` at a host socket from inside `dind-rootless` will not work. Changing the UID otherwise means rebuilding the image from a base with a different `rootless` user.
|
||||||
|
|
||||||
|
> **Note on Podman:** these images target the Docker daemon. The bundled `dind`/`dind-rootless` daemons are `dockerd`, not Podman, and the `basic` flavour expects a Docker-compatible socket. Running them under rootless Podman is not a supported configuration, though pointing the `basic` flavour at a Podman socket that emulates the Docker API may work for some workloads.
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
You can also configure the runner with a configuration file.
|
The runner is configured with a YAML file. Generate a starting point (this matches what ships in the tree):
|
||||||
The configuration file is a YAML file, you can generate a sample configuration file with `./act_runner generate-config`.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./act_runner generate-config > config.yaml
|
./gitea-runner generate-config > config.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
You can specify the configuration file path with `-c`/`--config` argument.
|
Pass it with `-c` / `--config` on any command that loads configuration (`register`, `daemon`, `cache-server`):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./act_runner -c config.yaml register # register with config file
|
./gitea-runner -c config.yaml register
|
||||||
./act_runner -c config.yaml daemon # run with config file
|
./gitea-runner -c config.yaml daemon
|
||||||
|
./gitea-runner -c config.yaml cache-server
|
||||||
```
|
```
|
||||||
|
|
||||||
You can read the latest version of the configuration file online at [config.example.yaml](internal/pkg/config/config.example.yaml).
|
Every option is described in [config.example.yaml](internal/pkg/config/config.example.yaml) (the same content `generate-config` prints).
|
||||||
|
|
||||||
|
#### Without a config file
|
||||||
|
|
||||||
|
If you omit `-c`, built-in defaults apply (same as an empty YAML document).
|
||||||
|
|
||||||
|
Earlier releases let a small set of environment variables (`GITEA_DEBUG`, `GITEA_TRACE`, `GITEA_RUNNER_CAPACITY`, `GITEA_RUNNER_FILE`, `GITEA_RUNNER_ENVIRON`, `GITEA_RUNNER_ENV_FILE`) override parts of the default config. Those overrides have been removed — use a YAML config file for all settings instead. For the Docker images, the entrypoint still understands a separate set of variables (such as `RUNNER_STATE_FILE`); see [scripts/run.sh](scripts/run.sh) and the container documentation below.
|
||||||
|
|
||||||
|
### Labels
|
||||||
|
|
||||||
|
Labels decide **which jobs a runner accepts** and **how it runs them**. A job's `runs-on` is matched against the runner's label names; the first match wins and selects the execution environment for that job.
|
||||||
|
|
||||||
|
A label is written as:
|
||||||
|
|
||||||
|
```text
|
||||||
|
<name>[:<schema>[:<args>]]
|
||||||
|
```
|
||||||
|
|
||||||
|
| Part | Meaning |
|
||||||
|
| --- | --- |
|
||||||
|
| `name` | The name a workflow refers to in `runs-on`, e.g. `ubuntu-latest`. |
|
||||||
|
| `schema` | Either `docker` or `host`. Defaults to `host` when omitted. |
|
||||||
|
| `args` | Only used by the `docker` schema: the image to run the job in. |
|
||||||
|
|
||||||
|
Two schemas are supported:
|
||||||
|
|
||||||
|
- **`docker://<image>`** — the job runs inside a container created from `<image>`:
|
||||||
|
|
||||||
|
```text
|
||||||
|
ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest
|
||||||
|
```
|
||||||
|
|
||||||
|
- **`host`** — the job's steps run directly on the machine the runner is on, using the tools installed there:
|
||||||
|
|
||||||
|
```text
|
||||||
|
macos:host
|
||||||
|
```
|
||||||
|
|
||||||
|
So with the labels
|
||||||
|
|
||||||
|
```text
|
||||||
|
ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest,macos:host
|
||||||
|
```
|
||||||
|
|
||||||
|
a workflow with `runs-on: ubuntu-latest` is executed in the `runner-images:ubuntu-latest` container, and one with `runs-on: macos` is executed directly on the host.
|
||||||
|
|
||||||
|
Names may themselves contain a colon (for example `pool:e57e18d4-10d4-406f-93bf-60f127221bdd`); only `host` and `docker` are treated as schemas.
|
||||||
|
|
||||||
|
If a job's `runs-on` matches none of the runner's labels, the job still runs, in the default `docker.gitea.com/runner-images:ubuntu-latest` image. Images maintained for this purpose are listed at [gitea/runner-images](https://gitea.com/gitea/runner-images).
|
||||||
|
|
||||||
|
Labels are chosen at registration time (`--labels`, or the interactive prompt) and can be changed afterwards by editing `runner.labels` in the config file, or in the Gitea UI under the runner's settings.
|
||||||
|
|
||||||
|
#### Registration vs config labels
|
||||||
|
|
||||||
|
If `runner.labels` is set in the YAML file, those labels are used during `register` and the `--labels` CLI flag is ignored.
|
||||||
|
|
||||||
|
The `daemon` command also accepts `--labels` (which defaults to the `GITEA_RUNNER_LABELS` environment variable), so the labels of an already registered runner can be changed without deleting its registration file. The most explicit source wins:
|
||||||
|
|
||||||
|
```
|
||||||
|
--labels / GITEA_RUNNER_LABELS > runner.labels in the config file > labels in the .runner file
|
||||||
|
```
|
||||||
|
|
||||||
|
Whenever the resulting labels differ from the ones in the registration file, they are written back to it and re-declared to the Gitea instance on startup.
|
||||||
|
|
||||||
|
> **Note:** A runner that only exposes `host` labels still needs access to a Docker daemon (e.g. a mounted `/var/run/docker.sock`) whenever a job uses a `docker://` action or a service container. `host` labels only change where the job's own steps run; container-based steps and actions are still executed with Docker.
|
||||||
|
|
||||||
|
#### Proxy
|
||||||
|
|
||||||
|
Set these variables in the runner's environment, with systemd `Environment=`, `docker run -e`, or Kubernetes `env:`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
http_proxy=http://proxy.example:3128
|
||||||
|
https_proxy=http://proxy.example:3128
|
||||||
|
no_proxy=gitea.internal,.example.local
|
||||||
|
```
|
||||||
|
|
||||||
|
The runner uses them for its own requests and gives them to every job, in lower and upper case.
|
||||||
|
|
||||||
|
These hosts are added to `no_proxy` for jobs, so they are always reached directly:
|
||||||
|
|
||||||
|
- the cache server
|
||||||
|
- `localhost`, `127.0.0.1` and `::1`
|
||||||
|
- the job's service containers
|
||||||
|
- the Docker daemon, when it is reached over `tcp://`
|
||||||
|
|
||||||
|
Gitea is not added. Add it to `no_proxy` yourself if it should be reached directly.
|
||||||
|
|
||||||
|
To change a value for one job, set it in a step's `env:` or in the job's `container.env`. Setting it at workflow or job level has no effect. To change it for the whole runner, set it in `runner.envs`. A `no_proxy` set there is added to the list above instead of replacing it.
|
||||||
|
|
||||||
|
Images are pulled by the Docker daemon, which needs its own proxy setting. In the `dind` images the daemon runs in the same container and reads the variables above. For any other daemon, see [the Docker documentation](https://docs.docker.com/engine/daemon/proxy/). The runner logs a warning at startup if it has a proxy and the daemon does not.
|
||||||
|
|
||||||
|
Dockerfile actions are built with these variables as build arguments, so their `RUN` steps can reach the network.
|
||||||
|
|
||||||
|
A password in a proxy URL is hidden in job logs. Any step can still read it, because the step is given the proxy URL in its environment.
|
||||||
|
|
||||||
|
#### Caching (`actions/cache`)
|
||||||
|
|
||||||
|
Each runner starts its own cache server automatically. Cache entries are local to that runner — runners do not share a cache by default.
|
||||||
|
|
||||||
|
**Cache service v2**
|
||||||
|
|
||||||
|
`actions/cache@v4.2` and later can use the *cache service v2* API. The runner serves it from the same store as v1, on by default, and it works with `external_server`. Turn it off with:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cache:
|
||||||
|
v2: false
|
||||||
|
```
|
||||||
|
|
||||||
|
Those actions refuse any host they do not take for GitHub. Rather than misreport the server URL, the runner edits that check out of the action's own bundle and keeps the untouched copy beside it; a bundle it does not recognise is left alone and keeps to v1. The same edit lets the stock `actions/upload-artifact` and `actions/download-artifact` work from `v4.4.0` on, without the `gitea-upload-artifact` fork.
|
||||||
|
|
||||||
|
**Shared cache across multiple runners**
|
||||||
|
|
||||||
|
Run one dedicated `gitea-runner cache-server` that all runners point at.
|
||||||
|
|
||||||
|
1. Create a config file for the cache server host:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cache:
|
||||||
|
dir: /data/actcache
|
||||||
|
port: 8088
|
||||||
|
external_secret: "replace-with-a-strong-random-secret"
|
||||||
|
# external_secret_file: /path/to/secret # secret can also be passed via a file
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Start the server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
gitea-runner -c cache-server-config.yaml cache-server
|
||||||
|
```
|
||||||
|
|
||||||
|
3. On every runner:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cache:
|
||||||
|
external_server: "http://<cache-server-host>:8088/"
|
||||||
|
external_secret: "replace-with-a-strong-random-secret" # must match the server
|
||||||
|
# external_secret_file: /path/to/secret # secret can also be passed via a file
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, mount the same NFS/CIFS share on every runner and point `cache.dir` at it — simpler, but with weaker isolation between repositories.
|
||||||
|
|
||||||
|
**S3 / MinIO** — mount object storage as a FUSE filesystem (e.g. [s3fs](https://github.com/s3fs-fuse/s3fs-fuse) or [goofys](https://github.com/kahing/goofys)) and set `cache.dir` to the mount point.
|
||||||
|
|
||||||
|
Flags `--dir`, `--host`, and `--port` on `cache-server` override the corresponding `cache.*` YAML keys; all other settings, including `external_secret`, require the config file.
|
||||||
|
|
||||||
|
#### Official Docker image
|
||||||
|
|
||||||
|
Besides `GITEA_INSTANCE_URL` and `GITEA_RUNNER_REGISTRATION_TOKEN`, the image entrypoint supports optional variables such as `CONFIG_FILE` (passed through as `-c`), `GITEA_RUNNER_LABELS`, `GITEA_RUNNER_EPHEMERAL`, `GITEA_RUNNER_ONCE`, `GITEA_RUNNER_NAME`, `GITEA_MAX_REG_ATTEMPTS`, `RUNNER_STATE_FILE`, and `GITEA_RUNNER_REGISTRATION_TOKEN_FILE`. See [scripts/run.sh](scripts/run.sh) for exact behavior.
|
||||||
|
|
||||||
|
For a fuller container-oriented walkthrough, see [examples/docker](examples/docker/README.md).
|
||||||
|
|
||||||
|
While the runner is idle it cleans up after earlier jobs:
|
||||||
|
- when `container.bind_workdir` is enabled, stale task workspace directories older than `runner.workdir_cleanup_age` are removed (default: `24h`; set `0` to disable)
|
||||||
|
- only purely numeric subdirectories under `container.workdir_parent` are treated as task workspaces and may be removed
|
||||||
|
- cleanup assumes `container.workdir_parent` is not shared across multiple runners
|
||||||
|
- on runners that use docker, per-job networks left behind by jobs the runner did not live to tear down are removed, identified by the `com.gitea.runner.uuid` label carrying this runner's uuid
|
||||||
|
- cleanup runs every `runner.idle_cleanup_interval` (default: `10m`; set `0` to disable), and setting either knob to `0` disables all of the above
|
||||||
|
|
||||||
|
#### Post-task script (`runner.post_task_script`)
|
||||||
|
|
||||||
|
Optional host script that runs **after** each task's built-in cleanup (post-steps, container teardown, bind-workdir removal). Use it for extra machine housekeeping — Docker pruning, disk cleanup, and similar.
|
||||||
|
|
||||||
|
**While the script runs, the runner stops task heartbeats and stays offline from Gitea's perspective until the script exits (or hits `runner.post_task_script_timeout`, default `5m`).** A script that blocks without exiting keeps the runner from taking new work for up to that timeout. Script output goes to the runner log, not the job log; a non-zero exit is warned but does not change the job result.
|
||||||
|
|
||||||
|
On Windows, use `.exe`, `.bat`, or `.cmd` paths; **PowerShell (`.ps1`) is not supported yet** as the configured path — wrap commands in a `.cmd` file instead.
|
||||||
|
|
||||||
|
See **[docs/post-task-script.md](docs/post-task-script.md)** for lifecycle details, environment variables, timeout interaction, and platform notes.
|
||||||
|
|
||||||
|
#### Job hooks (`runner.hooks.job_started`, `runner.hooks.job_completed`)
|
||||||
|
|
||||||
|
Optional scripts that run **inside the job environment** (the job container, or the host in host mode), before the job's first step and after its last one. They are the equivalent of GitHub's `ACTIONS_RUNNER_HOOK_JOB_STARTED` / `ACTIONS_RUNNER_HOOK_JOB_COMPLETED`, which are read when the settings are unset.
|
||||||
|
|
||||||
|
Because they run where the steps run and see the job's environment, they are the place for per-job setup no workflow should have to carry: registry logins, mirror configuration, or masking runner-wide secrets with `::add-mask::`. Their output is part of the job log and is scanned for workflow commands, and they can export to the job through `$GITHUB_ENV` and `$GITHUB_PATH`.
|
||||||
|
|
||||||
|
Both hooks are synchronous and block the job while they run. Either one exiting non-zero fails the job, and there is no per-hook timeout.
|
||||||
|
|
||||||
|
See **[docs/job-hooks.md](docs/job-hooks.md)** for the execution order, environment, and platform notes.
|
||||||
|
|
||||||
### Example Deployments
|
### Example Deployments
|
||||||
|
|
||||||
|
|||||||
@@ -5,21 +5,28 @@
|
|||||||
package artifactcache
|
package artifactcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -28,14 +35,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
urlBase = "/_apis/artifactcache"
|
apiPath = "/_apis/artifactcache"
|
||||||
|
internalPath = "/_internal"
|
||||||
|
|
||||||
|
// artifactURLTTL bounds how long a signed artifactLocation URL stays valid.
|
||||||
|
// Short enough that a leaked URL is near-worthless; long enough to let the
|
||||||
|
// @actions/cache client download a big blob that was returned from /cache.
|
||||||
|
artifactURLTTL = 10 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type credKey struct{}
|
||||||
|
|
||||||
|
// JobCredential ties a per-job bearer token (ACTIONS_RUNTIME_TOKEN) to the
|
||||||
|
// repository that owns it. Every cache entry is stamped with Repo on
|
||||||
|
// reserve/commit and checked on read/write so one repo can never observe or
|
||||||
|
// poison another repo's cache, even from inside a container that reaches the
|
||||||
|
// cache server over the docker bridge network.
|
||||||
|
type JobCredential struct {
|
||||||
|
Repo string
|
||||||
|
}
|
||||||
|
|
||||||
|
// credEntry holds a registered job's credential along with an active
|
||||||
|
// registration count. RegisterJob is reference-counted so that if two tasks
|
||||||
|
// briefly share an ACTIONS_RUNTIME_TOKEN — e.g. a runner that retries a task
|
||||||
|
// after a crash before the old registration is revoked — the first task's
|
||||||
|
// revoker does not cut the second task's auth out from under it.
|
||||||
|
type credEntry struct {
|
||||||
|
cred JobCredential
|
||||||
|
refs int
|
||||||
|
}
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
dir string
|
dir string
|
||||||
storage *Storage
|
storage *Storage
|
||||||
router *httprouter.Router
|
router *httprouter.Router
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
|
port int
|
||||||
server *http.Server
|
server *http.Server
|
||||||
logger logrus.FieldLogger
|
logger logrus.FieldLogger
|
||||||
|
|
||||||
@@ -43,10 +78,36 @@ type Handler struct {
|
|||||||
gcAt time.Time
|
gcAt time.Time
|
||||||
|
|
||||||
outboundIP string
|
outboundIP string
|
||||||
|
|
||||||
|
// internalSecret guards /_internal/{register,revoke}. When set, a remote
|
||||||
|
// runner can use these endpoints to pre-register per-job
|
||||||
|
// ACTIONS_RUNTIME_TOKENs against this server, enabling the same
|
||||||
|
// per-job auth and repo scoping as the embedded handler over the
|
||||||
|
// network. Empty disables the control-plane entirely.
|
||||||
|
internalSecret string
|
||||||
|
|
||||||
|
// secret signs short-lived artifact download URLs. The @actions/cache
|
||||||
|
// toolkit does not send Authorization on the download request, so blob
|
||||||
|
// GETs authenticate via a per-URL HMAC signature with expiry rather than
|
||||||
|
// via the bearer token used for management endpoints.
|
||||||
|
secret []byte
|
||||||
|
|
||||||
|
credMu sync.RWMutex
|
||||||
|
creds map[string]*credEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
|
// StartHandler opens the on-disk cache store and starts the HTTP server.
|
||||||
h := &Handler{}
|
//
|
||||||
|
// internalSecret, when non-empty, enables a control-plane API at
|
||||||
|
// /_internal/{register,revoke} that lets a remote runner pre-register the
|
||||||
|
// per-job ACTIONS_RUNTIME_TOKENs it expects this server to honor. The
|
||||||
|
// embedded in-process handler leaves it empty and registers tokens via the
|
||||||
|
// in-process RegisterJob method directly.
|
||||||
|
func StartHandler(dir, outboundIP string, port uint16, internalSecret string, logger logrus.FieldLogger) (*Handler, error) {
|
||||||
|
h := &Handler{
|
||||||
|
creds: make(map[string]*credEntry),
|
||||||
|
internalSecret: internalSecret,
|
||||||
|
}
|
||||||
|
|
||||||
if logger == nil {
|
if logger == nil {
|
||||||
discard := logrus.New()
|
discard := logrus.New()
|
||||||
@@ -83,22 +144,47 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
|
|||||||
h.outboundIP = ip.String()
|
h.outboundIP = ip.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
secret, err := loadOrCreateSecret(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
h.secret = secret
|
||||||
|
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
router.GET(urlBase+"/cache", h.middleware(h.find))
|
router.GET(apiPath+"/cache", h.bearerAuth(h.find))
|
||||||
router.POST(urlBase+"/caches", h.middleware(h.reserve))
|
router.POST(apiPath+"/caches", h.bearerAuth(h.reserve))
|
||||||
router.PATCH(urlBase+"/caches/:id", h.middleware(h.upload))
|
router.PATCH(apiPath+"/caches/:id", h.bearerAuth(h.upload))
|
||||||
router.POST(urlBase+"/caches/:id", h.middleware(h.commit))
|
router.POST(apiPath+"/caches/:id", h.bearerAuth(h.commit))
|
||||||
router.GET(urlBase+"/artifacts/:id", h.middleware(h.get))
|
router.POST(apiPath+"/clean", h.bearerAuth(h.clean))
|
||||||
router.POST(urlBase+"/clean", h.middleware(h.clean))
|
// Artifact GET is signed via query-string HMAC because @actions/cache
|
||||||
|
// does not attach Authorization when downloading archiveLocation.
|
||||||
|
router.GET(apiPath+"/artifacts/:id", h.signedAuth("", h.get))
|
||||||
|
// Control-plane: a remote runner registers/revokes per-job tokens so the
|
||||||
|
// cache API can authenticate them. Always wired so the routes exist; the
|
||||||
|
// handlers themselves 401 when internalSecret is unset.
|
||||||
|
router.POST(internalPath+"/register", h.internalAuth(h.internalRegister))
|
||||||
|
router.POST(internalPath+"/revoke", h.internalAuth(h.internalRevoke))
|
||||||
|
h.registerV2Routes(router)
|
||||||
|
|
||||||
h.router = router
|
h.router = router
|
||||||
|
|
||||||
h.gcCache()
|
h.gcCache()
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) // listen on all interfaces
|
// Listen on all interfaces. Binding to outboundIP only would give no real
|
||||||
|
// security benefit (it is the LAN/internet-facing address either way) and
|
||||||
|
// can break Docker Desktop variants where the host's outbound IP is not
|
||||||
|
// routable from inside the container network. Authentication is enforced
|
||||||
|
// by the bearer middleware and per-repo scoping, not by reachability.
|
||||||
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
addr, ok := listener.Addr().(*net.TCPAddr)
|
||||||
|
if !ok {
|
||||||
|
listener.Close()
|
||||||
|
return nil, fmt.Errorf("cache server listens on %T, want a TCP address", listener.Addr())
|
||||||
|
}
|
||||||
|
h.port = addr.Port
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
ReadHeaderTimeout: 2 * time.Second,
|
ReadHeaderTimeout: 2 * time.Second,
|
||||||
Handler: router,
|
Handler: router,
|
||||||
@@ -116,9 +202,92 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
|
|||||||
|
|
||||||
func (h *Handler) ExternalURL() string {
|
func (h *Handler) ExternalURL() string {
|
||||||
// TODO: make the external url configurable if necessary
|
// TODO: make the external url configurable if necessary
|
||||||
return fmt.Sprintf("http://%s:%d",
|
return fmt.Sprintf("http://%s:%d", h.outboundIP, h.port)
|
||||||
h.outboundIP,
|
}
|
||||||
h.listener.Addr().(*net.TCPAddr).Port)
|
|
||||||
|
// RegisterJob makes token a valid bearer credential for cache requests from
|
||||||
|
// the given repository and returns a function that removes it. The runner
|
||||||
|
// calls this at job start and defers the returned func so that the credential
|
||||||
|
// is only accepted while the job is running.
|
||||||
|
//
|
||||||
|
// Registrations are reference-counted: if a token is already registered, the
|
||||||
|
// existing repo is kept and the refcount is incremented. The entry is
|
||||||
|
// removed only when every revoker returned by RegisterJob has been called.
|
||||||
|
// This keeps a stray re-registration from silently revoking a live job.
|
||||||
|
func (h *Handler) RegisterJob(token, repo string) func() {
|
||||||
|
if h == nil || token == "" {
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
h.credMu.Lock()
|
||||||
|
if existing, ok := h.creds[token]; ok {
|
||||||
|
existing.refs++
|
||||||
|
} else {
|
||||||
|
h.creds[token] = &credEntry{
|
||||||
|
cred: JobCredential{Repo: repo},
|
||||||
|
refs: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.credMu.Unlock()
|
||||||
|
return func() {
|
||||||
|
h.credMu.Lock()
|
||||||
|
if entry, ok := h.creds[token]; ok {
|
||||||
|
entry.refs--
|
||||||
|
if entry.refs <= 0 {
|
||||||
|
delete(h.creds, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.credMu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RevokeJob explicitly revokes one registration of token, mirroring one call
|
||||||
|
// of the closure returned by RegisterJob. Used by the control-plane endpoint
|
||||||
|
// so a remote runner can revoke without holding the closure.
|
||||||
|
func (h *Handler) RevokeJob(token string) {
|
||||||
|
if h == nil || token == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.credMu.Lock()
|
||||||
|
if entry, ok := h.creds[token]; ok {
|
||||||
|
entry.refs--
|
||||||
|
if entry.refs <= 0 {
|
||||||
|
delete(h.creds, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.credMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) lookupCredential(token string) (JobCredential, bool) {
|
||||||
|
h.credMu.RLock()
|
||||||
|
entry, ok := h.creds[token]
|
||||||
|
h.credMu.RUnlock()
|
||||||
|
if !ok {
|
||||||
|
return JobCredential{}, false
|
||||||
|
}
|
||||||
|
return entry.cred, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadOrCreateSecret returns the 32-byte HMAC signing key for artifact URLs,
|
||||||
|
// persisted in dir/.secret so signed URLs handed out before a restart stay
|
||||||
|
// valid across the restart and so the standalone cache-server can be pointed
|
||||||
|
// at by config.Cache.ExternalServer without the URL rotating.
|
||||||
|
func loadOrCreateSecret(dir string) ([]byte, error) {
|
||||||
|
path := filepath.Join(dir, ".secret")
|
||||||
|
if data, err := os.ReadFile(path); err == nil {
|
||||||
|
if secret, err := hex.DecodeString(strings.TrimSpace(string(data))); err == nil && len(secret) >= 32 {
|
||||||
|
return secret, nil
|
||||||
|
}
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("read cache secret: %w", err)
|
||||||
|
}
|
||||||
|
secret := make([]byte, 32)
|
||||||
|
if _, err := rand.Read(secret); err != nil {
|
||||||
|
return nil, fmt.Errorf("generate cache secret: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, []byte(hex.EncodeToString(secret)), 0o600); err != nil {
|
||||||
|
return nil, fmt.Errorf("write cache secret: %w", err)
|
||||||
|
}
|
||||||
|
return secret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) Close() error {
|
func (h *Handler) Close() error {
|
||||||
@@ -160,11 +329,8 @@ func (h *Handler) openDB() (*bolthold.Store, error) {
|
|||||||
|
|
||||||
// GET /_apis/artifactcache/cache
|
// GET /_apis/artifactcache/cache
|
||||||
func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
keys := strings.Split(r.URL.Query().Get("keys"), ",")
|
keys := strings.Split(r.URL.Query().Get("keys"), ",")
|
||||||
// cache keys are case insensitive
|
|
||||||
for i, key := range keys {
|
|
||||||
keys[i] = strings.ToLower(key)
|
|
||||||
}
|
|
||||||
version := r.URL.Query().Get("version")
|
version := r.URL.Query().Get("version")
|
||||||
|
|
||||||
db, err := h.openDB()
|
db, err := h.openDB()
|
||||||
@@ -174,7 +340,7 @@ func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
cache, err := findCache(db, keys, version)
|
cache, err := h.lookupCache(db, cred.Repo, keys, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.responseJSON(w, r, 500, err)
|
h.responseJSON(w, r, 500, err)
|
||||||
return
|
return
|
||||||
@@ -183,33 +349,43 @@ func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
|||||||
h.responseJSON(w, r, 204)
|
h.responseJSON(w, r, 204)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok, err := h.storage.Exist(cache.ID); err != nil {
|
|
||||||
h.responseJSON(w, r, 500, err)
|
|
||||||
return
|
|
||||||
} else if !ok {
|
|
||||||
_ = db.Delete(cache.ID, cache)
|
|
||||||
h.responseJSON(w, r, 204)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.responseJSON(w, r, 200, map[string]any{
|
h.responseJSON(w, r, 200, map[string]any{
|
||||||
"result": "hit",
|
"result": "hit",
|
||||||
"archiveLocation": fmt.Sprintf("%s%s/artifacts/%d", h.ExternalURL(), urlBase, cache.ID),
|
"archiveLocation": h.signedArtifactURL(cache.ID, time.Now().Add(artifactURLTTL)),
|
||||||
"cacheKey": cache.Key,
|
"cacheKey": cache.Key,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lookupCache returns the entry to restore for these keys, or (nil, nil) when there is none:
|
||||||
|
// either nothing matched, or the match had lost its blob to a prune, in which case the dangling
|
||||||
|
// entry is dropped on the way out.
|
||||||
|
func (h *Handler) lookupCache(db *bolthold.Store, repo string, keys []string, version string) (*Cache, error) {
|
||||||
|
cache, err := findCache(db, repo, keys, version)
|
||||||
|
if err != nil || cache == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ok, err := h.storage.Exist(cache.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
_ = db.Delete(cache.ID, cache)
|
||||||
|
return nil, nil //nolint:nilnil // absence is not an error here
|
||||||
|
}
|
||||||
|
return cache, nil
|
||||||
|
}
|
||||||
|
|
||||||
// POST /_apis/artifactcache/caches
|
// POST /_apis/artifactcache/caches
|
||||||
func (h *Handler) reserve(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (h *Handler) reserve(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
api := &Request{}
|
api := &Request{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(api); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(api); err != nil {
|
||||||
h.responseJSON(w, r, 400, err)
|
h.responseJSON(w, r, 400, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// cache keys are case insensitive
|
|
||||||
api.Key = strings.ToLower(api.Key)
|
|
||||||
|
|
||||||
cache := api.ToCache()
|
cache := api.ToCache()
|
||||||
|
cache.Repo = cred.Repo
|
||||||
db, err := h.openDB()
|
db, err := h.openDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.responseJSON(w, r, 500, err)
|
h.responseJSON(w, r, 500, err)
|
||||||
@@ -231,6 +407,7 @@ func (h *Handler) reserve(w http.ResponseWriter, r *http.Request, _ httprouter.P
|
|||||||
|
|
||||||
// PATCH /_apis/artifactcache/caches/:id
|
// PATCH /_apis/artifactcache/caches/:id
|
||||||
func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.responseJSON(w, r, 400, err)
|
h.responseJSON(w, r, 400, err)
|
||||||
@@ -253,6 +430,11 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprout
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cache.Repo != cred.Repo {
|
||||||
|
h.responseJSON(w, r, 403, fmt.Errorf("cache %d: forbidden", id))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if cache.Complete {
|
if cache.Complete {
|
||||||
h.responseJSON(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
h.responseJSON(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
||||||
return
|
return
|
||||||
@@ -265,13 +447,15 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprout
|
|||||||
}
|
}
|
||||||
if err := h.storage.Write(cache.ID, start, r.Body); err != nil {
|
if err := h.storage.Write(cache.ID, start, r.Body); err != nil {
|
||||||
h.responseJSON(w, r, 500, err)
|
h.responseJSON(w, r, 500, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
h.useCache(id)
|
_ = h.touchCache(uint64(id), false)
|
||||||
h.responseJSON(w, r, 200)
|
h.responseJSON(w, r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /_apis/artifactcache/caches/:id
|
// POST /_apis/artifactcache/caches/:id
|
||||||
func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.responseJSON(w, r, 400, err)
|
h.responseJSON(w, r, 400, err)
|
||||||
@@ -294,6 +478,11 @@ func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprout
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cache.Repo != cred.Repo {
|
||||||
|
h.responseJSON(w, r, 403, fmt.Errorf("cache %d: forbidden", id))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if cache.Complete {
|
if cache.Complete {
|
||||||
h.responseJSON(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
h.responseJSON(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
||||||
return
|
return
|
||||||
@@ -301,23 +490,7 @@ func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprout
|
|||||||
|
|
||||||
db.Close()
|
db.Close()
|
||||||
|
|
||||||
size, err := h.storage.Commit(cache.ID, cache.Size)
|
if err := h.commitCache(cache); err != nil {
|
||||||
if err != nil {
|
|
||||||
h.responseJSON(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// write real size back to cache, it may be different from the current value when the request doesn't specify it.
|
|
||||||
cache.Size = size
|
|
||||||
|
|
||||||
db, err = h.openDB()
|
|
||||||
if err != nil {
|
|
||||||
h.responseJSON(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
cache.Complete = true
|
|
||||||
if err := db.Update(cache.ID, cache); err != nil {
|
|
||||||
h.responseJSON(w, r, 500, err)
|
h.responseJSON(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -325,14 +498,38 @@ func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprout
|
|||||||
h.responseJSON(w, r, 200)
|
h.responseJSON(w, r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// commitCache assembles the uploaded parts and marks the entry complete. The caller must
|
||||||
|
// have closed its store first: Commit concatenates the whole archive and would otherwise
|
||||||
|
// hold bolt's exclusive file lock for the duration.
|
||||||
|
func (h *Handler) commitCache(cache *Cache) error {
|
||||||
|
written, err := h.storage.Commit(cache.ID, cache.Size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// write real size back to cache, it may be different from the current value when the request doesn't specify it.
|
||||||
|
cache.Size = written
|
||||||
|
cache.Complete = true
|
||||||
|
|
||||||
|
db, err := h.openDB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
return db.Update(cache.ID, cache)
|
||||||
|
}
|
||||||
|
|
||||||
// GET /_apis/artifactcache/artifacts/:id
|
// GET /_apis/artifactcache/artifacts/:id
|
||||||
|
// Authenticated via signed URL (see signedAuth), not bearer, because the
|
||||||
|
// @actions/cache toolkit downloads archiveLocation without Authorization.
|
||||||
|
// Repository scoping is already enforced at find() time; the signature binds
|
||||||
|
// the URL to the specific cache ID and an expiry.
|
||||||
func (h *Handler) get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
func (h *Handler) get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.responseJSON(w, r, 400, err)
|
h.responseJSON(w, r, 400, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.useCache(id)
|
_ = h.touchCache(uint64(id), false)
|
||||||
h.storage.Serve(w, r, uint64(id))
|
h.storage.Serve(w, r, uint64(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,28 +541,170 @@ func (h *Handler) clean(w http.ResponseWriter, r *http.Request, _ httprouter.Par
|
|||||||
h.responseJSON(w, r, 200)
|
h.responseJSON(w, r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) middleware(handler httprouter.Handle) httprouter.Handle {
|
// bearerAuth resolves ACTIONS_RUNTIME_TOKEN against the set of currently
|
||||||
|
// registered jobs. A match attaches the job's JobCredential to the request
|
||||||
|
// context; a miss returns 401 before the handler body runs.
|
||||||
|
func (h *Handler) bearerAuth(handler httprouter.Handle) httprouter.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
h.logger.Debugf("%s %s", r.Method, r.RequestURI)
|
h.logger.Debugf("%s %s", r.Method, r.URL.Path)
|
||||||
|
token := bearerToken(r)
|
||||||
|
if token == "" {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("missing bearer token"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cred, ok := h.lookupCredential(token)
|
||||||
|
if !ok {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("unknown bearer token"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(r.Context(), credKey{}, cred)
|
||||||
|
handler(w, r.WithContext(ctx), params)
|
||||||
|
go h.gcCache()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// signedAuth authenticates a signed URL. purpose separates the flavours of URL the
|
||||||
|
// handler hands out, so one cannot be replayed as another; see computeSignature.
|
||||||
|
func (h *Handler) signedAuth(purpose string, handler httprouter.Handle) httprouter.Handle {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
|
h.logger.Debugf("%s %s", r.Method, r.URL.Path)
|
||||||
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
h.responseJSON(w, r, 400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expStr := r.URL.Query().Get("exp")
|
||||||
|
sig := r.URL.Query().Get("sig")
|
||||||
|
if expStr == "" || sig == "" {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("missing signature"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
exp, err := strconv.ParseInt(expStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("invalid expiry"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if time.Now().Unix() > exp {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("signature expired"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expected := h.computeSignature(purpose, id, exp)
|
||||||
|
if !hmac.Equal([]byte(sig), []byte(expected)) {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("bad signature"))
|
||||||
|
return
|
||||||
|
}
|
||||||
handler(w, r, params)
|
handler(w, r, params)
|
||||||
go h.gcCache()
|
go h.gcCache()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// internalAuth gates the control-plane endpoints. The bearer must
|
||||||
|
// constant-time-equal the configured internalSecret. If the secret is empty,
|
||||||
|
// the control-plane is disabled and every request gets 404 — which matches
|
||||||
|
// the upstream nektos/act behavior of "the route does not exist".
|
||||||
|
func (h *Handler) internalAuth(handler httprouter.Handle) httprouter.Handle {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
|
if h.internalSecret == "" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
token := bearerToken(r)
|
||||||
|
if token == "" || !hmac.Equal([]byte(token), []byte(h.internalSecret)) {
|
||||||
|
h.responseJSON(w, r, http.StatusUnauthorized, errors.New("internal: bad secret"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handler(w, r, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type internalRegisterBody struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
Repo string `json:"repo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type internalRevokeBody struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /_internal/register
|
||||||
|
func (h *Handler) internalRegister(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
var body internalRegisterBody
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if body.Token == "" {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, errors.New("token is required"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.RegisterJob(body.Token, body.Repo)
|
||||||
|
h.responseJSON(w, r, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /_internal/revoke
|
||||||
|
func (h *Handler) internalRevoke(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
var body internalRevokeBody
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if body.Token == "" {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, errors.New("token is required"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.RevokeJob(body.Token)
|
||||||
|
h.responseJSON(w, r, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bearerToken(r *http.Request) string {
|
||||||
|
auth := r.Header.Get("Authorization")
|
||||||
|
const prefix = "Bearer "
|
||||||
|
if len(auth) > len(prefix) && strings.EqualFold(auth[:len(prefix)], prefix) {
|
||||||
|
return auth[len(prefix):]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func credFromContext(ctx context.Context) JobCredential {
|
||||||
|
if cred, ok := ctx.Value(credKey{}).(JobCredential); ok {
|
||||||
|
return cred
|
||||||
|
}
|
||||||
|
return JobCredential{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// computeSignature signs a URL for one cache entry and expiry. purpose is mixed into the
|
||||||
|
// message so a URL handed out for writing an entry cannot be replayed to read one, and the
|
||||||
|
// other way round. Downloads use the empty purpose, the message v1 has always signed.
|
||||||
|
func (h *Handler) computeSignature(purpose string, cacheID, exp int64) string {
|
||||||
|
mac := hmac.New(sha256.New, h.secret)
|
||||||
|
fmt.Fprintf(mac, "%s%d:%d", purpose, cacheID, exp)
|
||||||
|
return hex.EncodeToString(mac.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// signedURL builds a URL under path that signedAuth accepts for the same purpose.
|
||||||
|
func (h *Handler) signedURL(path, purpose string, cacheID uint64, exp time.Time) string {
|
||||||
|
expUnix := exp.Unix()
|
||||||
|
q := url.Values{}
|
||||||
|
q.Set("exp", strconv.FormatInt(expUnix, 10))
|
||||||
|
q.Set("sig", h.computeSignature(purpose, int64(cacheID), expUnix))
|
||||||
|
return fmt.Sprintf("%s%s/%d?%s", h.ExternalURL(), path, cacheID, q.Encode())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) signedArtifactURL(cacheID uint64, exp time.Time) string {
|
||||||
|
return h.signedURL(apiPath+"/artifacts", "", cacheID, exp)
|
||||||
|
}
|
||||||
|
|
||||||
// if not found, return (nil, nil) instead of an error.
|
// if not found, return (nil, nil) instead of an error.
|
||||||
func findCache(db *bolthold.Store, keys []string, version string) (*Cache, error) {
|
func findCache(db *bolthold.Store, repo string, keys []string, version string) (*Cache, error) {
|
||||||
cache := &Cache{}
|
cache := &Cache{}
|
||||||
for _, prefix := range keys {
|
for _, prefix := range keys {
|
||||||
// if a key in the list matches exactly, don't return partial matches
|
// if a key in the list matches exactly, don't return partial matches
|
||||||
if err := db.FindOne(cache,
|
exact, err := findExactCache(db, repo, prefix, version, true)
|
||||||
bolthold.Where("Key").Eq(prefix).
|
if err != nil {
|
||||||
And("Version").Eq(version).
|
return nil, err
|
||||||
And("Complete").Eq(true).
|
}
|
||||||
SortBy("CreatedAt").Reverse()); err == nil || !errors.Is(err, bolthold.ErrNotFound) {
|
if exact != nil {
|
||||||
if err != nil {
|
return exact, nil
|
||||||
return nil, fmt.Errorf("find cache: %w", err)
|
|
||||||
}
|
|
||||||
return cache, nil
|
|
||||||
}
|
}
|
||||||
prefixPattern := "^" + regexp.QuoteMeta(prefix)
|
prefixPattern := "^" + regexp.QuoteMeta(prefix)
|
||||||
re, err := regexp.Compile(prefixPattern)
|
re, err := regexp.Compile(prefixPattern)
|
||||||
@@ -373,7 +712,8 @@ func findCache(db *bolthold.Store, keys []string, version string) (*Cache, error
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := db.FindOne(cache,
|
if err := db.FindOne(cache,
|
||||||
bolthold.Where("Key").RegExp(re).
|
bolthold.Where("Repo").Eq(repo).
|
||||||
|
And("Key").RegExp(re).
|
||||||
And("Version").Eq(version).
|
And("Version").Eq(version).
|
||||||
And("Complete").Eq(true).
|
And("Complete").Eq(true).
|
||||||
SortBy("CreatedAt").Reverse()); err != nil {
|
SortBy("CreatedAt").Reverse()); err != nil {
|
||||||
@@ -387,6 +727,34 @@ func findCache(db *bolthold.Store, keys []string, version string) (*Cache, error
|
|||||||
return nil, nil //nolint:nilnil // pre-existing issue from nektos/act
|
return nil, nil //nolint:nilnil // pre-existing issue from nektos/act
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// findExactCache returns the entry for exactly this key and version, or (nil, nil) if there is
|
||||||
|
// none. Unlike findCache it never falls back to a prefix (restore-key) match, which is what both
|
||||||
|
// its callers need: a new key that is only a prefix of an existing key is not the same entry.
|
||||||
|
//
|
||||||
|
// A completed entry is the one to restore, sorted by when it was written. An incomplete one is a
|
||||||
|
// reservation being uploaded to, sorted by when it was last written to, because the upload route
|
||||||
|
// touches UsedAt on every part.
|
||||||
|
func findExactCache(db *bolthold.Store, repo, key, version string, complete bool) (*Cache, error) {
|
||||||
|
sortBy := "UsedAt"
|
||||||
|
if complete {
|
||||||
|
sortBy = "CreatedAt"
|
||||||
|
}
|
||||||
|
cache := &Cache{}
|
||||||
|
err := db.FindOne(cache,
|
||||||
|
bolthold.Where("Repo").Eq(repo).
|
||||||
|
And("Key").Eq(key).
|
||||||
|
And("Version").Eq(version).
|
||||||
|
And("Complete").Eq(complete).
|
||||||
|
SortBy(sortBy).Reverse())
|
||||||
|
if errors.Is(err, bolthold.ErrNotFound) {
|
||||||
|
return nil, nil //nolint:nilnil // absence is not an error here
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("find cache: %w", err)
|
||||||
|
}
|
||||||
|
return cache, nil
|
||||||
|
}
|
||||||
|
|
||||||
func insertCache(db *bolthold.Store, cache *Cache) error {
|
func insertCache(db *bolthold.Store, cache *Cache) error {
|
||||||
if err := db.Insert(bolthold.NextSequence(), cache); err != nil {
|
if err := db.Insert(bolthold.NextSequence(), cache); err != nil {
|
||||||
return fmt.Errorf("insert cache: %w", err)
|
return fmt.Errorf("insert cache: %w", err)
|
||||||
@@ -398,18 +766,30 @@ func insertCache(db *bolthold.Store, cache *Cache) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) useCache(id int64) {
|
// touchCache stamps UsedAt so gcCache does not reap an entry mid-upload. With requireIncomplete
|
||||||
|
// it also refuses an entry that is already complete, which is what the v2 blob route needs: its
|
||||||
|
// upload URL outlives the finalize call, and overwriting a finished entry would leave the blob
|
||||||
|
// other jobs restore no longer matching its recorded size. An entry missing from the store is
|
||||||
|
// accepted, since the signature proves the id was handed out.
|
||||||
|
func (h *Handler) touchCache(id uint64, requireIncomplete bool) error {
|
||||||
db, err := h.openDB()
|
db, err := h.openDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
cache := &Cache{}
|
cache := &Cache{}
|
||||||
if err := db.Get(id, cache); err != nil {
|
if err := db.Get(id, cache); err != nil {
|
||||||
return
|
if errors.Is(err, bolthold.ErrNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if requireIncomplete && cache.Complete {
|
||||||
|
return fmt.Errorf("cache %d: already complete", id)
|
||||||
}
|
}
|
||||||
cache.UsedAt = time.Now().Unix()
|
cache.UsedAt = time.Now().Unix()
|
||||||
_ = db.Update(cache.ID, cache)
|
return db.Update(cache.ID, cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -419,7 +799,6 @@ const (
|
|||||||
keepOld = 5 * time.Minute
|
keepOld = 5 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func (h *Handler) gcCache() {
|
func (h *Handler) gcCache() {
|
||||||
if h.gcing.Load() {
|
if h.gcing.Load() {
|
||||||
return
|
return
|
||||||
@@ -494,12 +873,16 @@ func (h *Handler) gcCache() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the old caches with the same key and version, keep the latest one.
|
// Remove the old caches with the same key and version within the same
|
||||||
|
// repository, keep the latest one. Aggregation must include Repo so two
|
||||||
|
// repos that happen to share a (key, version) do not evict each other —
|
||||||
|
// otherwise per-repo scoping holds for reads but one repo can age
|
||||||
|
// another out after keepOld.
|
||||||
// Also keep the olds which have been used recently for a while in case of the cache is still in use.
|
// Also keep the olds which have been used recently for a while in case of the cache is still in use.
|
||||||
if results, err := db.FindAggregate(
|
if results, err := db.FindAggregate(
|
||||||
&Cache{},
|
&Cache{},
|
||||||
bolthold.Where("Complete").Eq(true),
|
bolthold.Where("Complete").Eq(true),
|
||||||
"Key", "Version",
|
"Repo", "Key", "Version",
|
||||||
); err != nil {
|
); err != nil {
|
||||||
h.logger.Warnf("find aggregate caches: %v", err)
|
h.logger.Warnf("find aggregate caches: %v", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -533,7 +916,7 @@ func (h *Handler) responseJSON(w http.ResponseWriter, r *http.Request, code int,
|
|||||||
if len(v) == 0 || v[0] == nil {
|
if len(v) == 0 || v[0] == nil {
|
||||||
data, _ = json.Marshal(struct{}{})
|
data, _ = json.Marshal(struct{}{})
|
||||||
} else if err, ok := v[0].(error); ok {
|
} else if err, ok := v[0].(error); ok {
|
||||||
h.logger.Errorf("%v %v: %v", r.Method, r.RequestURI, err)
|
h.logger.Errorf("%v %v: %v", r.Method, r.URL.Path, err)
|
||||||
data, _ = json.Marshal(map[string]any{
|
data, _ = json.Marshal(map[string]any{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -22,12 +23,38 @@ import (
|
|||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// testToken is registered with the cache server in every test that needs to
|
||||||
|
// make authenticated requests; testClient then attaches it as the
|
||||||
|
// Authorization: Bearer header. testRepo is the repository scope used when
|
||||||
|
// registering it; cross-repo isolation is exercised in its own test.
|
||||||
|
const (
|
||||||
|
testToken = "test-runtime-token"
|
||||||
|
testRepo = "owner/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type bearerTransport struct{ token string }
|
||||||
|
|
||||||
|
func (b *bearerTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
|
r.Header.Set("Authorization", "Bearer "+b.token)
|
||||||
|
return http.DefaultTransport.RoundTrip(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
var testClient = &http.Client{Transport: &bearerTransport{token: testToken}}
|
||||||
|
|
||||||
|
// signArtifactURL builds a signed download URL the same way the server does;
|
||||||
|
// tests use it to reach the get handler directly without going through a
|
||||||
|
// find/cache-hit round trip.
|
||||||
|
func signArtifactURL(h *Handler, id int64) string {
|
||||||
|
return h.signedArtifactURL(uint64(id), time.Now().Add(artifactURLTTL))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHandler(t *testing.T) {
|
func TestHandler(t *testing.T) {
|
||||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
handler, err := StartHandler(dir, "", 0, nil)
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
handler.RegisterJob(testToken, testRepo)
|
||||||
|
|
||||||
base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase)
|
base := fmt.Sprintf("%s%s", handler.ExternalURL(), apiPath)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
t.Run("inpect db", func(t *testing.T) {
|
t.Run("inpect db", func(t *testing.T) {
|
||||||
@@ -45,7 +72,10 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, handler.Close())
|
require.NoError(t, handler.Close())
|
||||||
assert.Nil(t, handler.server)
|
assert.Nil(t, handler.server)
|
||||||
assert.Nil(t, handler.listener)
|
assert.Nil(t, handler.listener)
|
||||||
_, err := http.Post(fmt.Sprintf("%s/caches/%d", base, 1), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, 1), "", nil)
|
||||||
|
if err == nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
@@ -53,8 +83,9 @@ func TestHandler(t *testing.T) {
|
|||||||
t.Run("get not exist", func(t *testing.T) {
|
t.Run("get not exist", func(t *testing.T) {
|
||||||
key := strings.ToLower(t.Name())
|
key := strings.ToLower(t.Name())
|
||||||
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 204, resp.StatusCode)
|
require.Equal(t, 204, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -68,16 +99,18 @@ func TestHandler(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("clean", func(t *testing.T) {
|
t.Run("clean", func(t *testing.T) {
|
||||||
resp, err := http.Post(base+"/clean", "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/clean", "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("reserve with bad request", func(t *testing.T) {
|
t.Run("reserve with bad request", func(t *testing.T) {
|
||||||
body := []byte(`invalid json`)
|
body := []byte(`invalid json`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -94,8 +127,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&first))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&first))
|
||||||
@@ -108,8 +142,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&second))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&second))
|
||||||
@@ -125,8 +160,9 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -136,8 +172,9 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -155,8 +192,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
got := struct {
|
got := struct {
|
||||||
@@ -171,13 +209,15 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@@ -186,8 +226,9 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -206,8 +247,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
got := struct {
|
got := struct {
|
||||||
@@ -222,24 +264,27 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes xx-99/*")
|
req.Header.Set("Content-Range", "bytes xx-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("commit with bad id", func(t *testing.T) {
|
t.Run("commit with bad id", func(t *testing.T) {
|
||||||
{
|
{
|
||||||
resp, err := http.Post(base+"/caches/invalid_id", "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches/invalid_id", "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("commit with not exist id", func(t *testing.T) {
|
t.Run("commit with not exist id", func(t *testing.T) {
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, 100), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, 100), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -258,8 +303,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
got := struct {
|
got := struct {
|
||||||
@@ -274,22 +320,73 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 400, resp.StatusCode)
|
assert.Equal(t, 400, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("upload write failure returns only error", func(t *testing.T) {
|
||||||
|
key := strings.ToLower(t.Name())
|
||||||
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
|
var id uint64
|
||||||
|
{
|
||||||
|
body, err := json.Marshal(&Request{
|
||||||
|
Key: key,
|
||||||
|
Version: version,
|
||||||
|
Size: 100,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
|
got := struct {
|
||||||
|
CacheID uint64 `json:"cacheId"`
|
||||||
|
}{}
|
||||||
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
|
id = got.CacheID
|
||||||
|
}
|
||||||
|
|
||||||
|
storageFile := filepath.Join(dir, "not-a-directory")
|
||||||
|
require.NoError(t, os.WriteFile(storageFile, []byte("blocked"), 0o600))
|
||||||
|
originalStorage := handler.storage
|
||||||
|
handler.storage = &Storage{rootDir: storageFile}
|
||||||
|
defer func() {
|
||||||
|
handler.storage = originalStorage
|
||||||
|
}()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPatch,
|
||||||
|
fmt.Sprintf("%s/caches/%d", base, id), bytes.NewReader(make([]byte, 100)))
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
|
resp, err := testClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
require.Equal(t, 500, resp.StatusCode)
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
var got map[string]string
|
||||||
|
require.NoError(t, json.Unmarshal(body, &got))
|
||||||
|
assert.NotEmpty(t, got["error"])
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("commit early", func(t *testing.T) {
|
t.Run("commit early", func(t *testing.T) {
|
||||||
key := strings.ToLower(t.Name())
|
key := strings.ToLower(t.Name())
|
||||||
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
@@ -304,8 +401,9 @@ func TestHandler(t *testing.T) {
|
|||||||
Size: 100,
|
Size: 100,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
got := struct {
|
got := struct {
|
||||||
@@ -320,32 +418,30 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-59/*")
|
req.Header.Set("Content-Range", "bytes 0-59/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 500, resp.StatusCode)
|
assert.Equal(t, 500, resp.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("get with bad id", func(t *testing.T) {
|
t.Run("get with bad id", func(t *testing.T) {
|
||||||
resp, err := http.Get(base + "/artifacts/invalid_id") //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(base + "/artifacts/invalid_id")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 400, resp.StatusCode)
|
require.Equal(t, 400, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("get with not exist id", func(t *testing.T) {
|
t.Run("get with not exist id", func(t *testing.T) {
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/artifacts/%d", base, 100)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(signArtifactURL(handler, 100))
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 404, resp.StatusCode)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("get with not exist id", func(t *testing.T) {
|
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/artifacts/%d", base, 100)) //nolint:bodyclose // pre-existing issue from nektos/act
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 404, resp.StatusCode)
|
require.Equal(t, 404, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -366,7 +462,8 @@ func TestHandler(t *testing.T) {
|
|||||||
_, err := rand.Read(contents[i])
|
_, err := rand.Read(contents[i])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
||||||
time.Sleep(time.Second) // ensure CreatedAt of caches are different
|
// ensure CreatedAt of caches are different, in upload order
|
||||||
|
backdateCache(t, handler, keys[i], time.Duration(len(contents)-i)*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqKeys := strings.Join([]string{
|
reqKeys := strings.Join([]string{
|
||||||
@@ -375,8 +472,9 @@ func TestHandler(t *testing.T) {
|
|||||||
key + "_a",
|
key + "_a",
|
||||||
}, ",")
|
}, ",")
|
||||||
|
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -395,26 +493,31 @@ func TestHandler(t *testing.T) {
|
|||||||
assert.Equal(t, "hit", got.Result)
|
assert.Equal(t, "hit", got.Result)
|
||||||
assert.Equal(t, keys[except], got.CacheKey)
|
assert.Equal(t, keys[except], got.CacheKey)
|
||||||
|
|
||||||
contentResp, err := http.Get(got.ArchiveLocation) //nolint:bodyclose // pre-existing issue from nektos/act
|
contentResp, err := testClient.Get(got.ArchiveLocation)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer contentResp.Body.Close()
|
||||||
require.Equal(t, 200, contentResp.StatusCode)
|
require.Equal(t, 200, contentResp.StatusCode)
|
||||||
content, err := io.ReadAll(contentResp.Body)
|
content, err := io.ReadAll(contentResp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, contents[except], content)
|
assert.Equal(t, contents[except], content)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("case insensitive", func(t *testing.T) {
|
t.Run("case preserved", func(t *testing.T) {
|
||||||
|
// Some actions (e.g. actions/setup-go, actions/setup-node) build cache keys that contain mixed-case fragments such as RUNNER_OS=Linux,
|
||||||
|
// then compare the cacheKey returned by the cache server to their original key with case-sensitive equality to decide whether the
|
||||||
|
// cache was a complete hit. The server must therefore preserve the original key case.
|
||||||
|
|
||||||
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
key := strings.ToLower(t.Name())
|
key := strings.ToLower(t.Name()) + "_ABC"
|
||||||
content := make([]byte, 100)
|
content := make([]byte, 100)
|
||||||
_, err := rand.Read(content)
|
_, err := rand.Read(content)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
uploadCacheNormally(t, base, key+"_ABC", version, content)
|
uploadCacheNormally(t, base, key, version, content)
|
||||||
|
|
||||||
{
|
{
|
||||||
reqKey := key + "_aBc"
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKey, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
got := struct {
|
got := struct {
|
||||||
Result string `json:"result"`
|
Result string `json:"result"`
|
||||||
@@ -423,7 +526,8 @@ func TestHandler(t *testing.T) {
|
|||||||
}{}
|
}{}
|
||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
assert.Equal(t, "hit", got.Result)
|
assert.Equal(t, "hit", got.Result)
|
||||||
assert.Equal(t, key+"_abc", got.CacheKey)
|
assert.Equal(t, key, got.CacheKey)
|
||||||
|
assert.NotEqual(t, strings.ToLower(key), got.CacheKey)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -444,7 +548,8 @@ func TestHandler(t *testing.T) {
|
|||||||
_, err := rand.Read(contents[i])
|
_, err := rand.Read(contents[i])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
||||||
time.Sleep(time.Second) // ensure CreatedAt of caches are different
|
// ensure CreatedAt of caches are different, in upload order
|
||||||
|
backdateCache(t, handler, keys[i], time.Duration(len(contents)-i)*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqKeys := strings.Join([]string{
|
reqKeys := strings.Join([]string{
|
||||||
@@ -452,8 +557,9 @@ func TestHandler(t *testing.T) {
|
|||||||
key + "_a_b",
|
key + "_a_b",
|
||||||
}, ",")
|
}, ",")
|
||||||
|
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -470,8 +576,9 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
assert.Equal(t, keys[expect], got.CacheKey)
|
assert.Equal(t, keys[expect], got.CacheKey)
|
||||||
|
|
||||||
contentResp, err := http.Get(got.ArchiveLocation) //nolint:bodyclose // pre-existing issue from nektos/act
|
contentResp, err := testClient.Get(got.ArchiveLocation)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer contentResp.Body.Close()
|
||||||
require.Equal(t, 200, contentResp.StatusCode)
|
require.Equal(t, 200, contentResp.StatusCode)
|
||||||
content, err := io.ReadAll(contentResp.Body)
|
content, err := io.ReadAll(contentResp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -495,7 +602,8 @@ func TestHandler(t *testing.T) {
|
|||||||
_, err := rand.Read(contents[i])
|
_, err := rand.Read(contents[i])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
uploadCacheNormally(t, base, keys[i], version, contents[i])
|
||||||
time.Sleep(time.Second) // ensure CreatedAt of caches are different
|
// ensure CreatedAt of caches are different, in upload order
|
||||||
|
backdateCache(t, handler, keys[i], time.Duration(len(contents)-i)*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqKeys := strings.Join([]string{
|
reqKeys := strings.Join([]string{
|
||||||
@@ -504,8 +612,9 @@ func TestHandler(t *testing.T) {
|
|||||||
key + "_a_b",
|
key + "_a_b",
|
||||||
}, ",")
|
}, ",")
|
||||||
|
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, reqKeys, version))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -523,8 +632,9 @@ func TestHandler(t *testing.T) {
|
|||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
assert.Equal(t, keys[expect], got.CacheKey)
|
assert.Equal(t, keys[expect], got.CacheKey)
|
||||||
|
|
||||||
contentResp, err := http.Get(got.ArchiveLocation) //nolint:bodyclose // pre-existing issue from nektos/act
|
contentResp, err := testClient.Get(got.ArchiveLocation)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer contentResp.Body.Close()
|
||||||
require.Equal(t, 200, contentResp.StatusCode)
|
require.Equal(t, 200, contentResp.StatusCode)
|
||||||
content, err := io.ReadAll(contentResp.Body)
|
content, err := io.ReadAll(contentResp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -532,6 +642,20 @@ func TestHandler(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// backdateCache rewrites a cache's CreatedAt. It has one-second resolution, so age-ordering
|
||||||
|
// tests set it directly instead of sleeping a second between uploads.
|
||||||
|
func backdateCache(t *testing.T, handler *Handler, key string, age time.Duration) {
|
||||||
|
db, err := handler.openDB()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
var caches []*Cache
|
||||||
|
require.NoError(t, db.Find(&caches, bolthold.Where("Key").Eq(key)))
|
||||||
|
require.Len(t, caches, 1)
|
||||||
|
caches[0].CreatedAt = time.Now().Add(-age).Unix()
|
||||||
|
require.NoError(t, db.Update(caches[0].ID, caches[0]))
|
||||||
|
}
|
||||||
|
|
||||||
func uploadCacheNormally(t *testing.T, base, key, version string, content []byte) { //nolint:unparam // pre-existing issue from nektos/act
|
func uploadCacheNormally(t *testing.T, base, key, version string, content []byte) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
var id uint64
|
var id uint64
|
||||||
{
|
{
|
||||||
@@ -541,8 +665,9 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
|||||||
Size: int64(len(content)),
|
Size: int64(len(content)),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
resp, err := http.Post(base+"/caches", "application/json", bytes.NewReader(body)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(body))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
got := struct {
|
got := struct {
|
||||||
@@ -557,19 +682,22 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Content-Range", "bytes 0-99/*")
|
req.Header.Set("Content-Range", "bytes 0-99/*")
|
||||||
resp, err := http.DefaultClient.Do(req) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Post(fmt.Sprintf("%s/caches/%d", base, id), "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
assert.Equal(t, 200, resp.StatusCode)
|
||||||
}
|
}
|
||||||
var archiveLocation string
|
var archiveLocation string
|
||||||
{
|
{
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version)) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
got := struct {
|
got := struct {
|
||||||
Result string `json:"result"`
|
Result string `json:"result"`
|
||||||
@@ -578,12 +706,13 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
|||||||
}{}
|
}{}
|
||||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
assert.Equal(t, "hit", got.Result)
|
assert.Equal(t, "hit", got.Result)
|
||||||
assert.Equal(t, strings.ToLower(key), got.CacheKey)
|
assert.Equal(t, key, got.CacheKey)
|
||||||
archiveLocation = got.ArchiveLocation
|
archiveLocation = got.ArchiveLocation
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
resp, err := http.Get(archiveLocation) //nolint:bodyclose // pre-existing issue from nektos/act
|
resp, err := testClient.Get(archiveLocation)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
got, err := io.ReadAll(resp.Body)
|
got, err := io.ReadAll(resp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -593,7 +722,7 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
|||||||
|
|
||||||
func TestHandler_gcCache(t *testing.T) {
|
func TestHandler_gcCache(t *testing.T) {
|
||||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
handler, err := StartHandler(dir, "", 0, nil)
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -699,3 +828,421 @@ func TestHandler_gcCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
require.NoError(t, db.Close())
|
require.NoError(t, db.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHandler_RejectsMissingBearer covers the advisory's root cause:
|
||||||
|
// unauthenticated access to management endpoints is now refused with 401.
|
||||||
|
func TestHandler_RejectsMissingBearer(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
method string
|
||||||
|
path string
|
||||||
|
body string
|
||||||
|
}{
|
||||||
|
{"find", http.MethodGet, "/cache?keys=x&version=y", ""},
|
||||||
|
{"reserve", http.MethodPost, "/caches", "{}"},
|
||||||
|
{"upload", http.MethodPatch, "/caches/1", ""},
|
||||||
|
{"commit", http.MethodPost, "/caches/1", ""},
|
||||||
|
{"clean", http.MethodPost, "/clean", ""},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
req, err := http.NewRequest(tc.method, base+tc.path, strings.NewReader(tc.body))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_RejectsUnknownBearer verifies that a bearer token is only
|
||||||
|
// accepted after RegisterJob; stale/forged tokens cannot be replayed.
|
||||||
|
func TestHandler_RejectsUnknownBearer(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, base+"/cache?keys=x&version=y", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Authorization", "Bearer not-a-registered-token")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_UnregisterRevokes ensures that the function returned by
|
||||||
|
// RegisterJob invalidates the credential, so a token leaked at job time stops
|
||||||
|
// working the moment the job ends instead of living for the runner's lifetime.
|
||||||
|
func TestHandler_UnregisterRevokes(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
unregister := handler.RegisterJob("tmp-token", testRepo)
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
req, err := http.NewRequest(http.MethodGet, base+"/cache?keys=x&version=y", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Authorization", "Bearer tmp-token")
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.NotEqual(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
|
||||||
|
unregister()
|
||||||
|
|
||||||
|
resp, err = http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_CrossRepoIsolation addresses the intra-runner poisoning vector
|
||||||
|
// raised in GHSA-82g9-637c-2fx2: job containers can reach the cache server
|
||||||
|
// over the docker bridge, so IP allowlisting alone does not stop a malicious
|
||||||
|
// PR run from another repo. A cache entry created under repoA must be
|
||||||
|
// invisible to queries scoped to repoB.
|
||||||
|
func TestHandler_CrossRepoIsolation(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
handler.RegisterJob("token-a", "owner/repoA")
|
||||||
|
handler.RegisterJob("token-b", "owner/repoB")
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
key := "shared-key"
|
||||||
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
|
content := []byte("repoA-payload")
|
||||||
|
|
||||||
|
clientA := &http.Client{Transport: &bearerTransport{token: "token-a"}}
|
||||||
|
clientB := &http.Client{Transport: &bearerTransport{token: "token-b"}}
|
||||||
|
|
||||||
|
// repoA reserves + uploads + commits.
|
||||||
|
reserveBody, err := json.Marshal(&Request{Key: key, Version: version, Size: int64(len(content))})
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := clientA.Post(base+"/caches", "application/json", bytes.NewReader(reserveBody))
|
||||||
|
require.NoError(t, err)
|
||||||
|
var reserved struct {
|
||||||
|
CacheID uint64 `json:"cacheId"`
|
||||||
|
}
|
||||||
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&reserved))
|
||||||
|
resp.Body.Close()
|
||||||
|
require.NotZero(t, reserved.CacheID)
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), bytes.NewReader(content))
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Content-Range", fmt.Sprintf("bytes 0-%d/*", len(content)-1))
|
||||||
|
resp, err = clientA.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
resp, err = clientA.Post(fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
// repoB with a matching key and version must NOT see repoA's cache.
|
||||||
|
resp, err = clientB.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
|
||||||
|
|
||||||
|
// repoA still sees its own cache.
|
||||||
|
resp, err = clientA.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
// repoB cannot upload to repoA's reserved id either (forbidden, not 401).
|
||||||
|
req, err = http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), bytes.NewReader([]byte("poison")))
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Content-Range", "bytes 0-5/*")
|
||||||
|
resp, err = clientB.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_ArtifactSignature verifies that archive downloads reject
|
||||||
|
// missing / tampered / expired signatures, so a leaked archiveLocation stops
|
||||||
|
// working after artifactURLTTL even if the bearer token is still registered.
|
||||||
|
func TestHandler_ArtifactSignature(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
handler.RegisterJob(testToken, testRepo)
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
|
||||||
|
t.Run("missing signature", func(t *testing.T) {
|
||||||
|
resp, err := testClient.Get(fmt.Sprintf("%s/artifacts/%d", base, 1))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("tampered signature", func(t *testing.T) {
|
||||||
|
good := handler.signedArtifactURL(1, time.Now().Add(artifactURLTTL))
|
||||||
|
bad := good[:len(good)-4] + "dead"
|
||||||
|
resp, err := testClient.Get(bad)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("expired signature", func(t *testing.T) {
|
||||||
|
expired := handler.signedArtifactURL(1, time.Now().Add(-time.Second))
|
||||||
|
resp, err := testClient.Get(expired)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("signature from a different server", func(t *testing.T) {
|
||||||
|
dir2 := filepath.Join(t.TempDir(), "artifactcache2")
|
||||||
|
other, err := StartHandler(dir2, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer other.Close()
|
||||||
|
otherURL := other.signedArtifactURL(1, time.Now().Add(artifactURLTTL))
|
||||||
|
// Rewrite the host so the request still lands on our handler, but
|
||||||
|
// the signature was computed with a different secret.
|
||||||
|
parts := strings.SplitN(otherURL, apiPath, 2)
|
||||||
|
forged := base + parts[1]
|
||||||
|
resp, err := testClient.Get(forged)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_SecretPersistsAcrossRestarts is the property that lets
|
||||||
|
// gitea-runner cache-server be pointed at via cfg.Cache.ExternalServer: a
|
||||||
|
// restart must not invalidate signed URLs the handler has already issued
|
||||||
|
// (within their expiry window).
|
||||||
|
func TestHandler_SecretPersistsAcrossRestarts(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
|
||||||
|
first, err := StartHandler(dir, "127.0.0.1", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
exp := time.Now().Add(artifactURLTTL).Unix()
|
||||||
|
sig := first.computeSignature("", 42, exp)
|
||||||
|
require.NoError(t, first.Close())
|
||||||
|
|
||||||
|
second, err := StartHandler(dir, "127.0.0.1", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer second.Close()
|
||||||
|
|
||||||
|
assert.Equal(t, sig, second.computeSignature("", 42, exp))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_ArtifactSignatureDownload is a happy-path round trip that
|
||||||
|
// ensures a real reserve/upload/commit/find/download flow still works after
|
||||||
|
// the auth refactor.
|
||||||
|
func TestHandler_ArtifactSignatureDownload(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
handler.RegisterJob(testToken, testRepo)
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
key := "download-key"
|
||||||
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
|
content := []byte("hello")
|
||||||
|
uploadCacheNormally(t, base, key, version, content)
|
||||||
|
|
||||||
|
resp, err := testClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
var hit struct {
|
||||||
|
ArchiveLocation string `json:"archiveLocation"`
|
||||||
|
}
|
||||||
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&hit))
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
require.Contains(t, hit.ArchiveLocation, "sig=")
|
||||||
|
require.Contains(t, hit.ArchiveLocation, "exp=")
|
||||||
|
|
||||||
|
// Download without any Authorization header — the signature alone must
|
||||||
|
// be enough, because @actions/cache downloads archiveLocation unauth'd.
|
||||||
|
dl, err := http.Get(hit.ArchiveLocation)
|
||||||
|
require.NoError(t, err)
|
||||||
|
body, err := io.ReadAll(dl.Body)
|
||||||
|
dl.Body.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, dl.StatusCode)
|
||||||
|
assert.Equal(t, content, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_RegisterJob_RefCounted verifies that a duplicate RegisterJob
|
||||||
|
// for the same token does not silently revoke the first registration on the
|
||||||
|
// first revoker call. This matters if a runner ever re-registers a token
|
||||||
|
// (restart mid-task, retry), which must not kill the live job's auth.
|
||||||
|
func TestHandler_RegisterJob_RefCounted(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
first := handler.RegisterJob("shared", testRepo)
|
||||||
|
second := handler.RegisterJob("shared", testRepo)
|
||||||
|
|
||||||
|
base := handler.ExternalURL() + apiPath
|
||||||
|
probe := func() int {
|
||||||
|
req, err := http.NewRequest(http.MethodGet, base+"/cache?keys=x&version=v", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Authorization", "Bearer shared")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
return resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NotEqual(t, http.StatusUnauthorized, probe())
|
||||||
|
first()
|
||||||
|
assert.NotEqual(t, http.StatusUnauthorized, probe(),
|
||||||
|
"token must stay valid while another registration holds the refcount")
|
||||||
|
second()
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, probe(),
|
||||||
|
"token is revoked only after every revoker has run")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_GC_PerRepoDedup ensures duplicate-pruning does not evict
|
||||||
|
// another repo's entry. Two repos reserve the same (key, version); after the
|
||||||
|
// keepOld window, GC must keep the one from each repo.
|
||||||
|
func TestHandler_GC_PerRepoDedup(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
handler.RegisterJob("tok-a", "owner/repoA")
|
||||||
|
handler.RegisterJob("tok-b", "owner/repoB")
|
||||||
|
|
||||||
|
key := "shared-dedup-key"
|
||||||
|
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||||
|
|
||||||
|
// Seed one completed cache per repo directly via the DB, bypassing the
|
||||||
|
// HTTP round trip so we can precisely control UsedAt.
|
||||||
|
db, err := handler.openDB()
|
||||||
|
require.NoError(t, err)
|
||||||
|
now := time.Now().Unix()
|
||||||
|
stale := time.Now().Add(-keepOld - time.Minute).Unix()
|
||||||
|
a := &Cache{Repo: "owner/repoA", Key: key, Version: version, Complete: true, CreatedAt: stale, UsedAt: stale, Size: 1}
|
||||||
|
b := &Cache{Repo: "owner/repoB", Key: key, Version: version, Complete: true, CreatedAt: now, UsedAt: now, Size: 1}
|
||||||
|
require.NoError(t, insertCache(db, a))
|
||||||
|
require.NoError(t, insertCache(db, b))
|
||||||
|
// Write the backing blobs so the dedup deletion has something to remove.
|
||||||
|
require.NoError(t, handler.storage.Write(a.ID, 0, strings.NewReader("a")))
|
||||||
|
_, err = handler.storage.Commit(a.ID, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, handler.storage.Write(b.ID, 0, strings.NewReader("b")))
|
||||||
|
_, err = handler.storage.Commit(b.ID, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, db.Close())
|
||||||
|
|
||||||
|
// Force GC to run regardless of the cooldown.
|
||||||
|
handler.gcAt = time.Time{}
|
||||||
|
handler.gcCache()
|
||||||
|
|
||||||
|
db, err = handler.openDB()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
var after []Cache
|
||||||
|
require.NoError(t, db.Find(&after, bolthold.Where("Key").Eq(key).And("Version").Eq(version)))
|
||||||
|
|
||||||
|
repos := make(map[string]bool)
|
||||||
|
for _, c := range after {
|
||||||
|
repos[c.Repo] = true
|
||||||
|
}
|
||||||
|
assert.True(t, repos["owner/repoA"], "repoA's cache must survive dedup against repoB")
|
||||||
|
assert.True(t, repos["owner/repoB"], "repoB's cache must survive dedup against repoA")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_InternalAPI_Disabled verifies that without an internalSecret
|
||||||
|
// the control-plane routes are 404 — operators can't accidentally hit
|
||||||
|
// register/revoke when the feature is off.
|
||||||
|
func TestHandler_InternalAPI_Disabled(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
handler, err := StartHandler(dir, "", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
for _, ep := range []string{"/_internal/register", "/_internal/revoke"} {
|
||||||
|
resp, err := http.Post(handler.ExternalURL()+ep, "application/json", strings.NewReader(`{}`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode, ep)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandler_InternalAPI_AuthAndUsage covers the control-plane: bad/missing
|
||||||
|
// secret → 401, malformed body → 400, happy path round-trips a token through
|
||||||
|
// register → cache-API accepts it → revoke → cache-API rejects it.
|
||||||
|
func TestHandler_InternalAPI_AuthAndUsage(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||||
|
const secret = "internal-secret"
|
||||||
|
handler, err := StartHandler(dir, "", 0, secret, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer handler.Close()
|
||||||
|
|
||||||
|
base := handler.ExternalURL()
|
||||||
|
|
||||||
|
post := func(path, bearer, body string) int {
|
||||||
|
req, err := http.NewRequest(http.MethodPost, base+path, strings.NewReader(body))
|
||||||
|
require.NoError(t, err)
|
||||||
|
if bearer != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+bearer)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
return resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("missing secret 401", func(t *testing.T) {
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, post("/_internal/register", "", `{"token":"x","repo":"r"}`))
|
||||||
|
})
|
||||||
|
t.Run("wrong secret 401", func(t *testing.T) {
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, post("/_internal/register", "wrong", `{"token":"x","repo":"r"}`))
|
||||||
|
})
|
||||||
|
t.Run("malformed body 400", func(t *testing.T) {
|
||||||
|
assert.Equal(t, http.StatusBadRequest, post("/_internal/register", secret, `not json`))
|
||||||
|
})
|
||||||
|
t.Run("missing token 400", func(t *testing.T) {
|
||||||
|
assert.Equal(t, http.StatusBadRequest, post("/_internal/register", secret, `{"repo":"r"}`))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("register then revoke round-trip", func(t *testing.T) {
|
||||||
|
probe := func(token string) int {
|
||||||
|
req, _ := http.NewRequest(http.MethodGet, base+apiPath+"/cache?keys=k&version=v", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
return resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, probe("via-internal-api"))
|
||||||
|
assert.Equal(t, http.StatusOK, post("/_internal/register", secret, `{"token":"via-internal-api","repo":"owner/repo"}`))
|
||||||
|
assert.NotEqual(t, http.StatusUnauthorized, probe("via-internal-api"))
|
||||||
|
assert.Equal(t, http.StatusOK, post("/_internal/revoke", secret, `{"token":"via-internal-api"}`))
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, probe("via-internal-api"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
268
act/artifactcache/handler_v2.go
Normal file
268
act/artifactcache/handler_v2.go
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package artifactcache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cmp"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The cache service v2 API. A client on this version talks twirp to
|
||||||
|
// `github.actions.results.api.v1.CacheService` instead of the /_apis/artifactcache
|
||||||
|
// endpoints, and uploads the archive to the returned URL with the Azure blob protocol.
|
||||||
|
// Both API versions are served from the same store, so a repository keeps its cache
|
||||||
|
// when a workflow moves between action versions.
|
||||||
|
const (
|
||||||
|
cacheServiceV2Path = "/twirp/github.actions.results.api.v1.CacheService"
|
||||||
|
|
||||||
|
// blobPath authenticates by signature, because the client uploads without an
|
||||||
|
// Authorization header. Downloads are handed the v1 artifact URL instead.
|
||||||
|
blobPath = apiPath + "/blobs"
|
||||||
|
|
||||||
|
// blobUploadPurpose keeps an upload URL from being replayed to read an entry.
|
||||||
|
blobUploadPurpose = "upload:"
|
||||||
|
|
||||||
|
blobUploadURLTTL = time.Hour
|
||||||
|
|
||||||
|
// twirpInternal is the only error code that is not the client's fault.
|
||||||
|
twirpInternal = "internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) registerV2Routes(router *httprouter.Router) {
|
||||||
|
router.POST(cacheServiceV2Path+"/CreateCacheEntry", h.bearerAuth(h.v2CreateCacheEntry))
|
||||||
|
router.POST(cacheServiceV2Path+"/FinalizeCacheEntryUpload", h.bearerAuth(h.v2FinalizeCacheEntryUpload))
|
||||||
|
router.POST(cacheServiceV2Path+"/GetCacheEntryDownloadURL", h.bearerAuth(h.v2GetCacheEntryDownloadURL))
|
||||||
|
router.PUT(blobPath+"/:id", h.signedAuth(blobUploadPurpose, h.v2UploadBlob))
|
||||||
|
}
|
||||||
|
|
||||||
|
// An entry that already exists is reported as not ok, which is how the client learns to skip
|
||||||
|
// the upload.
|
||||||
|
func (h *Handler) v2CreateCacheEntry(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
|
req, err := decodeTwirpRequest[v2CreateRequest](r)
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, "malformed_request", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Key == "" || req.Version == "" {
|
||||||
|
h.twirpError(w, r, "invalid_argument", errors.New("key and version are required"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := h.openDB()
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// An exact (key, version) match means the entry is already cached; the client then skips
|
||||||
|
// the upload. A prefix match must not count here, or a shorter key would be reported as
|
||||||
|
// existing and silently never saved.
|
||||||
|
if existing, err := findExactCache(db, cred.Repo, req.Key, req.Version, true); err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
} else if existing != nil {
|
||||||
|
h.twirpNotOK(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
cache := &Cache{
|
||||||
|
Repo: cred.Repo,
|
||||||
|
Key: req.Key,
|
||||||
|
Version: req.Version,
|
||||||
|
Size: -1, // the size is only known at finalize time
|
||||||
|
CreatedAt: now,
|
||||||
|
UsedAt: now,
|
||||||
|
}
|
||||||
|
if err := insertCache(db, cache); err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.responseJSON(w, r, http.StatusOK, map[string]any{
|
||||||
|
"ok": true,
|
||||||
|
"signedUploadUrl": h.signedURL(blobPath, blobUploadPurpose, cache.ID, time.Now().Add(blobUploadURLTTL)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) v2FinalizeCacheEntryUpload(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
|
req, err := decodeTwirpRequest[v2FinalizeRequest](r)
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, "malformed_request", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := h.openDB()
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
cache, err := findExactCache(db, cred.Repo, req.Key, req.Version, false)
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cache == nil {
|
||||||
|
h.twirpNotOK(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.Close() // commitCache needs the store closed
|
||||||
|
|
||||||
|
cache.Size, _ = cmp.Or(req.SizeBytes, req.SizeBytesCamel).Int64()
|
||||||
|
if err := h.commitCache(cache); err != nil {
|
||||||
|
h.logger.Errorf("finalize cache %d (%s): %v", cache.ID, cache.Key, err)
|
||||||
|
h.twirpNotOK(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.responseJSON(w, r, http.StatusOK, map[string]any{
|
||||||
|
"ok": true,
|
||||||
|
// int64 fields travel as strings in the proto JSON mapping.
|
||||||
|
"entryId": strconv.FormatUint(cache.ID, 10),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) v2GetCacheEntryDownloadURL(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
cred := credFromContext(r.Context())
|
||||||
|
req, err := decodeTwirpRequest[v2DownloadRequest](r)
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, "malformed_request", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := h.openDB()
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
cache, err := h.lookupCache(db, cred.Repo, req.keys(), req.Version)
|
||||||
|
if err != nil {
|
||||||
|
h.twirpError(w, r, twirpInternal, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cache == nil {
|
||||||
|
h.twirpNotOK(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.responseJSON(w, r, http.StatusOK, map[string]any{
|
||||||
|
"ok": true,
|
||||||
|
"signedDownloadUrl": h.signedArtifactURL(cache.ID, time.Now().Add(artifactURLTTL)),
|
||||||
|
"matchedKey": cache.Key,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// The archive arrives over the subset of the Azure blob API the toolkit uses: a small
|
||||||
|
// cache is a single PUT, a large one is staged as blocks that a final block list puts
|
||||||
|
// in order.
|
||||||
|
func (h *Handler) v2UploadBlob(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
|
id, err := strconv.ParseUint(params.ByName("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.touchCache(id, true); err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query := r.URL.Query()
|
||||||
|
switch strings.ToLower(query.Get("comp")) {
|
||||||
|
case "block":
|
||||||
|
blockID := query.Get("blockid")
|
||||||
|
if blockID == "" {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, errors.New("missing blockid"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = h.storage.WriteBlock(id, blockID, r.Body)
|
||||||
|
case "blocklist":
|
||||||
|
var list struct{ Latest []string }
|
||||||
|
if err := xml.NewDecoder(io.LimitReader(r.Body, 8<<20)).Decode(&list); err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusBadRequest, fmt.Errorf("malformed block list: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = h.storage.OrderBlocks(id, list.Latest)
|
||||||
|
default:
|
||||||
|
err = h.storage.Write(id, 0, r.Body)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
h.responseJSON(w, r, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}
|
||||||
|
|
||||||
|
// twirpNotOK is the negative answer all three endpoints share: no such entry to restore, no
|
||||||
|
// reservation to finalize, or an entry that already exists and need not be uploaded again.
|
||||||
|
func (h *Handler) twirpNotOK(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h.responseJSON(w, r, http.StatusOK, map[string]any{"ok": false})
|
||||||
|
}
|
||||||
|
|
||||||
|
// twirpError reports in the shape a twirp client expects, so the toolkit surfaces the message
|
||||||
|
// instead of a parse error.
|
||||||
|
func (h *Handler) twirpError(w http.ResponseWriter, r *http.Request, code string, err error) {
|
||||||
|
h.logger.Debugf("%s %s: %v", r.Method, r.URL.Path, err)
|
||||||
|
status := http.StatusBadRequest
|
||||||
|
if code == twirpInternal {
|
||||||
|
status = http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
h.responseJSON(w, r, status, map[string]any{"code": code, "msg": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
// The twirp request bodies. The toolkit's client serialises with useProtoFieldName, so the proto
|
||||||
|
// names are what arrive; the camelCase spellings of the same mapping are accepted too, as are
|
||||||
|
// int64s sent as a bare number rather than the string the mapping prescribes.
|
||||||
|
type (
|
||||||
|
v2CreateRequest struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
}
|
||||||
|
|
||||||
|
v2FinalizeRequest struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
SizeBytes json.Number `json:"size_bytes"`
|
||||||
|
SizeBytesCamel json.Number `json:"sizeBytes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
v2DownloadRequest struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
RestoreKeys []string `json:"restore_keys"`
|
||||||
|
RestoreKeysCamel []string `json:"restoreKeys"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d v2DownloadRequest) keys() []string {
|
||||||
|
restoreKeys := d.RestoreKeys
|
||||||
|
if len(restoreKeys) == 0 {
|
||||||
|
restoreKeys = d.RestoreKeysCamel
|
||||||
|
}
|
||||||
|
return append([]string{d.Key}, restoreKeys...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeTwirpRequest[T any](r *http.Request) (T, error) {
|
||||||
|
var req T
|
||||||
|
err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&req)
|
||||||
|
return req, err
|
||||||
|
}
|
||||||
236
act/artifactcache/handler_v2_test.go
Normal file
236
act/artifactcache/handler_v2_test.go
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package artifactcache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// v2Call posts a twirp request to the cache service and returns the decoded response.
|
||||||
|
// Field names are the proto ones, which is what the toolkit's client sends.
|
||||||
|
func v2Call(t *testing.T, handler *Handler, client *http.Client, method string, request any) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
body, err := json.Marshal(request)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
resp, err := client.Post(handler.ExternalURL()+cacheServiceV2Path+"/"+method, "application/json", bytes.NewReader(body))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
got := map[string]any{}
|
||||||
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
|
return got
|
||||||
|
}
|
||||||
|
|
||||||
|
// putBlob uploads to a signed URL and returns the status, so a test can assert a refusal.
|
||||||
|
func putBlob(t *testing.T, url string, content []byte) int {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(t.Context(), http.MethodPut, url, bytes.NewReader(content))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
return resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func getURL(t *testing.T, url string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, url, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func startTestHandler(t *testing.T) *Handler {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
handler, err := StartHandler(filepath.Join(t.TempDir(), "artifactcache"), "127.0.0.1", 0, "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = handler.Close() })
|
||||||
|
handler.RegisterJob(testToken, testRepo)
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// saveV2 runs the reserve/upload/finalize sequence and returns the finalize response along
|
||||||
|
// with the upload URL it used.
|
||||||
|
func saveV2(t *testing.T, handler *Handler, key, version string, content []byte) (finalized map[string]any, uploadURL string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
created := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": key, "version": version})
|
||||||
|
require.Equal(t, true, created["ok"])
|
||||||
|
uploadURL, _ = created["signedUploadUrl"].(string)
|
||||||
|
require.NotEmpty(t, uploadURL)
|
||||||
|
require.Equal(t, http.StatusCreated, putBlob(t, uploadURL, content))
|
||||||
|
|
||||||
|
return v2Call(t, handler, testClient, "FinalizeCacheEntryUpload", map[string]any{
|
||||||
|
"key": key, "version": version,
|
||||||
|
"size_bytes": strconv.Itoa(len(content)),
|
||||||
|
}), uploadURL
|
||||||
|
}
|
||||||
|
|
||||||
|
// The whole round trip an actions/cache v2 client makes, plus the guarantees on the signed
|
||||||
|
// URLs it is handed: unsigned requests are refused, an upload URL cannot be replayed to read
|
||||||
|
// or to replace a finalized entry.
|
||||||
|
func TestCacheServiceV2RoundTrip(t *testing.T) {
|
||||||
|
handler := startTestHandler(t)
|
||||||
|
content := []byte("the cached archive")
|
||||||
|
|
||||||
|
unsigned := fmt.Sprintf("%s%s/1", handler.ExternalURL(), blobPath)
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, putBlob(t, unsigned, content))
|
||||||
|
|
||||||
|
finalized, uploadURL := saveV2(t, handler, "deps-v1", "abc123", content)
|
||||||
|
require.Equal(t, true, finalized["ok"])
|
||||||
|
assert.NotEmpty(t, finalized["entryId"])
|
||||||
|
|
||||||
|
// The upload URL outlives the finalize call, so replaying it must not poison the entry,
|
||||||
|
// and it is an upload URL only: nothing reads a blob back through it.
|
||||||
|
assert.Equal(t, http.StatusBadRequest, putBlob(t, uploadURL, []byte("poisoned")))
|
||||||
|
resp, err := http.Get(uploadURL) //nolint:noctx // the URL is the server under test
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
|
||||||
|
|
||||||
|
got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{"key": "deps-v1", "version": "abc123"})
|
||||||
|
require.Equal(t, true, got["ok"])
|
||||||
|
assert.Equal(t, "deps-v1", got["matchedKey"])
|
||||||
|
downloadURL, _ := got["signedDownloadUrl"].(string)
|
||||||
|
require.NotEmpty(t, downloadURL)
|
||||||
|
assert.Equal(t, content, getURL(t, downloadURL))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A large archive is staged as blocks and only put in order by the final block list, so
|
||||||
|
// blocks that arrive out of order must still be assembled the way the client asked.
|
||||||
|
func TestCacheServiceV2BlockUpload(t *testing.T) {
|
||||||
|
handler := startTestHandler(t)
|
||||||
|
|
||||||
|
created := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "blocks", "version": "v1"})
|
||||||
|
uploadURL, _ := created["signedUploadUrl"].(string)
|
||||||
|
require.NotEmpty(t, uploadURL)
|
||||||
|
|
||||||
|
blocks := map[string][]byte{}
|
||||||
|
var order []string
|
||||||
|
for i, part := range []string{"hello ", "world", "!"} {
|
||||||
|
blockID := base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "block-%d", i))
|
||||||
|
blocks[blockID] = []byte(part)
|
||||||
|
order = append(order, blockID)
|
||||||
|
}
|
||||||
|
// Upload in an order that is not the block list order.
|
||||||
|
for _, blockID := range []string{order[2], order[0], order[1]} {
|
||||||
|
require.Equal(t, http.StatusCreated, putBlob(t, uploadURL+"&comp=block&blockid="+blockID, blocks[blockID]))
|
||||||
|
}
|
||||||
|
|
||||||
|
var list bytes.Buffer
|
||||||
|
list.WriteString(`<?xml version="1.0" encoding="utf-8"?><BlockList>`)
|
||||||
|
for _, blockID := range order {
|
||||||
|
fmt.Fprintf(&list, "<Latest>%s</Latest>", blockID)
|
||||||
|
}
|
||||||
|
list.WriteString(`</BlockList>`)
|
||||||
|
require.Equal(t, http.StatusCreated, putBlob(t, uploadURL+"&comp=blocklist", list.Bytes()))
|
||||||
|
|
||||||
|
finalized := v2Call(t, handler, testClient, "FinalizeCacheEntryUpload", map[string]any{
|
||||||
|
"key": "blocks", "version": "v1", "size_bytes": len("hello world!"),
|
||||||
|
})
|
||||||
|
require.Equal(t, true, finalized["ok"])
|
||||||
|
|
||||||
|
got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{"key": "blocks", "version": "v1"})
|
||||||
|
require.Equal(t, true, got["ok"])
|
||||||
|
assert.Equal(t, "hello world!", string(getURL(t, got["signedDownloadUrl"].(string))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheServiceV2Lookups(t *testing.T) {
|
||||||
|
handler := startTestHandler(t)
|
||||||
|
saved, _ := saveV2(t, handler, "deps-abc", "v1", []byte("x"))
|
||||||
|
require.Equal(t, true, saved["ok"])
|
||||||
|
|
||||||
|
t.Run("reports a miss for an unknown key", func(t *testing.T) {
|
||||||
|
got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{"key": "nothing", "version": "v1"})
|
||||||
|
assert.Equal(t, false, got["ok"])
|
||||||
|
})
|
||||||
|
|
||||||
|
// The toolkit serialises with the proto field names; the camelCase spellings of the same
|
||||||
|
// proto JSON mapping are accepted alongside them.
|
||||||
|
for _, field := range []string{"restore_keys", "restoreKeys"} {
|
||||||
|
t.Run("restore keys match by prefix, spelled "+field, func(t *testing.T) {
|
||||||
|
got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{
|
||||||
|
"key": "deps-zzz", field: []string{"deps-"}, "version": "v1",
|
||||||
|
})
|
||||||
|
require.Equal(t, true, got["ok"])
|
||||||
|
assert.Equal(t, "deps-abc", got["matchedKey"])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("an existing entry is not reserved twice", func(t *testing.T) {
|
||||||
|
again := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "deps-abc", "version": "v1"})
|
||||||
|
assert.Equal(t, false, again["ok"])
|
||||||
|
})
|
||||||
|
|
||||||
|
// A key that is only a prefix of an existing one is a different entry, so the
|
||||||
|
// reservation check must be exact and not a restore-key prefix match, or the shorter
|
||||||
|
// key would be reported as existing and silently never saved.
|
||||||
|
t.Run("a prefix of an existing key is still reserved", func(t *testing.T) {
|
||||||
|
reserved := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "deps", "version": "v1"})
|
||||||
|
require.Equal(t, true, reserved["ok"])
|
||||||
|
assert.NotEmpty(t, reserved["signedUploadUrl"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("finalizing without a reservation is not ok", func(t *testing.T) {
|
||||||
|
got := v2Call(t, handler, testClient, "FinalizeCacheEntryUpload", map[string]any{
|
||||||
|
"key": "never-reserved", "version": "v1", "size_bytes": 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, false, got["ok"])
|
||||||
|
})
|
||||||
|
|
||||||
|
// The size the client declares is what Commit validates the assembled archive against.
|
||||||
|
t.Run("finalizing with the wrong size is not ok", func(t *testing.T) {
|
||||||
|
created := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "wrong-size", "version": "v1"})
|
||||||
|
require.Equal(t, http.StatusCreated, putBlob(t, created["signedUploadUrl"].(string), []byte("four")))
|
||||||
|
|
||||||
|
got := v2Call(t, handler, testClient, "FinalizeCacheEntryUpload", map[string]any{
|
||||||
|
"key": "wrong-size", "version": "v1", "size_bytes": 99,
|
||||||
|
})
|
||||||
|
assert.Equal(t, false, got["ok"])
|
||||||
|
})
|
||||||
|
|
||||||
|
// Both API versions are served from one store, so an entry written through v2 is a hit for
|
||||||
|
// a v1 client asking for the same key and version.
|
||||||
|
t.Run("a v1 client sees an entry written through v2", func(t *testing.T) {
|
||||||
|
resp, err := testClient.Get(fmt.Sprintf("%s%s/cache?keys=deps-abc&version=v1", handler.ExternalURL(), apiPath))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
got := map[string]any{}
|
||||||
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||||
|
assert.Equal(t, "deps-abc", got["cacheKey"])
|
||||||
|
assert.NotEmpty(t, got["archiveLocation"])
|
||||||
|
})
|
||||||
|
|
||||||
|
// The cache of one repository must stay invisible to another, as it does for the v1 API.
|
||||||
|
t.Run("another repository sees nothing", func(t *testing.T) {
|
||||||
|
handler.RegisterJob("other-runtime-token", "other/repo")
|
||||||
|
otherClient := &http.Client{Transport: &bearerTransport{token: "other-runtime-token"}}
|
||||||
|
|
||||||
|
got := v2Call(t, handler, otherClient, "GetCacheEntryDownloadURL", map[string]any{"key": "deps-abc", "version": "v1"})
|
||||||
|
assert.Equal(t, false, got["ok"])
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ func (c *Request) ToCache() *Cache {
|
|||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
ID uint64 `json:"id" boltholdKey:"ID"`
|
ID uint64 `json:"id" boltholdKey:"ID"`
|
||||||
|
Repo string `json:"repo" boltholdIndex:"Repo"`
|
||||||
Key string `json:"key" boltholdIndex:"Key"`
|
Key string `json:"key" boltholdIndex:"Key"`
|
||||||
Version string `json:"version" boltholdIndex:"Version"`
|
Version string `json:"version" boltholdIndex:"Version"`
|
||||||
Size int64 `json:"cacheSize"`
|
Size int64 `json:"cacheSize"`
|
||||||
|
|||||||
@@ -5,12 +5,15 @@
|
|||||||
package artifactcache
|
package artifactcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
@@ -37,7 +40,10 @@ func (s *Storage) Exist(id uint64) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error {
|
func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error {
|
||||||
name := s.tempName(id, offset)
|
return s.writeFile(s.tempName(id, offset), reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Storage) writeFile(name string, reader io.Reader) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -51,6 +57,26 @@ func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Storage) WriteBlock(id uint64, blockID string, reader io.Reader) error {
|
||||||
|
return s.writeFile(s.blockName(id, blockID), reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrderBlocks renames the staged blocks into the order the block list gives. A block the list
|
||||||
|
// does not name keeps its staged name, which is how Commit leaves it out, as Azure drops it. One
|
||||||
|
// rename pass is safe because a staged name always carries blockFilePrefix and a target name
|
||||||
|
// never does, so no rename can collide with a block not yet moved.
|
||||||
|
func (s *Storage) OrderBlocks(id uint64, blockIDs []string) error {
|
||||||
|
for i, blockID := range blockIDs {
|
||||||
|
if err := os.Rename(s.blockName(id, blockID), s.tempName(id, int64(i))); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("block %q of cache %d was never uploaded: %w", blockID, id, err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Storage) Commit(id uint64, size int64) (int64, error) {
|
func (s *Storage) Commit(id uint64, size int64) (int64, error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = os.RemoveAll(s.tempDir(id))
|
_ = os.RemoveAll(s.tempDir(id))
|
||||||
@@ -65,6 +91,31 @@ func (s *Storage) Commit(id uint64, size int64) (int64, error) {
|
|||||||
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
written, err := assemble(name, tempNames)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
// If size is less than 0, it means the size is unknown.
|
||||||
|
// We can't check the size of the file, just skip the check.
|
||||||
|
// It happens when the request comes from old versions of actions, like `actions/cache@v2`.
|
||||||
|
if size >= 0 && written != size {
|
||||||
|
_ = os.Remove(name)
|
||||||
|
return 0, fmt.Errorf("broken file: %v != %v", written, size)
|
||||||
|
}
|
||||||
|
return written, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// assemble concatenates the uploaded parts into name. A single part, which is what the v2 API
|
||||||
|
// produces below the client's block threshold, is already the whole archive and is moved.
|
||||||
|
func assemble(name string, tempNames []string) (int64, error) {
|
||||||
|
if len(tempNames) == 1 {
|
||||||
|
info, err := os.Stat(tempNames[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return info.Size(), os.Rename(tempNames[0], name)
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Create(name)
|
file, err := os.Create(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -84,16 +135,6 @@ func (s *Storage) Commit(id uint64, size int64) (int64, error) {
|
|||||||
}
|
}
|
||||||
written += n
|
written += n
|
||||||
}
|
}
|
||||||
|
|
||||||
// If size is less than 0, it means the size is unknown.
|
|
||||||
// We can't check the size of the file, just skip the check.
|
|
||||||
// It happens when the request comes from old versions of actions, like `actions/cache@v2`.
|
|
||||||
if size >= 0 && written != size {
|
|
||||||
_ = file.Close()
|
|
||||||
_ = os.Remove(name)
|
|
||||||
return 0, fmt.Errorf("broken file: %v != %v", written, size)
|
|
||||||
}
|
|
||||||
|
|
||||||
return written, nil
|
return written, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +160,17 @@ func (s *Storage) tempName(id uint64, offset int64) string {
|
|||||||
return filepath.Join(s.tempDir(id), fmt.Sprintf("%016x", offset))
|
return filepath.Join(s.tempDir(id), fmt.Sprintf("%016x", offset))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// blockFilePrefix marks a staged, not yet ordered block, so that tempNames can keep it out of
|
||||||
|
// Commit's name-ordered concatenation.
|
||||||
|
const blockFilePrefix = "block-"
|
||||||
|
|
||||||
|
func (s *Storage) blockName(id uint64, blockID string) string {
|
||||||
|
// The block id is client-chosen (base64), so it is hashed rather than trusted as a
|
||||||
|
// path element.
|
||||||
|
sum := sha256.Sum256([]byte(blockID))
|
||||||
|
return filepath.Join(s.tempDir(id), blockFilePrefix+hex.EncodeToString(sum[:]))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Storage) tempNames(id uint64) ([]string, error) {
|
func (s *Storage) tempNames(id uint64) ([]string, error) {
|
||||||
dir := s.tempDir(id)
|
dir := s.tempDir(id)
|
||||||
files, err := os.ReadDir(dir)
|
files, err := os.ReadDir(dir)
|
||||||
@@ -127,7 +179,7 @@ func (s *Storage) tempNames(id uint64) ([]string, error) {
|
|||||||
}
|
}
|
||||||
var names []string
|
var names []string
|
||||||
for _, v := range files {
|
for _, v := range files {
|
||||||
if !v.IsDir() {
|
if !v.IsDir() && !strings.HasPrefix(v.Name(), blockFilePrefix) {
|
||||||
names = append(names, filepath.Join(dir, v.Name()))
|
names = append(names, filepath.Join(dir, v.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
act/artifactcache/testdata/example/example.yaml
vendored
30
act/artifactcache/testdata/example/example.yaml
vendored
@@ -1,30 +0,0 @@
|
|||||||
# Copied from https://github.com/actions/cache#example-cache-workflow
|
|
||||||
name: Caching Primes
|
|
||||||
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- run: env
|
|
||||||
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Cache Primes
|
|
||||||
id: cache-primes
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: prime-numbers
|
|
||||||
key: ${{ runner.os }}-primes-${{ github.run_id }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-primes
|
|
||||||
${{ runner.os }}
|
|
||||||
|
|
||||||
- name: Generate Prime Numbers
|
|
||||||
if: steps.cache-primes.outputs.cache-hit != 'true'
|
|
||||||
run: cat /proc/sys/kernel/random/uuid > prime-numbers
|
|
||||||
|
|
||||||
- name: Use Prime Numbers
|
|
||||||
run: cat prime-numbers
|
|
||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,24 +5,25 @@
|
|||||||
package artifacts
|
package artifacts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/fstest"
|
"testing/fstest"
|
||||||
|
"time"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
|
||||||
"gitea.com/gitea/act_runner/act/runner"
|
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type writableMapFile struct {
|
type writableMapFile struct {
|
||||||
@@ -202,7 +203,7 @@ func TestListArtifactContainer(t *testing.T) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(1, len(response.Value)) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Len(response.Value, 1)
|
||||||
assert.Equal("some/file", response.Value[0].Path)
|
assert.Equal("some/file", response.Value[0].Path)
|
||||||
assert.Equal("file", response.Value[0].ItemType)
|
assert.Equal("file", response.Value[0].ItemType)
|
||||||
assert.Equal("http://localhost/artifact/1/some/file/.", response.Value[0].ContentLocation)
|
assert.Equal("http://localhost/artifact/1/some/file/.", response.Value[0].ContentLocation)
|
||||||
@@ -234,89 +235,133 @@ func TestDownloadArtifactFile(t *testing.T) {
|
|||||||
assert.Equal("content", string(data))
|
assert.Equal("content", string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestJobFileInfo struct {
|
// TestArtifactFlow drives the real Serve() artifact server over a loopback socket, exercising
|
||||||
workdir string
|
// the same upload -> finalize -> list -> download protocol the upload-artifact/download-artifact
|
||||||
workflowPath string
|
// actions speak. Running it in-process (rather than from a job container) keeps it network-free
|
||||||
eventName string
|
// and reachable everywhere, including when the CI job is itself a container.
|
||||||
errorMessage string
|
|
||||||
platforms map[string]string
|
|
||||||
containerArchitecture string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
artifactsPath = path.Join(os.TempDir(), "test-artifacts")
|
|
||||||
artifactsAddr = "127.0.0.1"
|
|
||||||
artifactsPort = "12345"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestArtifactFlow(t *testing.T) {
|
func TestArtifactFlow(t *testing.T) {
|
||||||
if testing.Short() {
|
artifactPath := t.TempDir()
|
||||||
t.Skip("skipping integration test")
|
|
||||||
|
// Serve the exact routes Serve() wires up, on a real loopback socket via httptest. httptest
|
||||||
|
// picks a free port and Close() tears the server down synchronously — avoiding both the
|
||||||
|
// port-rebind race and Serve()'s detached ListenAndServe goroutine, which logger.Fatal()s
|
||||||
|
// (process exit) on a bind error and can outlive the test's temp-dir cleanup.
|
||||||
|
router := httprouter.New()
|
||||||
|
fsys := readWriteFSImpl{}
|
||||||
|
uploads(router, artifactPath, fsys)
|
||||||
|
downloads(router, artifactPath, fsys)
|
||||||
|
server := httptest.NewServer(router)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
baseURL := server.URL
|
||||||
|
client := server.Client()
|
||||||
|
client.Timeout = 5 * time.Second
|
||||||
|
|
||||||
|
// request performs one HTTP call and returns the status and body. The default transport adds
|
||||||
|
// Accept-Encoding: gzip and transparently decompresses, so gzipped downloads come back plain.
|
||||||
|
request := func(t *testing.T, method, rawURL string, body io.Reader, header http.Header) (int, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
req, err := http.NewRequest(method, rawURL, body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
maps.Copy(req.Header, header)
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return resp.StatusCode, data
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
t.Run("upload-and-download", func(t *testing.T) {
|
||||||
|
const runID, item, content = "1", "my-artifact/data.txt", "hello artifact\n"
|
||||||
|
|
||||||
cancel := Serve(ctx, artifactsPath, artifactsAddr, artifactsPort)
|
status, data := request(t, http.MethodPost, baseURL+"/_apis/pipelines/workflows/"+runID+"/artifacts", nil, nil)
|
||||||
defer cancel()
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
|
var prep FileContainerResourceURL
|
||||||
|
require.NoError(t, json.Unmarshal(data, &prep))
|
||||||
|
require.Equal(t, baseURL+"/upload/"+runID, prep.FileContainerResourceURL)
|
||||||
|
|
||||||
platforms := map[string]string{
|
status, data = request(t, http.MethodPut, prep.FileContainerResourceURL+"?itemPath="+url.QueryEscape(item), strings.NewReader(content), nil)
|
||||||
"ubuntu-latest": "node:16-buster", // Don't use node:16-buster-slim because it doesn't have curl command, which is used in the tests
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
}
|
var msg ResponseMessage
|
||||||
|
require.NoError(t, json.Unmarshal(data, &msg))
|
||||||
|
require.Equal(t, "success", msg.Message)
|
||||||
|
|
||||||
tables := []TestJobFileInfo{
|
status, data = request(t, http.MethodPatch, baseURL+"/_apis/pipelines/workflows/"+runID+"/artifacts", nil, nil)
|
||||||
{"testdata", "upload-and-download", "push", "", platforms, ""},
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
{"testdata", "GHSL-2023-004", "push", "", platforms, ""},
|
|
||||||
}
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
|
|
||||||
for _, table := range tables {
|
status, data = request(t, http.MethodGet, baseURL+"/_apis/pipelines/workflows/"+runID+"/artifacts", nil, nil)
|
||||||
runTestJobFile(ctx, t, table)
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
}
|
var list NamedFileContainerResourceURLResponse
|
||||||
}
|
require.NoError(t, json.Unmarshal(data, &list))
|
||||||
|
require.Equal(t, 1, list.Count)
|
||||||
|
require.Equal(t, "my-artifact", list.Value[0].Name)
|
||||||
|
|
||||||
func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
|
status, data = request(t, http.MethodGet, list.Value[0].FileContainerResourceURL+"?itemPath=my-artifact", nil, nil)
|
||||||
t.Run(tjfi.workflowPath, func(t *testing.T) {
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
fmt.Printf("::group::%s\n", tjfi.workflowPath) //nolint:forbidigo // pre-existing issue from nektos/act
|
var items ContainerItemResponse
|
||||||
|
require.NoError(t, json.Unmarshal(data, &items))
|
||||||
|
require.Len(t, items.Value, 1)
|
||||||
|
require.Equal(t, "file", items.Value[0].ItemType)
|
||||||
|
require.Equal(t, "my-artifact/data.txt", items.Value[0].Path)
|
||||||
|
|
||||||
if err := os.RemoveAll(artifactsPath); err != nil {
|
status, data = request(t, http.MethodGet, items.Value[0].ContentLocation, nil, nil)
|
||||||
panic(err)
|
require.Equal(t, http.StatusOK, status)
|
||||||
}
|
require.Equal(t, content, string(data))
|
||||||
|
|
||||||
workdir, err := filepath.Abs(tjfi.workdir)
|
stored, err := os.ReadFile(filepath.Join(artifactPath, runID, "my-artifact", "data.txt"))
|
||||||
assert.Nil(t, err, workdir) //nolint:testifylint // pre-existing issue from nektos/act
|
require.NoError(t, err)
|
||||||
fullWorkflowPath := filepath.Join(workdir, tjfi.workflowPath)
|
require.Equal(t, content, string(stored))
|
||||||
runnerConfig := &runner.Config{
|
})
|
||||||
Workdir: workdir,
|
|
||||||
BindWorkdir: false,
|
|
||||||
EventName: tjfi.eventName,
|
|
||||||
Platforms: tjfi.platforms,
|
|
||||||
ReuseContainers: false,
|
|
||||||
ContainerArchitecture: tjfi.containerArchitecture,
|
|
||||||
GitHubInstance: "github.com",
|
|
||||||
ArtifactServerPath: artifactsPath,
|
|
||||||
ArtifactServerAddr: artifactsAddr,
|
|
||||||
ArtifactServerPort: artifactsPort,
|
|
||||||
}
|
|
||||||
|
|
||||||
runner, err := runner.New(runnerConfig)
|
t.Run("gzip-roundtrip", func(t *testing.T) {
|
||||||
assert.Nil(t, err, tjfi.workflowPath) //nolint:testifylint // pre-existing issue from nektos/act
|
const runID, item, content = "2", "logs/app.log", "compressed payload\n"
|
||||||
|
|
||||||
planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true)
|
var buf bytes.Buffer
|
||||||
assert.Nil(t, err, fullWorkflowPath) //nolint:testifylint // pre-existing issue from nektos/act
|
gz := gzip.NewWriter(&buf)
|
||||||
|
_, err := gz.Write([]byte(content))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, gz.Close())
|
||||||
|
|
||||||
plan, err := planner.PlanEvent(tjfi.eventName)
|
status, data := request(t, http.MethodPut, baseURL+"/upload/"+runID+"?itemPath="+url.QueryEscape(item),
|
||||||
if err == nil {
|
&buf, http.Header{"Content-Encoding": []string{"gzip"}})
|
||||||
err = runner.NewPlanExecutor(plan)(ctx)
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
if tjfi.errorMessage == "" {
|
|
||||||
assert.Nil(t, err, fullWorkflowPath) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
} else {
|
|
||||||
assert.Error(t, err, tjfi.errorMessage) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
assert.Nil(t, plan)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("::endgroup::") //nolint:forbidigo // pre-existing issue from nektos/act
|
// stored compressed, with the server's gzip marker suffix
|
||||||
|
_, err = os.Stat(filepath.Join(artifactPath, runID, "logs", "app.log.gz__"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
status, data = request(t, http.MethodGet, baseURL+"/download/"+runID+"?itemPath=logs", nil, nil)
|
||||||
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
|
var items ContainerItemResponse
|
||||||
|
require.NoError(t, json.Unmarshal(data, &items))
|
||||||
|
require.Len(t, items.Value, 1)
|
||||||
|
require.Equal(t, "logs/app.log", items.Value[0].Path)
|
||||||
|
|
||||||
|
status, data = request(t, http.MethodGet, items.Value[0].ContentLocation, nil, nil)
|
||||||
|
require.Equal(t, http.StatusOK, status)
|
||||||
|
require.Equal(t, content, string(data))
|
||||||
|
})
|
||||||
|
|
||||||
|
// GHSL-2023-004: an itemPath that climbs out of the run directory must be neutralised so the
|
||||||
|
// blob cannot be written outside the artifact root.
|
||||||
|
t.Run("GHSL-2023-004", func(t *testing.T) {
|
||||||
|
const runID, content = "3", "contained\n"
|
||||||
|
|
||||||
|
status, data := request(t, http.MethodPut, baseURL+"/upload/"+runID+"?itemPath="+url.QueryEscape("../../escape.txt"),
|
||||||
|
strings.NewReader(content), nil)
|
||||||
|
require.Equal(t, http.StatusOK, status, string(data))
|
||||||
|
|
||||||
|
stored, err := os.ReadFile(filepath.Join(artifactPath, runID, "escape.txt"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, content, string(stored))
|
||||||
|
|
||||||
|
_, err = os.Stat(filepath.Join(filepath.Dir(artifactPath), "escape.txt"))
|
||||||
|
require.True(t, os.IsNotExist(err), "upload escaped the artifact root")
|
||||||
|
|
||||||
|
status, data = request(t, http.MethodGet, baseURL+"/artifact/"+runID+"/escape.txt", nil, nil)
|
||||||
|
require.Equal(t, http.StatusOK, status)
|
||||||
|
require.Equal(t, content, string(data))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,6 +390,43 @@ func TestMkdirFsImplSafeResolve(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadWriteFSWritableAndAppendable(t *testing.T) {
|
||||||
|
fsys := readWriteFSImpl{}
|
||||||
|
name := filepath.Join(t.TempDir(), "nested", "artifact.txt")
|
||||||
|
|
||||||
|
w, err := fsys.OpenWritable(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = w.Write([]byte("first"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, w.Close())
|
||||||
|
|
||||||
|
w, err = fsys.OpenAppendable(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = w.Write([]byte("-second"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, w.Close())
|
||||||
|
|
||||||
|
got, err := os.ReadFile(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "first-second", string(got))
|
||||||
|
|
||||||
|
w, err = fsys.OpenWritable(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = w.Write([]byte("replaced"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, w.Close())
|
||||||
|
|
||||||
|
got, err = os.ReadFile(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "replaced", string(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServeEmptyArtifactPathReturnsCancelableNoop(t *testing.T) {
|
||||||
|
cancel := Serve(t.Context(), "", "127.0.0.1", "0")
|
||||||
|
require.NotNil(t, cancel)
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
|
||||||
func TestDownloadArtifactFileUnsafePath(t *testing.T) {
|
func TestDownloadArtifactFileUnsafePath(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
|
|
||||||
name: "GHSL-2023-0004"
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test-artifacts:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo "hello world" > test.txt
|
|
||||||
- name: curl upload
|
|
||||||
run: curl --silent --show-error --fail ${ACTIONS_RUNTIME_URL}upload/1?itemPath=../../my-artifact/secret.txt --upload-file test.txt
|
|
||||||
- uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: my-artifact
|
|
||||||
path: test-artifacts
|
|
||||||
- name: 'Verify Artifact #1'
|
|
||||||
run: |
|
|
||||||
file="test-artifacts/secret.txt"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $file)" != "hello world" ] ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
- name: Verify download should work by clean extra dots
|
|
||||||
run: curl --silent --show-error --fail --path-as-is -o out.txt ${ACTIONS_RUNTIME_URL}artifact/1/../../../1/my-artifact/secret.txt
|
|
||||||
- name: 'Verify download content'
|
|
||||||
run: |
|
|
||||||
file="out.txt"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $file)" != "hello world" ] ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -1,230 +0,0 @@
|
|||||||
|
|
||||||
name: "Test that artifact uploads and downloads succeed"
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test-artifacts:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: mkdir -p path/to/artifact
|
|
||||||
- run: echo hello > path/to/artifact/world.txt
|
|
||||||
- uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: my-artifact
|
|
||||||
path: path/to/artifact/world.txt
|
|
||||||
|
|
||||||
- run: rm -rf path
|
|
||||||
|
|
||||||
- uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: my-artifact
|
|
||||||
- name: Display structure of downloaded files
|
|
||||||
run: ls -la
|
|
||||||
|
|
||||||
# Test end-to-end by uploading two artifacts and then downloading them
|
|
||||||
- name: Create artifact files
|
|
||||||
run: |
|
|
||||||
mkdir -p path/to/dir-1
|
|
||||||
mkdir -p path/to/dir-2
|
|
||||||
mkdir -p path/to/dir-3
|
|
||||||
mkdir -p path/to/dir-5
|
|
||||||
mkdir -p path/to/dir-6
|
|
||||||
mkdir -p path/to/dir-7
|
|
||||||
echo "Lorem ipsum dolor sit amet" > path/to/dir-1/file1.txt
|
|
||||||
echo "Hello world from file #2" > path/to/dir-2/file2.txt
|
|
||||||
echo "This is a going to be a test for a large enough file that should get compressed with GZip. The @actions/artifact package uses GZip to upload files. This text should have a compression ratio greater than 100% so it should get uploaded using GZip" > path/to/dir-3/gzip.txt
|
|
||||||
dd if=/dev/random of=path/to/dir-5/file5.rnd bs=1024 count=1024
|
|
||||||
dd if=/dev/random of=path/to/dir-6/file6.rnd bs=1024 count=$((10*1024))
|
|
||||||
dd if=/dev/random of=path/to/dir-7/file7.rnd bs=1024 count=$((10*1024))
|
|
||||||
|
|
||||||
# Upload a single file artifact
|
|
||||||
- name: 'Upload artifact #1'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Artifact-A'
|
|
||||||
path: path/to/dir-1/file1.txt
|
|
||||||
|
|
||||||
# Upload using a wildcard pattern, name should default to 'artifact' if not provided
|
|
||||||
- name: 'Upload artifact #2'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
path: path/**/dir*/
|
|
||||||
|
|
||||||
# Upload a directory that contains a file that will be uploaded with GZip
|
|
||||||
- name: 'Upload artifact #3'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'GZip-Artifact'
|
|
||||||
path: path/to/dir-3/
|
|
||||||
|
|
||||||
# Upload a directory that contains a file that will be uploaded with GZip
|
|
||||||
- name: 'Upload artifact #4'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Multi-Path-Artifact'
|
|
||||||
path: |
|
|
||||||
path/to/dir-1/*
|
|
||||||
path/to/dir-[23]/*
|
|
||||||
!path/to/dir-3/*.txt
|
|
||||||
|
|
||||||
# Upload a mid-size file artifact
|
|
||||||
- name: 'Upload artifact #5'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Mid-Size-Artifact'
|
|
||||||
path: path/to/dir-5/file5.rnd
|
|
||||||
|
|
||||||
# Upload a big file artifact
|
|
||||||
- name: 'Upload artifact #6'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Big-Artifact'
|
|
||||||
path: path/to/dir-6/file6.rnd
|
|
||||||
|
|
||||||
# Upload a big file artifact twice
|
|
||||||
- name: 'Upload artifact #7 (First)'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Big-Uploaded-Twice'
|
|
||||||
path: path/to/dir-7/file7.rnd
|
|
||||||
|
|
||||||
# Upload a big file artifact twice
|
|
||||||
- name: 'Upload artifact #7 (Second)'
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Big-Uploaded-Twice'
|
|
||||||
path: path/to/dir-7/file7.rnd
|
|
||||||
|
|
||||||
# Verify artifacts. Switch to download-artifact@v2 once it's out of preview
|
|
||||||
|
|
||||||
# Download Artifact #1 and verify the correctness of the content
|
|
||||||
- name: 'Download artifact #1'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Artifact-A'
|
|
||||||
path: some/new/path
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #1'
|
|
||||||
run: |
|
|
||||||
file="some/new/path/file1.txt"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $file)" != "Lorem ipsum dolor sit amet" ] ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Download Artifact #2 and verify the correctness of the content
|
|
||||||
- name: 'Download artifact #2'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'artifact'
|
|
||||||
path: some/other/path
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #2'
|
|
||||||
run: |
|
|
||||||
file1="some/other/path/to/dir-1/file1.txt"
|
|
||||||
file2="some/other/path/to/dir-2/file2.txt"
|
|
||||||
if [ ! -f $file1 -o ! -f $file2 ] ; then
|
|
||||||
echo "Expected files do not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $file1)" != "Lorem ipsum dolor sit amet" -o "$(cat $file2)" != "Hello world from file #2" ] ; then
|
|
||||||
echo "File contents of downloaded artifacts are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Download Artifact #3 and verify the correctness of the content
|
|
||||||
- name: 'Download artifact #3'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'GZip-Artifact'
|
|
||||||
path: gzip/artifact/path
|
|
||||||
|
|
||||||
# Because a directory was used as input during the upload the parent directories, path/to/dir-3/, should not be included in the uploaded artifact
|
|
||||||
- name: 'Verify Artifact #3'
|
|
||||||
run: |
|
|
||||||
gzipFile="gzip/artifact/path/gzip.txt"
|
|
||||||
if [ ! -f $gzipFile ] ; then
|
|
||||||
echo "Expected file do not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $gzipFile)" != "This is a going to be a test for a large enough file that should get compressed with GZip. The @actions/artifact package uses GZip to upload files. This text should have a compression ratio greater than 100% so it should get uploaded using GZip" ] ; then
|
|
||||||
echo "File contents of downloaded artifact is incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: 'Download artifact #4'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Multi-Path-Artifact'
|
|
||||||
path: multi/artifact
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #4'
|
|
||||||
run: |
|
|
||||||
file1="multi/artifact/dir-1/file1.txt"
|
|
||||||
file2="multi/artifact/dir-2/file2.txt"
|
|
||||||
if [ ! -f $file1 -o ! -f $file2 ] ; then
|
|
||||||
echo "Expected files do not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ "$(cat $file1)" != "Lorem ipsum dolor sit amet" -o "$(cat $file2)" != "Hello world from file #2" ] ; then
|
|
||||||
echo "File contents of downloaded artifacts are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: 'Download artifact #5'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Mid-Size-Artifact'
|
|
||||||
path: mid-size/artifact/path
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #5'
|
|
||||||
run: |
|
|
||||||
file="mid-size/artifact/path/file5.rnd"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if ! diff $file path/to/dir-5/file5.rnd ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: 'Download artifact #6'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Big-Artifact'
|
|
||||||
path: big/artifact/path
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #6'
|
|
||||||
run: |
|
|
||||||
file="big/artifact/path/file6.rnd"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if ! diff $file path/to/dir-6/file6.rnd ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: 'Download artifact #7'
|
|
||||||
uses: actions/download-artifact@v2
|
|
||||||
with:
|
|
||||||
name: 'Big-Uploaded-Twice'
|
|
||||||
path: big-uploaded-twice/artifact/path
|
|
||||||
|
|
||||||
- name: 'Verify Artifact #7'
|
|
||||||
run: |
|
|
||||||
file="big-uploaded-twice/artifact/path/file7.rnd"
|
|
||||||
if [ ! -f $file ] ; then
|
|
||||||
echo "Expected file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if ! diff $file path/to/dir-7/file7.rnd ; then
|
|
||||||
echo "File contents of downloaded artifact are incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
package common
|
package common
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
// CartesianProduct takes map of lists and returns list of unique tuples
|
// CartesianProduct takes map of lists and returns list of unique tuples
|
||||||
func CartesianProduct(mapOfLists map[string][]any) []map[string]any {
|
func CartesianProduct(mapOfLists map[string][]any) []map[string]any {
|
||||||
listNames := make([]string, 0)
|
listNames := make([]string, 0)
|
||||||
@@ -46,7 +48,7 @@ func cartN(a ...[]any) [][]any {
|
|||||||
for j, n := range n {
|
for j, n := range n {
|
||||||
pi[j] = a[j][n]
|
pi[j] = a[j][n]
|
||||||
}
|
}
|
||||||
for j := len(n) - 1; j >= 0; j-- {
|
for j := range slices.Backward(n) {
|
||||||
n[j]++
|
n[j]++
|
||||||
if n[j] < len(a[j]) {
|
if n[j] < len(a[j]) {
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -35,9 +35,9 @@ func TestCartesianProduct(t *testing.T) {
|
|||||||
"baz": {false, true},
|
"baz": {false, true},
|
||||||
}
|
}
|
||||||
output = CartesianProduct(input)
|
output = CartesianProduct(input)
|
||||||
assert.Len(output, 0) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Empty(output)
|
||||||
|
|
||||||
input = map[string][]any{}
|
input = map[string][]any{}
|
||||||
output = CartesianProduct(input)
|
output = CartesianProduct(input)
|
||||||
assert.Len(output, 0) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Empty(output)
|
||||||
}
|
}
|
||||||
|
|||||||
73
act/common/context_helpers_test.go
Normal file
73
act/common/context_helpers_test.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDryrunContext(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
if Dryrun(ctx) {
|
||||||
|
t.Fatal("plain context should not be dryrun")
|
||||||
|
}
|
||||||
|
if !Dryrun(WithDryrun(ctx, true)) {
|
||||||
|
t.Fatal("WithDryrun(true) should set dryrun")
|
||||||
|
}
|
||||||
|
if Dryrun(WithDryrun(ctx, false)) {
|
||||||
|
t.Fatal("WithDryrun(false) should clear dryrun")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobErrorContainer(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
err := errors.New("job failed")
|
||||||
|
|
||||||
|
SetJobError(ctx, err)
|
||||||
|
if got := JobError(ctx); got != nil {
|
||||||
|
t.Fatalf("JobError without container = %v, want nil", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = WithJobErrorContainer(ctx)
|
||||||
|
SetJobError(ctx, err)
|
||||||
|
if got := JobError(ctx); !errors.Is(got, err) {
|
||||||
|
t.Fatalf("JobError = %v, want %v", got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerAndHookContext(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
if Logger(ctx) != logrus.StandardLogger() {
|
||||||
|
t.Fatal("plain context should use standard logger")
|
||||||
|
}
|
||||||
|
if LoggerHook(ctx) != nil {
|
||||||
|
t.Fatal("plain context should not have a logger hook")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := logrus.New()
|
||||||
|
ctx = WithLogger(ctx, logger)
|
||||||
|
if Logger(ctx) != logger {
|
||||||
|
t.Fatal("WithLogger should set logger")
|
||||||
|
}
|
||||||
|
|
||||||
|
hook := testHook{}
|
||||||
|
ctx = WithLoggerHook(ctx, hook)
|
||||||
|
if LoggerHook(ctx) != hook {
|
||||||
|
t.Fatal("WithLoggerHook should set hook")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type testHook struct{}
|
||||||
|
|
||||||
|
func (testHook) Levels() []logrus.Level {
|
||||||
|
return logrus.AllLevels
|
||||||
|
}
|
||||||
|
|
||||||
|
func (testHook) Fire(*logrus.Entry) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Style is a specific style
|
|
||||||
type Style int
|
|
||||||
|
|
||||||
// Styles
|
|
||||||
const (
|
|
||||||
StyleDoubleLine = iota
|
|
||||||
StyleSingleLine
|
|
||||||
StyleDashedLine
|
|
||||||
StyleNoLine
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewPen creates a new pen
|
|
||||||
func NewPen(style Style, color int) *Pen {
|
|
||||||
bgcolor := 49
|
|
||||||
if os.Getenv("CLICOLOR") == "0" {
|
|
||||||
color = 0
|
|
||||||
bgcolor = 0
|
|
||||||
}
|
|
||||||
return &Pen{
|
|
||||||
style: style,
|
|
||||||
color: color,
|
|
||||||
bgcolor: bgcolor,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type styleDef struct {
|
|
||||||
cornerTL string
|
|
||||||
cornerTR string
|
|
||||||
cornerBL string
|
|
||||||
cornerBR string
|
|
||||||
lineH string
|
|
||||||
lineV string
|
|
||||||
}
|
|
||||||
|
|
||||||
var styleDefs = []styleDef{
|
|
||||||
{"\u2554", "\u2557", "\u255a", "\u255d", "\u2550", "\u2551"},
|
|
||||||
{"\u256d", "\u256e", "\u2570", "\u256f", "\u2500", "\u2502"},
|
|
||||||
{"\u250c", "\u2510", "\u2514", "\u2518", "\u254c", "\u254e"},
|
|
||||||
{" ", " ", " ", " ", " ", " "},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pen struct
|
|
||||||
type Pen struct {
|
|
||||||
style Style
|
|
||||||
color int
|
|
||||||
bgcolor int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drawing struct
|
|
||||||
type Drawing struct {
|
|
||||||
buf *strings.Builder
|
|
||||||
width int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pen) drawTopBars(buf io.Writer, labels ...string) {
|
|
||||||
style := styleDefs[p.style]
|
|
||||||
for _, label := range labels {
|
|
||||||
bar := strings.Repeat(style.lineH, len(label)+2)
|
|
||||||
fmt.Fprintf(buf, " ")
|
|
||||||
fmt.Fprintf(buf, "\x1b[%d;%dm", p.color, p.bgcolor)
|
|
||||||
fmt.Fprintf(buf, "%s%s%s", style.cornerTL, bar, style.cornerTR)
|
|
||||||
fmt.Fprintf(buf, "\x1b[%dm", 0)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(buf, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pen) drawBottomBars(buf io.Writer, labels ...string) {
|
|
||||||
style := styleDefs[p.style]
|
|
||||||
for _, label := range labels {
|
|
||||||
bar := strings.Repeat(style.lineH, len(label)+2)
|
|
||||||
fmt.Fprintf(buf, " ")
|
|
||||||
fmt.Fprintf(buf, "\x1b[%d;%dm", p.color, p.bgcolor)
|
|
||||||
fmt.Fprintf(buf, "%s%s%s", style.cornerBL, bar, style.cornerBR)
|
|
||||||
fmt.Fprintf(buf, "\x1b[%dm", 0)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(buf, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pen) drawLabels(buf io.Writer, labels ...string) {
|
|
||||||
style := styleDefs[p.style]
|
|
||||||
for _, label := range labels {
|
|
||||||
fmt.Fprintf(buf, " ")
|
|
||||||
fmt.Fprintf(buf, "\x1b[%d;%dm", p.color, p.bgcolor)
|
|
||||||
fmt.Fprintf(buf, "%s %s %s", style.lineV, label, style.lineV)
|
|
||||||
fmt.Fprintf(buf, "\x1b[%dm", 0)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(buf, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// DrawArrow between boxes
|
|
||||||
func (p *Pen) DrawArrow() *Drawing {
|
|
||||||
drawing := &Drawing{
|
|
||||||
buf: new(strings.Builder),
|
|
||||||
width: 1,
|
|
||||||
}
|
|
||||||
fmt.Fprintf(drawing.buf, "\x1b[%dm", p.color)
|
|
||||||
fmt.Fprintf(drawing.buf, "\u2b07")
|
|
||||||
fmt.Fprintf(drawing.buf, "\x1b[%dm", 0)
|
|
||||||
return drawing
|
|
||||||
}
|
|
||||||
|
|
||||||
// DrawBoxes to draw boxes
|
|
||||||
func (p *Pen) DrawBoxes(labels ...string) *Drawing {
|
|
||||||
width := 0
|
|
||||||
for _, l := range labels {
|
|
||||||
width += len(l) + 2 + 2 + 1
|
|
||||||
}
|
|
||||||
drawing := &Drawing{
|
|
||||||
buf: new(strings.Builder),
|
|
||||||
width: width,
|
|
||||||
}
|
|
||||||
p.drawTopBars(drawing.buf, labels...)
|
|
||||||
p.drawLabels(drawing.buf, labels...)
|
|
||||||
p.drawBottomBars(drawing.buf, labels...)
|
|
||||||
|
|
||||||
return drawing
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw to writer
|
|
||||||
func (d *Drawing) Draw(writer io.Writer, centerOnWidth int) {
|
|
||||||
padSize := max((centerOnWidth-d.GetWidth())/2, 0)
|
|
||||||
for l := range strings.SplitSeq(d.buf.String(), "\n") {
|
|
||||||
if len(l) > 0 {
|
|
||||||
padding := strings.Repeat(" ", padSize)
|
|
||||||
fmt.Fprintf(writer, "%s%s\n", padding, l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetWidth of drawing
|
|
||||||
func (d *Drawing) GetWidth() int {
|
|
||||||
return d.width
|
|
||||||
}
|
|
||||||
@@ -12,24 +12,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Warning that implements `error` but safe to ignore
|
|
||||||
type Warning struct {
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error the contract for error
|
|
||||||
func (w Warning) Error() string {
|
|
||||||
return w.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warningf create a warning
|
|
||||||
func Warningf(format string, args ...any) Warning {
|
|
||||||
w := Warning{
|
|
||||||
Message: fmt.Sprintf(format, args...),
|
|
||||||
}
|
|
||||||
return w
|
|
||||||
}
|
|
||||||
|
|
||||||
// Executor define contract for the steps of a workflow
|
// Executor define contract for the steps of a workflow
|
||||||
type Executor func(ctx context.Context) error
|
type Executor func(ctx context.Context) error
|
||||||
|
|
||||||
@@ -97,6 +79,12 @@ func NewErrorExecutor(err error) Executor {
|
|||||||
|
|
||||||
// NewParallelExecutor creates a new executor from a parallel of other executors
|
// NewParallelExecutor creates a new executor from a parallel of other executors
|
||||||
func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
||||||
|
if len(executors) == 0 {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
work := make(chan Executor, len(executors))
|
work := make(chan Executor, len(executors))
|
||||||
errs := make(chan error, len(executors))
|
errs := make(chan error, len(executors))
|
||||||
@@ -117,7 +105,7 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
|||||||
log.Debugf("Worker %d executing task %d", workerID, taskCount)
|
log.Debugf("Worker %d executing task %d", workerID, taskCount)
|
||||||
// Recover from panics in executors to avoid crashing the worker
|
// Recover from panics in executors to avoid crashing the worker
|
||||||
// goroutine which would leave the runner process hung.
|
// goroutine which would leave the runner process hung.
|
||||||
// https://gitea.com/gitea/act_runner/issues/371
|
// https://gitea.com/gitea/runner/issues/371
|
||||||
errs <- func() (err error) {
|
errs <- func() (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
@@ -156,14 +144,8 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
|||||||
// Then runs another executor if this executor succeeds
|
// Then runs another executor if this executor succeeds
|
||||||
func (e Executor) Then(then Executor) Executor {
|
func (e Executor) Then(then Executor) Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
err := e(ctx)
|
if err := e(ctx); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
switch err.(type) {
|
|
||||||
case Warning:
|
|
||||||
Logger(ctx).Warning(err.Error())
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Simple fast test that verifies max-parallel: 2 limits concurrency
|
|
||||||
func TestMaxParallel2Quick(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
var currentRunning atomic.Int32
|
|
||||||
var maxSimultaneous atomic.Int32
|
|
||||||
|
|
||||||
executors := make([]Executor, 4)
|
|
||||||
for i := range 4 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
current := currentRunning.Add(1)
|
|
||||||
|
|
||||||
// Update max if needed
|
|
||||||
for {
|
|
||||||
maxValue := maxSimultaneous.Load()
|
|
||||||
if current <= maxValue || maxSimultaneous.CompareAndSwap(maxValue, current) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
currentRunning.Add(-1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err := NewParallelExecutor(2, executors...)(ctx)
|
|
||||||
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.LessOrEqual(t, maxSimultaneous.Load(), int32(2),
|
|
||||||
"Should not exceed max-parallel: 2")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test that verifies max-parallel: 1 enforces sequential execution
|
|
||||||
func TestMaxParallel1Sequential(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
var currentRunning atomic.Int32
|
|
||||||
var maxSimultaneous atomic.Int32
|
|
||||||
var executionOrder []int
|
|
||||||
var orderMutex sync.Mutex
|
|
||||||
|
|
||||||
executors := make([]Executor, 5)
|
|
||||||
for i := range 5 {
|
|
||||||
taskID := i
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
current := currentRunning.Add(1)
|
|
||||||
|
|
||||||
// Track execution order
|
|
||||||
orderMutex.Lock()
|
|
||||||
executionOrder = append(executionOrder, taskID)
|
|
||||||
orderMutex.Unlock()
|
|
||||||
|
|
||||||
// Update max if needed
|
|
||||||
for {
|
|
||||||
maxValue := maxSimultaneous.Load()
|
|
||||||
if current <= maxValue || maxSimultaneous.CompareAndSwap(maxValue, current) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(20 * time.Millisecond)
|
|
||||||
currentRunning.Add(-1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err := NewParallelExecutor(1, executors...)(ctx)
|
|
||||||
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Equal(t, int32(1), maxSimultaneous.Load(),
|
|
||||||
"max-parallel: 1 should only run 1 task at a time")
|
|
||||||
assert.Len(t, executionOrder, 5, "All 5 tasks should have executed")
|
|
||||||
}
|
|
||||||
@@ -1,283 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestMaxParallelJobExecution tests actual job execution with max-parallel
|
|
||||||
func TestMaxParallelJobExecution(t *testing.T) {
|
|
||||||
t.Run("MaxParallel=1 Sequential", func(t *testing.T) {
|
|
||||||
var currentRunning atomic.Int32
|
|
||||||
var maxConcurrent int32
|
|
||||||
var executionOrder []int
|
|
||||||
var mu sync.Mutex
|
|
||||||
|
|
||||||
executors := make([]Executor, 5)
|
|
||||||
for i := range 5 {
|
|
||||||
taskID := i
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
current := currentRunning.Add(1)
|
|
||||||
|
|
||||||
// Track max concurrent
|
|
||||||
for {
|
|
||||||
maxValue := atomic.LoadInt32(&maxConcurrent)
|
|
||||||
if current <= maxValue || atomic.CompareAndSwapInt32(&maxConcurrent, maxValue, current) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mu.Lock()
|
|
||||||
executionOrder = append(executionOrder, taskID)
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
currentRunning.Add(-1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
err := NewParallelExecutor(1, executors...)(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, int32(1), maxConcurrent, "Should never exceed 1 concurrent execution")
|
|
||||||
assert.Len(t, executionOrder, 5, "All tasks should execute")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("MaxParallel=3 Limited", func(t *testing.T) {
|
|
||||||
var currentRunning atomic.Int32
|
|
||||||
var maxConcurrent int32
|
|
||||||
|
|
||||||
executors := make([]Executor, 10)
|
|
||||||
for i := range 10 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
current := currentRunning.Add(1)
|
|
||||||
|
|
||||||
for {
|
|
||||||
maxValue := atomic.LoadInt32(&maxConcurrent)
|
|
||||||
if current <= maxValue || atomic.CompareAndSwapInt32(&maxConcurrent, maxValue, current) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(20 * time.Millisecond)
|
|
||||||
currentRunning.Add(-1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
err := NewParallelExecutor(3, executors...)(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.LessOrEqual(t, int(maxConcurrent), 3, "Should never exceed 3 concurrent executions")
|
|
||||||
assert.GreaterOrEqual(t, int(maxConcurrent), 1, "Should have at least 1 concurrent execution")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("MaxParallel=0 Uses1Worker", func(t *testing.T) {
|
|
||||||
var maxConcurrent int32
|
|
||||||
var currentRunning atomic.Int32
|
|
||||||
|
|
||||||
executors := make([]Executor, 5)
|
|
||||||
for i := range 5 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
current := currentRunning.Add(1)
|
|
||||||
|
|
||||||
for {
|
|
||||||
maxValue := atomic.LoadInt32(&maxConcurrent)
|
|
||||||
if current <= maxValue || atomic.CompareAndSwapInt32(&maxConcurrent, maxValue, current) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
currentRunning.Add(-1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
// When maxParallel is 0 or negative, it defaults to 1
|
|
||||||
err := NewParallelExecutor(0, executors...)(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, int32(1), maxConcurrent, "Should use 1 worker when max-parallel is 0")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMaxParallelWithErrors tests error handling with max-parallel
|
|
||||||
func TestMaxParallelWithErrors(t *testing.T) {
|
|
||||||
t.Run("OneTaskFailsOthersContinue", func(t *testing.T) {
|
|
||||||
var successCount int32
|
|
||||||
|
|
||||||
executors := make([]Executor, 5)
|
|
||||||
for i := range 5 {
|
|
||||||
taskID := i
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
if taskID == 2 {
|
|
||||||
return assert.AnError
|
|
||||||
}
|
|
||||||
atomic.AddInt32(&successCount, 1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
err := NewParallelExecutor(2, executors...)(ctx)
|
|
||||||
|
|
||||||
// Should return the error from task 2
|
|
||||||
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
// Other tasks should still execute
|
|
||||||
assert.Equal(t, int32(4), successCount, "4 tasks should succeed")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ContextCancellation", func(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
var startedCount int32
|
|
||||||
executors := make([]Executor, 10)
|
|
||||||
for i := range 10 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
atomic.AddInt32(&startedCount, 1)
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel after a short delay
|
|
||||||
go func() {
|
|
||||||
time.Sleep(30 * time.Millisecond)
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
|
|
||||||
err := NewParallelExecutor(3, executors...)(ctx)
|
|
||||||
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.ErrorIs(t, err, context.Canceled) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
// Not all tasks should start due to cancellation (but timing may vary)
|
|
||||||
// Just verify cancellation occurred
|
|
||||||
t.Logf("Started %d tasks before cancellation", startedCount)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMaxParallelPerformance tests performance characteristics
|
|
||||||
func TestMaxParallelPerformance(t *testing.T) {
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("Skipping performance test in short mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run("ParallelFasterThanSequential", func(t *testing.T) {
|
|
||||||
executors := make([]Executor, 10)
|
|
||||||
for i := range 10 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Sequential (max-parallel=1)
|
|
||||||
start := time.Now()
|
|
||||||
err := NewParallelExecutor(1, executors...)(ctx)
|
|
||||||
sequentialDuration := time.Since(start)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
// Parallel (max-parallel=5)
|
|
||||||
start = time.Now()
|
|
||||||
err = NewParallelExecutor(5, executors...)(ctx)
|
|
||||||
parallelDuration := time.Since(start)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
// Parallel should be significantly faster
|
|
||||||
assert.Less(t, parallelDuration, sequentialDuration/2,
|
|
||||||
"Parallel execution should be at least 2x faster")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("OptimalWorkerCount", func(t *testing.T) {
|
|
||||||
executors := make([]Executor, 20)
|
|
||||||
for i := range 20 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Test with different worker counts
|
|
||||||
workerCounts := []int{1, 2, 5, 10, 20}
|
|
||||||
durations := make(map[int]time.Duration)
|
|
||||||
|
|
||||||
for _, count := range workerCounts {
|
|
||||||
start := time.Now()
|
|
||||||
err := NewParallelExecutor(count, executors...)(ctx)
|
|
||||||
durations[count] = time.Since(start)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
|
|
||||||
// More workers should generally be faster (up to a point)
|
|
||||||
assert.Less(t, durations[5], durations[1], "5 workers should be faster than 1")
|
|
||||||
assert.Less(t, durations[10], durations[2], "10 workers should be faster than 2")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMaxParallelResourceSharing tests resource sharing scenarios
|
|
||||||
func TestMaxParallelResourceSharing(t *testing.T) {
|
|
||||||
t.Run("SharedResourceWithMutex", func(t *testing.T) {
|
|
||||||
var sharedCounter int
|
|
||||||
var mu sync.Mutex
|
|
||||||
|
|
||||||
executors := make([]Executor, 100)
|
|
||||||
for i := range 100 {
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
mu.Lock()
|
|
||||||
sharedCounter++
|
|
||||||
mu.Unlock()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
err := NewParallelExecutor(10, executors...)(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, 100, sharedCounter, "All tasks should increment counter")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ChannelCommunication", func(t *testing.T) {
|
|
||||||
resultChan := make(chan int, 50)
|
|
||||||
|
|
||||||
executors := make([]Executor, 50)
|
|
||||||
for i := range 50 {
|
|
||||||
taskID := i
|
|
||||||
executors[i] = func(ctx context.Context) error {
|
|
||||||
resultChan <- taskID
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
err := NewParallelExecutor(5, executors...)(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
close(resultChan)
|
|
||||||
|
|
||||||
results := make(map[int]bool)
|
|
||||||
for result := range resultChan {
|
|
||||||
results[result] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Len(t, results, 50, "All task IDs should be received")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -7,11 +7,14 @@ package common
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewWorkflow(t *testing.T) {
|
func TestNewWorkflow(t *testing.T) {
|
||||||
@@ -21,11 +24,11 @@ func TestNewWorkflow(t *testing.T) {
|
|||||||
|
|
||||||
// empty
|
// empty
|
||||||
emptyWorkflow := NewPipelineExecutor()
|
emptyWorkflow := NewPipelineExecutor()
|
||||||
assert.Nil(emptyWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(emptyWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// error case
|
// error case
|
||||||
errorWorkflow := NewErrorExecutor(errors.New("test error"))
|
errorWorkflow := NewErrorExecutor(errors.New("test error"))
|
||||||
assert.NotNil(errorWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(errorWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// multiple success case
|
// multiple success case
|
||||||
runcount := 0
|
runcount := 0
|
||||||
@@ -38,7 +41,7 @@ func TestNewWorkflow(t *testing.T) {
|
|||||||
runcount++
|
runcount++
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
assert.Nil(successWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(successWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(2, runcount)
|
assert.Equal(2, runcount)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +63,7 @@ func TestNewConditionalExecutor(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})(ctx)
|
})(ctx)
|
||||||
|
|
||||||
assert.Nil(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(0, trueCount)
|
assert.Equal(0, trueCount)
|
||||||
assert.Equal(1, falseCount)
|
assert.Equal(1, falseCount)
|
||||||
|
|
||||||
@@ -74,49 +77,63 @@ func TestNewConditionalExecutor(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})(ctx)
|
})(ctx)
|
||||||
|
|
||||||
assert.Nil(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(1, trueCount)
|
assert.Equal(1, trueCount)
|
||||||
assert.Equal(1, falseCount)
|
assert.Equal(1, falseCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewParallelExecutor(t *testing.T) {
|
// concurrencyProbe returns an executor recording the peak number of concurrent copies. Copies
|
||||||
assert := assert.New(t)
|
// block until wantActive are in flight so the peak is exact without sleeping, and later copies
|
||||||
|
// find the gate already open so the last one still finishes with no partner left.
|
||||||
|
func concurrencyProbe(wantActive int32) (exec Executor, count, maxActive *atomic.Int32) {
|
||||||
|
var counted, active, peak atomic.Int32
|
||||||
|
var once sync.Once
|
||||||
|
reached := make(chan struct{})
|
||||||
|
|
||||||
ctx := context.Background()
|
return func(ctx context.Context) error {
|
||||||
|
counted.Add(1)
|
||||||
var count, activeCount, maxCount atomic.Int32
|
running := active.Add(1)
|
||||||
emptyWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
||||||
count.Add(1)
|
|
||||||
|
|
||||||
active := activeCount.Add(1)
|
|
||||||
for {
|
for {
|
||||||
m := maxCount.Load()
|
seen := peak.Load()
|
||||||
if active <= m || maxCount.CompareAndSwap(m, active) {
|
if running <= seen || peak.CompareAndSwap(seen, running) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(2 * time.Second)
|
if running >= wantActive {
|
||||||
activeCount.Add(-1)
|
once.Do(func() { close(reached) })
|
||||||
|
}
|
||||||
|
<-reached
|
||||||
|
active.Add(-1)
|
||||||
return nil
|
return nil
|
||||||
})
|
}, &counted, &peak
|
||||||
|
}
|
||||||
|
|
||||||
err := NewParallelExecutor(2, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
func TestNewParallelExecutor(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
assert.Equal(int32(3), count.Load(), "should run all 3 executors")
|
exec, count, maxActive := concurrencyProbe(2)
|
||||||
assert.Equal(int32(2), maxCount.Load(), "should run at most 2 executors in parallel")
|
require.NoError(t, NewParallelExecutor(2, exec, exec, exec)(ctx))
|
||||||
assert.Nil(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, int32(3), count.Load(), "should run all 3 executors")
|
||||||
|
assert.Equal(t, int32(2), maxActive.Load(), "should run at most 2 executors in parallel")
|
||||||
|
|
||||||
// Reset to test running the executor with 0 parallelism
|
// parallelism below 1 falls back to a single worker
|
||||||
count.Store(0)
|
exec, count, maxActive = concurrencyProbe(1)
|
||||||
activeCount.Store(0)
|
require.NoError(t, NewParallelExecutor(0, exec, exec, exec)(ctx))
|
||||||
maxCount.Store(0)
|
assert.Equal(t, int32(3), count.Load(), "should run all 3 executors")
|
||||||
|
assert.Equal(t, int32(1), maxActive.Load(), "should run at most 1 executor in parallel")
|
||||||
|
}
|
||||||
|
|
||||||
errSingle := NewParallelExecutor(0, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
func TestNewParallelExecutorEmpty(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
assert.Equal(int32(3), count.Load(), "should run all 3 executors")
|
ctx := context.Background()
|
||||||
assert.Equal(int32(1), maxCount.Load(), "should run at most 1 executors in parallel")
|
require.NoError(t, NewParallelExecutor(2)(ctx))
|
||||||
assert.Nil(errSingle) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
canceledCtx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
err := NewParallelExecutor(2)(canceledCtx)
|
||||||
|
assert.ErrorIs(err, context.Canceled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewParallelExecutorFailed(t *testing.T) {
|
func TestNewParallelExecutorFailed(t *testing.T) {
|
||||||
@@ -156,3 +173,60 @@ func TestNewParallelExecutorCanceled(t *testing.T) {
|
|||||||
assert.Equal(int32(3), count.Load())
|
assert.Equal(int32(3), count.Load())
|
||||||
assert.Error(errExpected, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(errExpected, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewParallelExecutorRunsRemainingAfterFailure(t *testing.T) {
|
||||||
|
var successCount atomic.Int32
|
||||||
|
executors := make([]Executor, 5)
|
||||||
|
for i := range executors {
|
||||||
|
executors[i] = func(ctx context.Context) error {
|
||||||
|
if i == 2 {
|
||||||
|
return errors.New("fake error")
|
||||||
|
}
|
||||||
|
successCount.Add(1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
require.Error(t, NewParallelExecutor(2, executors...)(context.Background()))
|
||||||
|
assert.Equal(t, int32(4), successCount.Load(), "a failing executor must not stop the others")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecutorConditionalsAndFinally(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
var calls []string
|
||||||
|
record := func(name string) Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
calls = append(calls, name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, record("if-true").If(func(context.Context) bool { return true })(ctx))
|
||||||
|
require.NoError(t, record("if-false").If(func(context.Context) bool { return false })(ctx))
|
||||||
|
require.NoError(t, record("if-not").IfNot(func(context.Context) bool { return false })(ctx))
|
||||||
|
require.NoError(t, record("if-bool").IfBool(true)(ctx))
|
||||||
|
require.NoError(t, record("main").Finally(record("finally"))(ctx))
|
||||||
|
|
||||||
|
want := []string{"if-true", "if-not", "if-bool", "main", "finally"}
|
||||||
|
if !reflect.DeepEqual(calls, want) {
|
||||||
|
t.Fatalf("calls = %v, want %v", calls, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecutorFinallyReturnsFinallyErrorWithOriginal(t *testing.T) {
|
||||||
|
mainErr := errors.New("main failed")
|
||||||
|
finalErr := errors.New("cleanup failed")
|
||||||
|
|
||||||
|
err := NewErrorExecutor(mainErr).Finally(NewErrorExecutor(finalErr))(context.Background())
|
||||||
|
require.Error(t, err)
|
||||||
|
if !strings.Contains(err.Error(), "cleanup failed") || !strings.Contains(err.Error(), "main failed") {
|
||||||
|
t.Fatalf("finally error = %q, want both cleanup and original error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConditionalNot(t *testing.T) {
|
||||||
|
cond := Conditional(func(context.Context) bool { return false })
|
||||||
|
if !cond.Not()(context.Background()) {
|
||||||
|
t.Fatal("inverted conditional should be true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CopyFile copy file
|
|
||||||
func CopyFile(source, dest string) (err error) {
|
|
||||||
sourcefile, err := os.Open(source)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer sourcefile.Close()
|
|
||||||
|
|
||||||
destfile, err := os.Create(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer destfile.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(destfile, sourcefile)
|
|
||||||
if err == nil {
|
|
||||||
sourceinfo, err := os.Stat(source)
|
|
||||||
if err != nil {
|
|
||||||
_ = os.Chmod(dest, sourceinfo.Mode())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CopyDir recursive copy of directory
|
|
||||||
func CopyDir(source, dest string) (err error) {
|
|
||||||
// get properties of source dir
|
|
||||||
sourceinfo, err := os.Stat(source)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// create dest dir
|
|
||||||
|
|
||||||
err = os.MkdirAll(dest, sourceinfo.Mode())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
objects, err := os.ReadDir(source)
|
|
||||||
|
|
||||||
for _, obj := range objects {
|
|
||||||
sourcefilepointer := source + "/" + obj.Name()
|
|
||||||
|
|
||||||
destinationfilepointer := dest + "/" + obj.Name()
|
|
||||||
|
|
||||||
if obj.IsDir() {
|
|
||||||
// create sub-directories - recursively
|
|
||||||
err = CopyDir(sourcefilepointer, destinationfilepointer)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err) //nolint:forbidigo // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// perform copy
|
|
||||||
err = CopyFile(sourcefilepointer, destinationfilepointer)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err) //nolint:forbidigo // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/internal/pkg/lock"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/config"
|
"github.com/go-git/go-git/v5/config"
|
||||||
@@ -32,12 +33,20 @@ var (
|
|||||||
githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`)
|
githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`)
|
||||||
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+?)(?:.git)?$`)
|
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+?)(?:.git)?$`)
|
||||||
|
|
||||||
cloneLock sync.Mutex
|
cloneLocks lock.Keyed[string] // key: clone target directory
|
||||||
|
|
||||||
ErrShortRef = errors.New("short SHA references are not supported")
|
ErrShortRef = errors.New("short SHA references are not supported")
|
||||||
ErrNoRepo = errors.New("unable to find git repo")
|
ErrNoRepo = errors.New("unable to find git repo")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AcquireCloneLock returns an unlock function after locking the per-directory mutex for dir.
|
||||||
|
// Only concurrent operations targeting the same directory are serialized; clones into different directories run in parallel.
|
||||||
|
// Callers reading files inside dir (e.g. tarring a checked-out action into a job container) must hold this lock too,
|
||||||
|
// otherwise a concurrent NewGitCloneExecutor on the same dir can mutate the worktree mid-read.
|
||||||
|
func AcquireCloneLock(dir string) func() {
|
||||||
|
return cloneLocks.Lock(dir)
|
||||||
|
}
|
||||||
|
|
||||||
type Error struct {
|
type Error struct {
|
||||||
err error
|
err error
|
||||||
commit string
|
commit string
|
||||||
@@ -55,8 +64,21 @@ func (e *Error) Commit() string {
|
|||||||
return e.commit
|
return e.commit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// goGitMu serializes go-git repository access across the process. go-git is not safe for
|
||||||
|
// concurrent use of the same repository (even read access decodes packfiles into shared
|
||||||
|
// state), so parallel jobs inspecting the shared workdir repo race without this. The guarded
|
||||||
|
// operations are fast local reads; gitea runs one job per process, so the lock is effectively
|
||||||
|
// uncontended in production.
|
||||||
|
var goGitMu sync.Mutex
|
||||||
|
|
||||||
// FindGitRevision get the current git revision
|
// FindGitRevision get the current git revision
|
||||||
func FindGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) {
|
func FindGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) {
|
||||||
|
goGitMu.Lock()
|
||||||
|
defer goGitMu.Unlock()
|
||||||
|
return findGitRevision(ctx, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
gitDir, err := git.PlainOpenWithOptions(
|
gitDir, err := git.PlainOpenWithOptions(
|
||||||
@@ -88,10 +110,13 @@ func FindGitRevision(ctx context.Context, file string) (shortSha, sha string, er
|
|||||||
|
|
||||||
// FindGitRef get the current git ref
|
// FindGitRef get the current git ref
|
||||||
func FindGitRef(ctx context.Context, file string) (string, error) {
|
func FindGitRef(ctx context.Context, file string) (string, error) {
|
||||||
|
goGitMu.Lock()
|
||||||
|
defer goGitMu.Unlock()
|
||||||
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
logger.Debugf("Loading revision from git directory")
|
logger.Debugf("Loading revision from git directory")
|
||||||
_, ref, err := FindGitRevision(ctx, file)
|
_, ref, err := findGitRevision(ctx, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -163,6 +188,8 @@ func FindGitRef(ctx context.Context, file string) (string, error) {
|
|||||||
|
|
||||||
// FindGithubRepo get the repo
|
// FindGithubRepo get the repo
|
||||||
func FindGithubRepo(ctx context.Context, file, githubInstance, remoteName string) (string, error) {
|
func FindGithubRepo(ctx context.Context, file, githubInstance, remoteName string) (string, error) {
|
||||||
|
goGitMu.Lock()
|
||||||
|
defer goGitMu.Unlock()
|
||||||
if remoteName == "" {
|
if remoteName == "" {
|
||||||
remoteName = "origin"
|
remoteName = "origin"
|
||||||
}
|
}
|
||||||
@@ -228,51 +255,77 @@ type NewGitCloneExecutorInput struct {
|
|||||||
Token string
|
Token string
|
||||||
OfflineMode bool
|
OfflineMode bool
|
||||||
|
|
||||||
|
// Depth limits the clone/fetch to the given number of commits from the tip of the requested ref.
|
||||||
|
// 0 for full clone.
|
||||||
|
Depth int
|
||||||
|
|
||||||
|
// Quiet drops the informational clone line to debug level, for callers that log their own
|
||||||
|
// download summary (the setup section's action report).
|
||||||
|
Quiet bool
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
InsecureSkipTLS bool
|
InsecureSkipTLS bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloneIfRequired ...
|
// CloneIfRequired returns the repository and a boolean indicating whether an existing local clone was reused.
|
||||||
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, error) {
|
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, bool, error) {
|
||||||
r, err := git.PlainOpen(input.Dir)
|
r, err := git.PlainOpen(input.Dir)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
var progressWriter io.Writer
|
// Verify the cached clone still points to the resolved URL before reusing it.
|
||||||
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
remote, err := r.Remote("origin")
|
||||||
if entry, ok := logger.(*log.Entry); ok {
|
if err == nil && len(remote.Config().URLs) > 0 && remote.Config().URLs[0] == input.URL {
|
||||||
progressWriter = entry.WriterLevel(log.DebugLevel)
|
// Reuse existing clone
|
||||||
} else if lgr, ok := logger.(*log.Logger); ok {
|
return r, true, nil
|
||||||
progressWriter = lgr.WriterLevel(log.DebugLevel)
|
|
||||||
} else {
|
|
||||||
log.Errorf("Unable to get writer from logger (type=%T)", logger)
|
|
||||||
progressWriter = os.Stdout
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cloneOptions := git.CloneOptions{
|
|
||||||
URL: input.URL,
|
|
||||||
Progress: progressWriter,
|
|
||||||
|
|
||||||
InsecureSkipTLS: input.InsecureSkipTLS, // For Gitea
|
|
||||||
}
|
|
||||||
if input.Token != "" {
|
|
||||||
cloneOptions.Auth = &http.BasicAuth{
|
|
||||||
Username: "token",
|
|
||||||
Password: input.Token,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err = git.PlainCloneContext(ctx, input.Dir, false, &cloneOptions)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Unable to clone %v %s: %v", input.URL, refName, err)
|
logger.Debugf("Removing cached clone at %s because origin cannot be read: %v", input.Dir, err)
|
||||||
return nil, err
|
} else if len(remote.Config().URLs) == 0 {
|
||||||
|
logger.Debugf("Removing cached clone at %s because origin has no URL", input.Dir)
|
||||||
|
} else {
|
||||||
|
logger.Debugf("Removing cached clone at %s because origin URL changed from %s to %s", input.Dir, remote.Config().URLs[0], input.URL)
|
||||||
}
|
}
|
||||||
|
if err := os.RemoveAll(input.Dir); err != nil {
|
||||||
if err = os.Chmod(input.Dir, 0o755); err != nil {
|
return nil, false, fmt.Errorf("remove cached clone %s: %w", input.Dir, err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, nil
|
var progressWriter io.Writer
|
||||||
|
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
||||||
|
if entry, ok := logger.(*log.Entry); ok {
|
||||||
|
progressWriter = entry.WriterLevel(log.DebugLevel)
|
||||||
|
} else if lgr, ok := logger.(*log.Logger); ok {
|
||||||
|
progressWriter = lgr.WriterLevel(log.DebugLevel)
|
||||||
|
} else {
|
||||||
|
log.Errorf("Unable to get writer from logger (type=%T)", logger)
|
||||||
|
progressWriter = os.Stdout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cloneOptions := git.CloneOptions{
|
||||||
|
URL: input.URL,
|
||||||
|
Progress: progressWriter,
|
||||||
|
|
||||||
|
InsecureSkipTLS: input.InsecureSkipTLS, // For Gitea
|
||||||
|
}
|
||||||
|
if input.Token != "" {
|
||||||
|
cloneOptions.Auth = &http.BasicAuth{
|
||||||
|
Username: "token",
|
||||||
|
Password: input.Token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err = cloneAtDepth(ctx, input, cloneOptions, logger)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Unable to clone %v %s: %v", input.URL, refName, err)
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.Chmod(input.Dir, 0o755); err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitOptions(token string) (fetchOptions git.FetchOptions, pullOptions git.PullOptions) {
|
func gitOptions(token string) (fetchOptions git.FetchOptions, pullOptions git.PullOptions) {
|
||||||
@@ -293,19 +346,20 @@ func gitOptions(token string) (fetchOptions git.FetchOptions, pullOptions git.Pu
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewGitCloneExecutor creates an executor to clone git repos
|
// NewGitCloneExecutor creates an executor to clone git repos
|
||||||
//
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
logger.Infof(" \u2601 git clone '%s' # ref=%s", input.URL, input.Ref)
|
if input.Quiet {
|
||||||
|
logger.Debugf("git clone '%s' # ref=%s", input.URL, input.Ref)
|
||||||
|
} else {
|
||||||
|
logger.Infof("git clone '%s' # ref=%s", input.URL, input.Ref)
|
||||||
|
}
|
||||||
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
||||||
|
|
||||||
cloneLock.Lock()
|
defer AcquireCloneLock(input.Dir)()
|
||||||
defer cloneLock.Unlock()
|
|
||||||
|
|
||||||
refName := plumbing.ReferenceName("refs/heads/" + input.Ref)
|
refName := plumbing.ReferenceName("refs/heads/" + input.Ref)
|
||||||
r, err := CloneIfRequired(ctx, refName, input, logger)
|
r, reused, err := CloneIfRequired(ctx, refName, input, logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -320,6 +374,16 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
|||||||
pullOptions.InsecureSkipTLS = true
|
pullOptions.InsecureSkipTLS = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Action clones only ever need the tip commit, so keep a shallow cache cheap on update at depth 1 regardless of its original depth
|
||||||
|
// Turning action_shallow_clone off does not convert an existing shallow cache; evict it for a full clone.
|
||||||
|
shallow := isShallow(r)
|
||||||
|
if shallow {
|
||||||
|
fetchOptions.Depth = 1
|
||||||
|
if spec, ok := shallowFetchRefSpec(r, input.Ref); ok {
|
||||||
|
fetchOptions.RefSpecs = []config.RefSpec{spec}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !isOfflineMode {
|
if !isOfflineMode {
|
||||||
err = r.Fetch(&fetchOptions)
|
err = r.Fetch(&fetchOptions)
|
||||||
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||||
@@ -330,10 +394,10 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
|||||||
var hash *plumbing.Hash
|
var hash *plumbing.Hash
|
||||||
rev := plumbing.Revision(input.Ref)
|
rev := plumbing.Revision(input.Ref)
|
||||||
if hash, err = r.ResolveRevision(rev); err != nil {
|
if hash, err = r.ResolveRevision(rev); err != nil {
|
||||||
|
// ResolveRevision returns a nil hash on error, and a branch ref legitimately fails
|
||||||
|
// here (no local refs/heads/<ref>); the duck-typing below resolves it.
|
||||||
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
||||||
}
|
} else if hash.String() != input.Ref && strings.HasPrefix(hash.String(), input.Ref) {
|
||||||
|
|
||||||
if hash.String() != input.Ref && strings.HasPrefix(hash.String(), input.Ref) {
|
|
||||||
return &Error{
|
return &Error{
|
||||||
err: ErrShortRef,
|
err: ErrShortRef,
|
||||||
commit: hash.String(),
|
commit: hash.String(),
|
||||||
@@ -384,12 +448,20 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isOfflineMode {
|
|
||||||
|
reusedMsg := ""
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case !isOfflineMode && !shallow:
|
||||||
|
// In shallow mode the depth-limited fetch above already advanced the ref.
|
||||||
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
|
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||||
logger.Debugf("Unable to pull %s: %v", refName, err)
|
logger.Debugf("Unable to pull %s: %v", refName, err)
|
||||||
}
|
}
|
||||||
|
case isOfflineMode && reused:
|
||||||
|
reusedMsg = " (reused in offline mode)"
|
||||||
}
|
}
|
||||||
logger.Debugf("Cloned %s to %s", input.URL, input.Dir)
|
|
||||||
|
logger.Debugf("Cloned %s to %s%s", input.URL, input.Dir, reusedMsg)
|
||||||
|
|
||||||
if hash.String() != input.Ref && refType == "branch" {
|
if hash.String() != input.Ref && refType == "branch" {
|
||||||
logger.Debugf("Provided ref is not a sha. Updating branch ref after pull")
|
logger.Debugf("Provided ref is not a sha. Updating branch ref after pull")
|
||||||
@@ -418,3 +490,53 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cloneAtDepth clones input.URL into input.Dir using opts.
|
||||||
|
// With input.Depth > 0 it first tries a shallow, single-branch clone of input.Ref, falling back when error.
|
||||||
|
func cloneAtDepth(ctx context.Context, input NewGitCloneExecutorInput, opts git.CloneOptions, logger log.FieldLogger) (*git.Repository, error) {
|
||||||
|
if input.Depth > 0 {
|
||||||
|
for _, refName := range []plumbing.ReferenceName{
|
||||||
|
plumbing.NewBranchReferenceName(input.Ref),
|
||||||
|
plumbing.NewTagReferenceName(input.Ref),
|
||||||
|
} {
|
||||||
|
shallowOpts := opts
|
||||||
|
shallowOpts.Depth = input.Depth
|
||||||
|
shallowOpts.SingleBranch = true
|
||||||
|
shallowOpts.ReferenceName = refName
|
||||||
|
shallowOpts.Tags = git.NoTags
|
||||||
|
|
||||||
|
r, err := git.PlainCloneContext(ctx, input.Dir, false, &shallowOpts)
|
||||||
|
if err == nil {
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
logger.Debugf("Shallow clone of %s as %s failed: %v", input.URL, refName, err)
|
||||||
|
if rmErr := os.RemoveAll(input.Dir); rmErr != nil {
|
||||||
|
return nil, fmt.Errorf("remove partial clone %s: %w", input.Dir, rmErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.Debugf("Falling back to a full clone of %s for ref %q", input.URL, input.Ref)
|
||||||
|
}
|
||||||
|
|
||||||
|
return git.PlainCloneContext(ctx, input.Dir, false, &opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isShallow reports whether the local repository was cloned with a limited depth.
|
||||||
|
func isShallow(r *git.Repository) bool {
|
||||||
|
shallows, err := r.Storer.Shallow()
|
||||||
|
return err == nil && len(shallows) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// shallowFetchRefSpec returns the single refspec that updates only input.Ref, keeping a shallow clone from re-downloading every branch's history.
|
||||||
|
// ok is false when the ref is not present locally as a tag or remote-tracking branch, in which case the broad default refspec is used.
|
||||||
|
func shallowFetchRefSpec(r *git.Repository, ref string) (config.RefSpec, bool) {
|
||||||
|
tagRef := plumbing.NewTagReferenceName(ref)
|
||||||
|
if _, err := r.Reference(tagRef, false); err == nil {
|
||||||
|
return config.RefSpec(fmt.Sprintf("+%s:%s", tagRef, tagRef)), true
|
||||||
|
}
|
||||||
|
remoteRef := plumbing.NewRemoteReferenceName("origin", ref)
|
||||||
|
if _, err := r.Reference(remoteRef, false); err == nil {
|
||||||
|
branchRef := plumbing.NewBranchReferenceName(ref)
|
||||||
|
return config.RefSpec(fmt.Sprintf("+%s:%s", branchRef, remoteRef)), true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,11 +10,16 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
logrustest "github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -48,8 +53,11 @@ func TestFindGitSlug(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDir(t *testing.T) string {
|
func TestErrorWrapsCommitAndCause(t *testing.T) {
|
||||||
return t.TempDir()
|
err := &Error{err: ErrShortRef, commit: "abc123"}
|
||||||
|
require.Equal(t, ErrShortRef.Error(), err.Error())
|
||||||
|
require.ErrorIs(t, err, ErrShortRef)
|
||||||
|
require.Equal(t, "abc123", err.Commit())
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanGitHooks(dir string) error {
|
func cleanGitHooks(dir string) error {
|
||||||
@@ -76,8 +84,7 @@ func cleanGitHooks(dir string) error {
|
|||||||
func TestFindGitRemoteURL(t *testing.T) {
|
func TestFindGitRemoteURL(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
basedir := testDir(t)
|
basedir := t.TempDir()
|
||||||
gitConfig()
|
|
||||||
err := gitCmd("init", basedir)
|
err := gitCmd("init", basedir)
|
||||||
assert.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
err = cleanGitHooks(basedir)
|
err = cleanGitHooks(basedir)
|
||||||
@@ -99,9 +106,24 @@ func TestFindGitRemoteURL(t *testing.T) {
|
|||||||
assert.Equal(remoteURL, u)
|
assert.Equal(remoteURL, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindGithubRepoUsesOriginAndCustomRemote(t *testing.T) {
|
||||||
|
basedir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("init", basedir))
|
||||||
|
require.NoError(t, cleanGitHooks(basedir))
|
||||||
|
require.NoError(t, gitCmd("-C", basedir, "remote", "add", "origin", "https://github.com/owner/repo.git"))
|
||||||
|
require.NoError(t, gitCmd("-C", basedir, "remote", "add", "ghe", "git@git.example.com:team/project.git"))
|
||||||
|
|
||||||
|
slug, err := FindGithubRepo(context.Background(), basedir, "github.com", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "owner/repo", slug)
|
||||||
|
|
||||||
|
slug, err = FindGithubRepo(context.Background(), basedir, "git.example.com", "ghe")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "team/project", slug)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGitFindRef(t *testing.T) {
|
func TestGitFindRef(t *testing.T) {
|
||||||
basedir := testDir(t)
|
basedir := t.TempDir()
|
||||||
gitConfig()
|
|
||||||
|
|
||||||
for name, tt := range map[string]struct {
|
for name, tt := range map[string]struct {
|
||||||
Prepare func(t *testing.T, dir string)
|
Prepare func(t *testing.T, dir string)
|
||||||
@@ -178,36 +200,55 @@ func TestGitFindRef(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutor(t *testing.T) {
|
func TestGitCloneExecutor(t *testing.T) {
|
||||||
|
// Build a local bare "remote" so this runs offline and fast. The cases below mirror
|
||||||
|
// the tag/branch/sha/short-sha ref paths the executor handles, formerly exercised by
|
||||||
|
// cloning actions/checkout and anchore/scan-action over the network.
|
||||||
|
remoteDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
|
|
||||||
|
workDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "tag", "v2"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v2"))
|
||||||
|
|
||||||
|
// A branch with a dash in the name (mirrors the historical scan-action@act-fails case).
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "act-fails"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "branch-commit"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "act-fails"))
|
||||||
|
|
||||||
|
out, err := exec.Command("git", "-C", workDir, "rev-parse", "main").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
fullSha := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
for name, tt := range map[string]struct {
|
for name, tt := range map[string]struct {
|
||||||
Err error
|
Err error
|
||||||
URL, Ref string
|
Ref string
|
||||||
}{
|
}{
|
||||||
"tag": {
|
"tag": {
|
||||||
Err: nil,
|
Err: nil,
|
||||||
URL: "https://github.com/actions/checkout",
|
|
||||||
Ref: "v2",
|
Ref: "v2",
|
||||||
},
|
},
|
||||||
"branch": {
|
"branch": {
|
||||||
Err: nil,
|
Err: nil,
|
||||||
URL: "https://github.com/anchore/scan-action",
|
|
||||||
Ref: "act-fails",
|
Ref: "act-fails",
|
||||||
},
|
},
|
||||||
"sha": {
|
"sha": {
|
||||||
Err: nil,
|
Err: nil,
|
||||||
URL: "https://github.com/actions/checkout",
|
Ref: fullSha,
|
||||||
Ref: "5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f", // v2
|
|
||||||
},
|
},
|
||||||
"short-sha": {
|
"short-sha": {
|
||||||
Err: &Error{ErrShortRef, "5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f"},
|
Err: &Error{ErrShortRef, fullSha},
|
||||||
URL: "https://github.com/actions/checkout",
|
Ref: fullSha[:7],
|
||||||
Ref: "5a4ac90", // v2
|
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: tt.URL,
|
URL: remoteDir,
|
||||||
Ref: tt.Ref,
|
Ref: tt.Ref,
|
||||||
Dir: testDir(t),
|
Dir: t.TempDir(),
|
||||||
})
|
})
|
||||||
|
|
||||||
err := clone(context.Background())
|
err := clone(context.Background())
|
||||||
@@ -221,13 +262,56 @@ func TestGitCloneExecutor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
|
||||||
|
createRemote := func(message string) string {
|
||||||
|
remoteDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
|
|
||||||
|
workDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", message))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
|
||||||
|
return remoteDir
|
||||||
|
}
|
||||||
|
|
||||||
|
oldRemoteDir := createRemote("old-action")
|
||||||
|
newRemoteDir := createRemote("new-action")
|
||||||
|
cacheDir := t.TempDir()
|
||||||
|
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: oldRemoteDir,
|
||||||
|
Ref: "main",
|
||||||
|
Dir: cacheDir,
|
||||||
|
})(t.Context()))
|
||||||
|
|
||||||
|
markerPath := filepath.Join(cacheDir, "stale-marker")
|
||||||
|
require.NoError(t, os.WriteFile(markerPath, []byte("stale"), 0o644))
|
||||||
|
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: newRemoteDir,
|
||||||
|
Ref: "main",
|
||||||
|
Dir: cacheDir,
|
||||||
|
})(t.Context()))
|
||||||
|
|
||||||
|
originURL, err := findGitRemoteURL(t.Context(), cacheDir, "origin")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, newRemoteDir, originURL)
|
||||||
|
|
||||||
|
out, err := exec.Command("git", "-C", cacheDir, "log", "--oneline", "-1", "--format=%s").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "new-action", strings.TrimSpace(string(out)))
|
||||||
|
|
||||||
|
_, err = os.Stat(markerPath)
|
||||||
|
require.True(t, os.IsNotExist(err), "stale cached directory should be removed before recloning")
|
||||||
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorNonFastForwardRef(t *testing.T) {
|
func TestGitCloneExecutorNonFastForwardRef(t *testing.T) {
|
||||||
// Simulate the scenario where a remote ref (e.g. a GitHub PR head ref) changes
|
// Simulate the scenario where a remote ref (e.g. a GitHub PR head ref) changes
|
||||||
// non-fast-forward between two fetches. Before the fix, the fetch used Force=false,
|
// non-fast-forward between two fetches. Before the fix, the fetch used Force=false,
|
||||||
// causing go-git to return ErrForceNeeded and short-circuit the checkout.
|
// causing go-git to return ErrForceNeeded and short-circuit the checkout.
|
||||||
|
|
||||||
gitConfig()
|
|
||||||
|
|
||||||
// Create a bare "remote" repo with an initial commit on main and a feature branch.
|
// Create a bare "remote" repo with an initial commit on main and a feature branch.
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
@@ -277,22 +361,195 @@ func TestGitCloneExecutorNonFastForwardRef(t *testing.T) {
|
|||||||
assert.Equal(t, "second", strings.TrimSpace(string(out)), "working tree should be at the latest commit")
|
assert.Equal(t, "second", strings.TrimSpace(string(out)), "working tree should be at the latest commit")
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitConfig() {
|
func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
||||||
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
// Build a local "remote" with a single commit on main.
|
||||||
var err error
|
remoteDir := t.TempDir()
|
||||||
if err = gitCmd("config", "--global", "user.email", "test@test.com"); err != nil {
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
log.Error(err)
|
workDir := t.TempDir()
|
||||||
}
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
if err = gitCmd("config", "--global", "user.name", "Unit Test"); err != nil {
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
log.Error(err)
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
||||||
}
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
|
||||||
|
// Prime the cache with an online clone of main.
|
||||||
|
cacheDir := t.TempDir()
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir,
|
||||||
|
Ref: "main",
|
||||||
|
Dir: cacheDir,
|
||||||
|
})(context.Background()))
|
||||||
|
|
||||||
|
t.Run("cached branch resolves without fetching", func(t *testing.T) {
|
||||||
|
// Offline reuse of a cached branch must succeed even though ResolveRevision(input.Ref)
|
||||||
|
// finds no local refs/heads/<ref>.
|
||||||
|
err := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir,
|
||||||
|
Ref: "main",
|
||||||
|
Dir: cacheDir,
|
||||||
|
OfflineMode: true,
|
||||||
|
})(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
out, err := exec.Command("git", "-C", cacheDir, "log", "--oneline", "-1", "--format=%s").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "initial", strings.TrimSpace(string(out)))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unresolvable cached ref returns error", func(t *testing.T) {
|
||||||
|
// The ref was never cached; offline mode cannot resolve it and must return an error.
|
||||||
|
err := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir,
|
||||||
|
Ref: "never-fetched",
|
||||||
|
Dir: cacheDir,
|
||||||
|
OfflineMode: true,
|
||||||
|
})(context.Background())
|
||||||
|
require.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
||||||
|
remoteDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
|
workDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
|
||||||
|
// Quiet callers report the download themselves, so the clone line must not reach the job log.
|
||||||
|
for name, quiet := range map[string]bool{"quiet": true, "not quiet": false} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
logger, hook := logrustest.NewNullLogger()
|
||||||
|
logger.SetLevel(log.InfoLevel)
|
||||||
|
ctx := common.WithLogger(context.Background(), logger.WithField("job", "j1"))
|
||||||
|
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir,
|
||||||
|
Ref: "main",
|
||||||
|
Dir: t.TempDir(),
|
||||||
|
Quiet: quiet,
|
||||||
|
})(ctx))
|
||||||
|
|
||||||
|
var cloneLines int
|
||||||
|
for _, entry := range hook.AllEntries() {
|
||||||
|
if strings.HasPrefix(entry.Message, "git clone ") {
|
||||||
|
cloneLines++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if quiet {
|
||||||
|
assert.Zero(t, cloneLines)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, 1, cloneLines)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitCloneExecutorShallow(t *testing.T) {
|
||||||
|
// Build a local "remote" with several commits on main plus a tag, so a full clone would pull noticeably more history than a shallow one.
|
||||||
|
remoteDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
|
workDir := t.TempDir()
|
||||||
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
|
for _, m := range []string{"c1", "c2", "c3"} {
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", m))
|
||||||
|
}
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "tag", "v1"))
|
||||||
|
sha := gitRevParse(t, workDir, "HEAD~1") // c2, a SHA that go-git cannot shallow-clone
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v1"))
|
||||||
|
|
||||||
|
shallowMarker := func(dir string) string { return filepath.Join(dir, ".git", "shallow") }
|
||||||
|
|
||||||
|
t.Run("branch is cloned shallowly", func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1,
|
||||||
|
})(t.Context()))
|
||||||
|
assert.FileExists(t, shallowMarker(dir), "clone should be shallow")
|
||||||
|
assert.Equal(t, 1, gitRevCount(t, dir), "only the tip commit should be present")
|
||||||
|
assert.Equal(t, "c3", gitHeadSubject(t, dir))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("tag is cloned shallowly", func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir, Ref: "v1", Dir: dir, Depth: 1,
|
||||||
|
})(t.Context()))
|
||||||
|
assert.FileExists(t, shallowMarker(dir), "clone should be shallow")
|
||||||
|
assert.Equal(t, 1, gitRevCount(t, dir))
|
||||||
|
assert.Equal(t, "c3", gitHeadSubject(t, dir))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("SHA falls back to a full clone", func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir, Ref: sha, Dir: dir, Depth: 1,
|
||||||
|
})(t.Context()))
|
||||||
|
// go-git cannot shallow-clone a raw SHA, so it falls back to a full clone; the absence of a shallow marker proves the fallback happened.
|
||||||
|
assert.NoFileExists(t, shallowMarker(dir), "a SHA ref must not produce a shallow clone")
|
||||||
|
assert.Equal(t, sha, gitRevParse(t, dir, "HEAD"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("moving branch updates while staying shallow", func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1,
|
||||||
|
})(t.Context()))
|
||||||
|
require.Equal(t, "c3", gitHeadSubject(t, dir))
|
||||||
|
|
||||||
|
// Advance main on the remote, then reuse the existing shallow clone.
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "c4"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main"))
|
||||||
|
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1,
|
||||||
|
})(t.Context()))
|
||||||
|
assert.Equal(t, "c4", gitHeadSubject(t, dir), "reused shallow clone should update to the new tip")
|
||||||
|
assert.FileExists(t, shallowMarker(dir), "repo should remain shallow after update")
|
||||||
|
assert.Equal(t, 1, gitRevCount(t, dir))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitRevParse(t *testing.T, dir, rev string) string {
|
||||||
|
t.Helper()
|
||||||
|
out, err := exec.Command("git", "-C", dir, "rev-parse", rev).Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
return strings.TrimSpace(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitRevCount(t *testing.T, dir string) int {
|
||||||
|
t.Helper()
|
||||||
|
out, err := exec.Command("git", "-C", dir, "rev-list", "--count", "HEAD").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
n, err := strconv.Atoi(strings.TrimSpace(string(out)))
|
||||||
|
require.NoError(t, err)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitHeadSubject(t *testing.T, dir string) string {
|
||||||
|
t.Helper()
|
||||||
|
out, err := exec.Command("git", "-C", dir, "log", "-1", "--format=%s").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
return strings.TrimSpace(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
func gitCmd(args ...string) error {
|
func gitCmd(args ...string) error {
|
||||||
cmd := exec.Command("git", args...)
|
cmd := exec.Command("git", args...)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
// Inject a deterministic identity and ignore the host's global/system config so commits
|
||||||
|
// succeed regardless of the host having no user.name/user.email (e.g. CI, GITHUB_ACTIONS
|
||||||
|
// unset) or a global commit.gpgsign, and without mutating the developer's ~/.gitconfig.
|
||||||
|
cmd.Env = append(os.Environ(),
|
||||||
|
"GIT_AUTHOR_NAME=Unit Test",
|
||||||
|
"GIT_AUTHOR_EMAIL=test@test.com",
|
||||||
|
"GIT_COMMITTER_NAME=Unit Test",
|
||||||
|
"GIT_COMMITTER_EMAIL=test@test.com",
|
||||||
|
"GIT_CONFIG_GLOBAL=/dev/null",
|
||||||
|
"GIT_CONFIG_SYSTEM=/dev/null",
|
||||||
|
)
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if exitError, ok := err.(*exec.ExitError); ok {
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
@@ -303,3 +560,53 @@ func gitCmd(args ...string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAcquireCloneLock(t *testing.T) {
|
||||||
|
t.Run("same directory serializes", func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
unlock1 := AcquireCloneLock(dir)
|
||||||
|
|
||||||
|
secondAcquired := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
unlock := AcquireCloneLock(dir)
|
||||||
|
close(secondAcquired)
|
||||||
|
unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-secondAcquired:
|
||||||
|
t.Fatal("second acquire should block while first holds the lock")
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
|
||||||
|
unlock1()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-secondAcquired:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("second acquire should proceed after first releases the lock")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("different directories do not block", func(t *testing.T) {
|
||||||
|
dirA := t.TempDir()
|
||||||
|
dirB := t.TempDir()
|
||||||
|
|
||||||
|
unlockA := AcquireCloneLock(dirA)
|
||||||
|
defer unlockA()
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
unlock := AcquireCloneLock(dirB)
|
||||||
|
unlock()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("acquire on a different directory must not block")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ func JobError(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetJobError(ctx context.Context, err error) {
|
func SetJobError(ctx context.Context, err error) {
|
||||||
ctx.Value(jobErrorContextKeyVal).(map[string]error)["error"] = err
|
if container, ok := ctx.Value(jobErrorContextKeyVal).(map[string]error); ok {
|
||||||
|
container["error"] = err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithJobErrorContainer adds a value to the context as a container for an error
|
// WithJobErrorContainer adds a value to the context as a container for an error
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ import (
|
|||||||
// LineHandler is a callback function for handling a line
|
// LineHandler is a callback function for handling a line
|
||||||
type LineHandler func(line string) bool
|
type LineHandler func(line string) bool
|
||||||
|
|
||||||
|
// Flusher is implemented by writers that buffer a trailing, not-yet-terminated
|
||||||
|
// line. Callers should flush once the underlying stream has reached EOF so the
|
||||||
|
// final line (when it is not newline-terminated) is not lost.
|
||||||
|
type Flusher interface {
|
||||||
|
Flush()
|
||||||
|
}
|
||||||
|
|
||||||
type lineWriter struct {
|
type lineWriter struct {
|
||||||
buffer bytes.Buffer
|
buffer bytes.Buffer
|
||||||
handlers []LineHandler
|
handlers []LineHandler
|
||||||
@@ -24,6 +31,14 @@ func NewLineWriter(handlers ...LineHandler) io.Writer {
|
|||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlushWriter flushes w if it implements Flusher. It is a no-op otherwise, so
|
||||||
|
// callers can flush an io.Writer without knowing its concrete type.
|
||||||
|
func FlushWriter(w io.Writer) {
|
||||||
|
if f, ok := w.(Flusher); ok {
|
||||||
|
f.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (lw *lineWriter) Write(p []byte) (n int, err error) {
|
func (lw *lineWriter) Write(p []byte) (n int, err error) {
|
||||||
pBuf := bytes.NewBuffer(p)
|
pBuf := bytes.NewBuffer(p)
|
||||||
written := 0
|
written := 0
|
||||||
@@ -44,6 +59,17 @@ func (lw *lineWriter) Write(p []byte) (n int, err error) {
|
|||||||
return written, nil
|
return written, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush emits any buffered, not-yet-newline-terminated content as a final line.
|
||||||
|
// It is safe to call multiple times; subsequent calls with an empty buffer are
|
||||||
|
// no-ops.
|
||||||
|
func (lw *lineWriter) Flush() {
|
||||||
|
if lw.buffer.Len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lw.handleLine(lw.buffer.String())
|
||||||
|
lw.buffer.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
func (lw *lineWriter) handleLine(line string) {
|
func (lw *lineWriter) handleLine(line string) {
|
||||||
for _, h := range lw.handlers {
|
for _, h := range lw.handlers {
|
||||||
ok := h(line)
|
ok := h(line)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -39,3 +40,33 @@ func TestLineWriter(t *testing.T) {
|
|||||||
assert.Equal(" and another\n", lines[2])
|
assert.Equal(" and another\n", lines[2])
|
||||||
assert.Equal("last line\n", lines[3])
|
assert.Equal("last line\n", lines[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLineWriterFlush(t *testing.T) {
|
||||||
|
lines := make([]string, 0)
|
||||||
|
lineHandler := func(s string) bool {
|
||||||
|
lines = append(lines, s)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
lineWriter := NewLineWriter(lineHandler)
|
||||||
|
|
||||||
|
assert := assert.New(t)
|
||||||
|
_, err := lineWriter.Write([]byte("complete line\npartial line without newline"))
|
||||||
|
assert.NoError(err) //nolint:testifylint // pre-existing pattern from nektos/act
|
||||||
|
|
||||||
|
// Only the newline-terminated line is emitted before flushing.
|
||||||
|
assert.Equal([]string{"complete line\n"}, lines)
|
||||||
|
|
||||||
|
// Flushing emits the buffered, not-yet-terminated trailing line.
|
||||||
|
FlushWriter(lineWriter)
|
||||||
|
assert.Equal([]string{"complete line\n", "partial line without newline"}, lines)
|
||||||
|
|
||||||
|
// Flushing again is a no-op: nothing is buffered.
|
||||||
|
FlushWriter(lineWriter)
|
||||||
|
assert.Len(lines, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFlushWriterIgnoresNonFlusher(t *testing.T) {
|
||||||
|
// FlushWriter must be a safe no-op for writers that do not buffer lines.
|
||||||
|
assert.NotPanics(t, func() { FlushWriter(io.Discard) })
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ func GetOutboundIP() net.IP {
|
|||||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
return conn.LocalAddr().(*net.UDPAddr).IP
|
if addr, ok := conn.LocalAddr().(*net.UDPAddr); ok {
|
||||||
|
return addr.IP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// So the machine cannot access the internet. Pick an IP address from network interfaces.
|
// So the machine cannot access the internet. Pick an IP address from network interfaces.
|
||||||
|
|||||||
@@ -6,13 +6,21 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ExitCodeError reports a non-zero process exit code from a container command.
|
||||||
|
type ExitCodeError int
|
||||||
|
|
||||||
|
func (e ExitCodeError) Error() string {
|
||||||
|
return fmt.Sprintf("Process completed with exit code %d.", int(e))
|
||||||
|
}
|
||||||
|
|
||||||
// NewContainerInput the input for the New function
|
// NewContainerInput the input for the New function
|
||||||
type NewContainerInput struct {
|
type NewContainerInput struct {
|
||||||
Image string
|
Image string
|
||||||
@@ -39,6 +47,7 @@ type NewContainerInput struct {
|
|||||||
// Gitea specific
|
// Gitea specific
|
||||||
AutoRemove bool
|
AutoRemove bool
|
||||||
ValidVolumes []string
|
ValidVolumes []string
|
||||||
|
AllocatePTY bool // allocate a pseudo-TTY for the container's exec processes
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileEntry is a file to copy to a container
|
// FileEntry is a file to copy to a container
|
||||||
@@ -73,6 +82,14 @@ type NewDockerBuildExecutorInput struct {
|
|||||||
BuildContext io.Reader
|
BuildContext io.Reader
|
||||||
ImageTag string
|
ImageTag string
|
||||||
Platform string
|
Platform string
|
||||||
|
BuildArgs map[string]*string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDockerNetworkCreateExecutorInput the input for the NewDockerNetworkCreateExecutor function
|
||||||
|
type NewDockerNetworkCreateExecutorInput struct {
|
||||||
|
EnableIPv4 *bool
|
||||||
|
EnableIPv6 *bool
|
||||||
|
RunnerUUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
||||||
|
|||||||
@@ -8,34 +8,33 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
|
"github.com/distribution/reference"
|
||||||
"github.com/docker/cli/cli/config"
|
"github.com/docker/cli/cli/config"
|
||||||
"github.com/docker/cli/cli/config/credentials"
|
"github.com/moby/moby/api/types/registry"
|
||||||
"github.com/docker/docker/api/types/registry"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadDockerAuthConfig(ctx context.Context, image string) (registry.AuthConfig, error) {
|
func LoadDockerAuthConfig(ctx context.Context, image string) (registry.AuthConfig, error) {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
config, err := config.Load(config.Dir())
|
// config.LoadDefaultConfigFile panics on nil io.Writer when the config
|
||||||
|
// file is malformed; use config.Load to route errors through the logger.
|
||||||
|
cfg, err := config.Load(config.Dir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("Could not load docker config: %v", err)
|
logger.Warnf("Could not load docker config: %v", err)
|
||||||
return registry.AuthConfig{}, err
|
return registry.AuthConfig{}, err
|
||||||
}
|
}
|
||||||
|
registryKey := registryAuthConfigKey("docker.io")
|
||||||
if !config.ContainsAuth() {
|
if image != "" {
|
||||||
config.CredentialsStore = credentials.DetectDefaultStore(config.CredentialsStore)
|
if registryRef, refErr := reference.ParseNormalizedNamed(image); refErr != nil {
|
||||||
|
logger.Warnf("Could not normalize image reference: %v", refErr)
|
||||||
|
} else {
|
||||||
|
registryKey = registryAuthConfigKey(reference.Domain(registryRef))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hostName := "index.docker.io"
|
authConfig, err := cfg.GetAuthConfig(registryKey)
|
||||||
index := strings.IndexRune(image, '/')
|
|
||||||
if index > -1 && (strings.ContainsAny(image[:index], ".:") || image[:index] == "localhost") {
|
|
||||||
hostName = image[:index]
|
|
||||||
}
|
|
||||||
|
|
||||||
authConfig, err := config.GetAuthConfig(hostName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("Could not get auth config from docker config: %v", err)
|
logger.Warnf("Could not get auth config from docker config: %v", err)
|
||||||
return registry.AuthConfig{}, err
|
return registry.AuthConfig{}, err
|
||||||
@@ -46,17 +45,16 @@ func LoadDockerAuthConfig(ctx context.Context, image string) (registry.AuthConfi
|
|||||||
|
|
||||||
func LoadDockerAuthConfigs(ctx context.Context) map[string]registry.AuthConfig {
|
func LoadDockerAuthConfigs(ctx context.Context) map[string]registry.AuthConfig {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
config, err := config.Load(config.Dir())
|
cfg, err := config.Load(config.Dir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("Could not load docker config: %v", err)
|
logger.Warnf("Could not load docker config: %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
creds, err := cfg.GetAllCredentials()
|
||||||
if !config.ContainsAuth() {
|
if err != nil {
|
||||||
config.CredentialsStore = credentials.DetectDefaultStore(config.CredentialsStore)
|
logger.Warnf("Could not get docker auth configs: %v", err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
creds, _ := config.GetAllCredentials()
|
|
||||||
authConfigs := make(map[string]registry.AuthConfig, len(creds))
|
authConfigs := make(map[string]registry.AuthConfig, len(creds))
|
||||||
for k, v := range creds {
|
for k, v := range creds {
|
||||||
authConfigs[k] = registry.AuthConfig(v)
|
authConfigs[k] = registry.AuthConfig(v)
|
||||||
@@ -64,3 +62,10 @@ func LoadDockerAuthConfigs(ctx context.Context) map[string]registry.AuthConfig {
|
|||||||
|
|
||||||
return authConfigs
|
return authConfigs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registryAuthConfigKey(domainName string) string {
|
||||||
|
if domainName == "docker.io" || domainName == "index.docker.io" {
|
||||||
|
return "https://index.docker.io/v1/"
|
||||||
|
}
|
||||||
|
return domainName
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,13 +12,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/moby/go-archive"
|
||||||
"github.com/docker/docker/pkg/archive"
|
"github.com/moby/go-archive/compression"
|
||||||
// github.com/docker/docker/builder/dockerignore is deprecated
|
"github.com/moby/moby/client"
|
||||||
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
|
|
||||||
"github.com/moby/patternmatcher"
|
"github.com/moby/patternmatcher"
|
||||||
|
"github.com/moby/patternmatcher/ignorefile"
|
||||||
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDockerBuildExecutor function to create a run executor for the container
|
// NewDockerBuildExecutor function to create a run executor for the container
|
||||||
@@ -26,9 +27,9 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
|||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
if input.Platform != "" {
|
if input.Platform != "" {
|
||||||
logger.Infof("%sdocker build -t %s --platform %s %s", logPrefix, input.ImageTag, input.Platform, input.ContextDir)
|
logger.Infof("docker build -t %s --platform %s %s", input.ImageTag, input.Platform, input.ContextDir)
|
||||||
} else {
|
} else {
|
||||||
logger.Infof("%sdocker build -t %s %s", logPrefix, input.ImageTag, input.ContextDir)
|
logger.Infof("docker build -t %s %s", input.ImageTag, input.ContextDir)
|
||||||
}
|
}
|
||||||
if common.Dryrun(ctx) {
|
if common.Dryrun(ctx) {
|
||||||
return nil
|
return nil
|
||||||
@@ -43,12 +44,19 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
|||||||
logger.Debugf("Building image from '%v'", input.ContextDir)
|
logger.Debugf("Building image from '%v'", input.ContextDir)
|
||||||
|
|
||||||
tags := []string{input.ImageTag}
|
tags := []string{input.ImageTag}
|
||||||
options := types.ImageBuildOptions{
|
options := client.ImageBuildOptions{
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Remove: true,
|
Remove: true,
|
||||||
Platform: input.Platform,
|
|
||||||
AuthConfigs: LoadDockerAuthConfigs(ctx),
|
AuthConfigs: LoadDockerAuthConfigs(ctx),
|
||||||
Dockerfile: input.Dockerfile,
|
Dockerfile: input.Dockerfile,
|
||||||
|
BuildArgs: input.BuildArgs,
|
||||||
|
}
|
||||||
|
platform, err := parsePlatform(input.Platform)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if platform != nil {
|
||||||
|
options.Platforms = []specs.Platform{*platform}
|
||||||
}
|
}
|
||||||
var buildContext io.ReadCloser
|
var buildContext io.ReadCloser
|
||||||
if input.BuildContext != nil {
|
if input.BuildContext != nil {
|
||||||
@@ -77,7 +85,7 @@ func createBuildContext(ctx context.Context, contextDir, relDockerfile string) (
|
|||||||
common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile)
|
common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile)
|
||||||
|
|
||||||
// And canonicalize dockerfile name to a platform-independent one
|
// And canonicalize dockerfile name to a platform-independent one
|
||||||
relDockerfile = archive.CanonicalTarNameForPath(relDockerfile)
|
relDockerfile = filepath.ToSlash(relDockerfile)
|
||||||
|
|
||||||
f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
|
f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
@@ -87,7 +95,7 @@ func createBuildContext(ctx context.Context, contextDir, relDockerfile string) (
|
|||||||
|
|
||||||
var excludes []string
|
var excludes []string
|
||||||
if err == nil {
|
if err == nil {
|
||||||
excludes, err = dockerignore.ReadAll(f) //nolint:staticcheck // pre-existing issue from nektos/act
|
excludes, err = ignorefile.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -107,9 +115,8 @@ func createBuildContext(ctx context.Context, contextDir, relDockerfile string) (
|
|||||||
includes = append(includes, ".dockerignore", relDockerfile)
|
includes = append(includes, ".dockerignore", relDockerfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
compression := archive.Uncompressed
|
|
||||||
buildCtx, err := archive.TarWithOptions(contextDir, &archive.TarOptions{
|
buildCtx, err := archive.TarWithOptions(contextDir, &archive.TarOptions{
|
||||||
Compression: compression,
|
Compression: compression.None,
|
||||||
ExcludePatterns: excludes,
|
ExcludePatterns: excludes,
|
||||||
IncludeFiles: includes,
|
IncludeFiles: includes,
|
||||||
})
|
})
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -16,15 +16,18 @@ package container
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
|
||||||
networktypes "github.com/docker/docker/api/types/network"
|
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
|
"github.com/moby/moby/api/types/container"
|
||||||
|
networktypes "github.com/moby/moby/api/types/network"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
@@ -77,21 +80,21 @@ func setupRunFlags() (*pflag.FlagSet, *containerOptions) {
|
|||||||
return flags, copts
|
return flags, copts
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustParse(t *testing.T, args string) (*container.Config, *container.HostConfig) {
|
func mustParse(t *testing.T, args string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
config, hostConfig, _, err := parseRun(append(strings.Split(args, " "), "ubuntu", "bash"))
|
config, hostConfig, networkingConfig, err := parseRun(append(strings.Split(args, " "), "ubuntu", "bash"))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
return config, hostConfig
|
return config, hostConfig, networkingConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseRunLinks(t *testing.T) {
|
func TestParseRunLinks(t *testing.T) {
|
||||||
if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
|
if _, hostConfig, _ := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
|
||||||
t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
|
t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
|
||||||
}
|
}
|
||||||
if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
|
if _, hostConfig, _ := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
|
||||||
t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
|
t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
|
||||||
}
|
}
|
||||||
if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
|
if _, hostConfig, _ := mustParse(t, ""); len(hostConfig.Links) != 0 {
|
||||||
t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
|
t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,7 +143,7 @@ func TestParseRunAttach(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.input, func(t *testing.T) {
|
t.Run(tc.input, func(t *testing.T) {
|
||||||
config, _ := mustParse(t, tc.input)
|
config, _, _ := mustParse(t, tc.input)
|
||||||
assert.Equal(t, config.AttachStdin, tc.expected.AttachStdin)
|
assert.Equal(t, config.AttachStdin, tc.expected.AttachStdin)
|
||||||
assert.Equal(t, config.AttachStdout, tc.expected.AttachStdout)
|
assert.Equal(t, config.AttachStdout, tc.expected.AttachStdout)
|
||||||
assert.Equal(t, config.AttachStderr, tc.expected.AttachStderr)
|
assert.Equal(t, config.AttachStderr, tc.expected.AttachStderr)
|
||||||
@@ -194,11 +197,10 @@ func TestParseRunWithInvalidArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
func TestParseWithVolumes(t *testing.T) { //nolint:gocyclo // verbatim copy from docker/cli tests
|
||||||
func TestParseWithVolumes(t *testing.T) {
|
|
||||||
// A single volume
|
// A single volume
|
||||||
arr, tryit := setupPlatformVolume([]string{`/tmp`}, []string{`c:\tmp`})
|
arr, tryit := setupPlatformVolume([]string{`/tmp`}, []string{`c:\tmp`})
|
||||||
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
|
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds != nil {
|
||||||
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
|
||||||
} else if _, exists := config.Volumes[arr[0]]; !exists {
|
} else if _, exists := config.Volumes[arr[0]]; !exists {
|
||||||
t.Fatalf("Error parsing volume flags, %q is missing from volumes. Received %v", tryit, config.Volumes)
|
t.Fatalf("Error parsing volume flags, %q is missing from volumes. Received %v", tryit, config.Volumes)
|
||||||
@@ -206,7 +208,7 @@ func TestParseWithVolumes(t *testing.T) {
|
|||||||
|
|
||||||
// Two volumes
|
// Two volumes
|
||||||
arr, tryit = setupPlatformVolume([]string{`/tmp`, `/var`}, []string{`c:\tmp`, `c:\var`})
|
arr, tryit = setupPlatformVolume([]string{`/tmp`, `/var`}, []string{`c:\tmp`, `c:\var`})
|
||||||
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
|
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds != nil {
|
||||||
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
|
||||||
} else if _, exists := config.Volumes[arr[0]]; !exists {
|
} else if _, exists := config.Volumes[arr[0]]; !exists {
|
||||||
t.Fatalf("Error parsing volume flags, %s is missing from volumes. Received %v", arr[0], config.Volumes)
|
t.Fatalf("Error parsing volume flags, %s is missing from volumes. Received %v", arr[0], config.Volumes)
|
||||||
@@ -216,13 +218,13 @@ func TestParseWithVolumes(t *testing.T) {
|
|||||||
|
|
||||||
// A single bind mount
|
// A single bind mount
|
||||||
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`})
|
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`})
|
||||||
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || hostConfig.Binds[0] != arr[0] {
|
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || hostConfig.Binds[0] != arr[0] {
|
||||||
t.Fatalf("Error parsing volume flags, %q should mount-bind the path before the colon into the path after the colon. Received %v %v", arr[0], hostConfig.Binds, config.Volumes)
|
t.Fatalf("Error parsing volume flags, %q should mount-bind the path before the colon into the path after the colon. Received %v %v", arr[0], hostConfig.Binds, config.Volumes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Two bind mounts.
|
// Two bind mounts.
|
||||||
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`}, []string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`})
|
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`}, []string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`})
|
||||||
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
||||||
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,26 +233,26 @@ func TestParseWithVolumes(t *testing.T) {
|
|||||||
arr, tryit = setupPlatformVolume(
|
arr, tryit = setupPlatformVolume(
|
||||||
[]string{`/hostTmp:/containerTmp:ro`, `/hostVar:/containerVar:rw`},
|
[]string{`/hostTmp:/containerTmp:ro`, `/hostVar:/containerVar:rw`},
|
||||||
[]string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`})
|
[]string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`})
|
||||||
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
||||||
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Similar to previous test but with alternate modes which are only supported by Linux
|
// Similar to previous test but with alternate modes which are only supported by Linux
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:ro,Z`, `/hostVar:/containerVar:rw,Z`}, []string{})
|
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:ro,Z`, `/hostVar:/containerVar:rw,Z`}, []string{})
|
||||||
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
||||||
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
||||||
}
|
}
|
||||||
|
|
||||||
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:Z`, `/hostVar:/containerVar:z`}, []string{})
|
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:Z`, `/hostVar:/containerVar:z`}, []string{})
|
||||||
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
|
||||||
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// One bind mount and one volume
|
// One bind mount and one volume
|
||||||
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/containerVar`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`, `c:\containerTmp`})
|
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/containerVar`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`, `c:\containerTmp`})
|
||||||
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] {
|
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] {
|
||||||
t.Fatalf("Error parsing volume flags, %s and %s should only one and only one bind mount %s. Received %s", arr[0], arr[1], arr[0], hostConfig.Binds)
|
t.Fatalf("Error parsing volume flags, %s and %s should only one and only one bind mount %s. Received %s", arr[0], arr[1], arr[0], hostConfig.Binds)
|
||||||
} else if _, exists := config.Volumes[arr[1]]; !exists {
|
} else if _, exists := config.Volumes[arr[1]]; !exists {
|
||||||
t.Fatalf("Error parsing volume flags %s and %s. %s is missing from volumes. Received %v", arr[0], arr[1], arr[1], config.Volumes)
|
t.Fatalf("Error parsing volume flags %s and %s. %s is missing from volumes. Received %v", arr[0], arr[1], arr[1], config.Volumes)
|
||||||
@@ -259,7 +261,7 @@ func TestParseWithVolumes(t *testing.T) {
|
|||||||
// Root to non-c: drive letter (Windows specific)
|
// Root to non-c: drive letter (Windows specific)
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
arr, tryit = setupPlatformVolume([]string{}, []string{os.Getenv("SystemDrive") + `\:d:`})
|
arr, tryit = setupPlatformVolume([]string{}, []string{os.Getenv("SystemDrive") + `\:d:`})
|
||||||
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] || len(config.Volumes) != 0 {
|
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] || len(config.Volumes) != 0 {
|
||||||
t.Fatalf("Error parsing %s. Should have a single bind mount and no volumes", arr[0])
|
t.Fatalf("Error parsing %s. Should have a single bind mount and no volumes", arr[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,6 +297,36 @@ func compareRandomizedStrings(a, b, c, d string) error {
|
|||||||
return errors.Errorf("strings don't match")
|
return errors.Errorf("strings don't match")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustNetworkPort(t *testing.T, value string) networktypes.Port {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
port, err := networktypes.ParsePort(value)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to parse network port %q: %v", value, err)
|
||||||
|
}
|
||||||
|
return port
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustAddr(t *testing.T, value string) netip.Addr {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
addr, err := netip.ParseAddr(value)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to parse address %q: %v", value, err)
|
||||||
|
}
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustAddrs(t *testing.T, values ...string) []netip.Addr {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
addrs := make([]netip.Addr, 0, len(values))
|
||||||
|
for _, value := range values {
|
||||||
|
addrs = append(addrs, mustAddr(t, value))
|
||||||
|
}
|
||||||
|
return addrs
|
||||||
|
}
|
||||||
|
|
||||||
// Simple parse with MacAddress validation
|
// Simple parse with MacAddress validation
|
||||||
func TestParseWithMacAddress(t *testing.T) {
|
func TestParseWithMacAddress(t *testing.T) {
|
||||||
invalidMacAddress := "--mac-address=invalidMacAddress"
|
invalidMacAddress := "--mac-address=invalidMacAddress"
|
||||||
@@ -302,9 +334,10 @@ func TestParseWithMacAddress(t *testing.T) {
|
|||||||
if _, _, _, err := parseRun([]string{invalidMacAddress, "img", "cmd"}); err != nil && err.Error() != "invalidMacAddress is not a valid mac address" {
|
if _, _, _, err := parseRun([]string{invalidMacAddress, "img", "cmd"}); err != nil && err.Error() != "invalidMacAddress is not a valid mac address" {
|
||||||
t.Fatalf("Expected an error with %v mac-address, got %v", invalidMacAddress, err)
|
t.Fatalf("Expected an error with %v mac-address, got %v", invalidMacAddress, err)
|
||||||
}
|
}
|
||||||
if config, _ := mustParse(t, validMacAddress); config.MacAddress != "92:d0:c6:0a:29:33" { //nolint:staticcheck // pre-existing issue from nektos/act
|
_, hostConfig, networkingConfig := mustParse(t, validMacAddress)
|
||||||
t.Fatalf("Expected the config to have '92:d0:c6:0a:29:33' as MacAddress, got '%v'", config.MacAddress) //nolint:staticcheck // pre-existing issue from nektos/act
|
endpoint := networkingConfig.EndpointsConfig[string(hostConfig.NetworkMode)]
|
||||||
}
|
assert.Check(t, endpoint != nil)
|
||||||
|
assert.Equal(t, "92:d0:c6:0a:29:33", endpoint.MacAddress.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunFlagsParseWithMemory(t *testing.T) {
|
func TestRunFlagsParseWithMemory(t *testing.T) {
|
||||||
@@ -313,7 +346,7 @@ func TestRunFlagsParseWithMemory(t *testing.T) {
|
|||||||
err := flags.Parse(args)
|
err := flags.Parse(args)
|
||||||
assert.ErrorContains(t, err, `invalid argument "invalid" for "-m, --memory" flag`)
|
assert.ErrorContains(t, err, `invalid argument "invalid" for "-m, --memory" flag`)
|
||||||
|
|
||||||
_, hostconfig := mustParse(t, "--memory=1G")
|
_, hostconfig, _ := mustParse(t, "--memory=1G")
|
||||||
assert.Check(t, is.Equal(int64(1073741824), hostconfig.Memory))
|
assert.Check(t, is.Equal(int64(1073741824), hostconfig.Memory))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,10 +356,10 @@ func TestParseWithMemorySwap(t *testing.T) {
|
|||||||
err := flags.Parse(args)
|
err := flags.Parse(args)
|
||||||
assert.ErrorContains(t, err, `invalid argument "invalid" for "--memory-swap" flag`)
|
assert.ErrorContains(t, err, `invalid argument "invalid" for "--memory-swap" flag`)
|
||||||
|
|
||||||
_, hostconfig := mustParse(t, "--memory-swap=1G")
|
_, hostconfig, _ := mustParse(t, "--memory-swap=1G")
|
||||||
assert.Check(t, is.Equal(int64(1073741824), hostconfig.MemorySwap))
|
assert.Check(t, is.Equal(int64(1073741824), hostconfig.MemorySwap))
|
||||||
|
|
||||||
_, hostconfig = mustParse(t, "--memory-swap=-1")
|
_, hostconfig, _ = mustParse(t, "--memory-swap=-1")
|
||||||
assert.Check(t, is.Equal(int64(-1), hostconfig.MemorySwap))
|
assert.Check(t, is.Equal(int64(-1), hostconfig.MemorySwap))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,14 +374,14 @@ func TestParseHostname(t *testing.T) {
|
|||||||
hostnameWithDomain := "--hostname=hostname.domainname"
|
hostnameWithDomain := "--hostname=hostname.domainname"
|
||||||
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
|
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
|
||||||
for hostname, expectedHostname := range validHostnames {
|
for hostname, expectedHostname := range validHostnames {
|
||||||
if config, _ := mustParse(t, "--hostname="+hostname); config.Hostname != expectedHostname {
|
if config, _, _ := mustParse(t, "--hostname="+hostname); config.Hostname != expectedHostname {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as %q, got %q", expectedHostname, config.Hostname)
|
t.Fatalf("Expected the config to have 'hostname' as %q, got %q", expectedHostname, config.Hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname.domainname" || config.Domainname != "" {
|
if config, _, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname.domainname" || config.Domainname != "" {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname, got %q", config.Hostname)
|
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname, got %q", config.Hostname)
|
||||||
}
|
}
|
||||||
if config, _ := mustParse(t, hostnameWithDomainTld); config.Hostname != "hostname.domainname.tld" || config.Domainname != "" {
|
if config, _, _ := mustParse(t, hostnameWithDomainTld); config.Hostname != "hostname.domainname.tld" || config.Domainname != "" {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname.tld, got %q", config.Hostname)
|
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname.tld, got %q", config.Hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -362,26 +395,28 @@ func TestParseHostnameDomainname(t *testing.T) {
|
|||||||
"domainname-63-bytes-long-should-be-valid-and-without-any-errors": "domainname-63-bytes-long-should-be-valid-and-without-any-errors",
|
"domainname-63-bytes-long-should-be-valid-and-without-any-errors": "domainname-63-bytes-long-should-be-valid-and-without-any-errors",
|
||||||
}
|
}
|
||||||
for domainname, expectedDomainname := range validDomainnames {
|
for domainname, expectedDomainname := range validDomainnames {
|
||||||
if config, _ := mustParse(t, "--domainname="+domainname); config.Domainname != expectedDomainname {
|
if config, _, _ := mustParse(t, "--domainname="+domainname); config.Domainname != expectedDomainname {
|
||||||
t.Fatalf("Expected the config to have 'domainname' as %q, got %q", expectedDomainname, config.Domainname)
|
t.Fatalf("Expected the config to have 'domainname' as %q, got %q", expectedDomainname, config.Domainname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config, _ := mustParse(t, "--hostname=some.prefix --domainname=domainname"); config.Hostname != "some.prefix" || config.Domainname != "domainname" {
|
if config, _, _ := mustParse(t, "--hostname=some.prefix --domainname=domainname"); config.Hostname != "some.prefix" || config.Domainname != "domainname" {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as 'some.prefix' and 'domainname' as 'domainname', got %q and %q", config.Hostname, config.Domainname)
|
t.Fatalf("Expected the config to have 'hostname' as 'some.prefix' and 'domainname' as 'domainname', got %q and %q", config.Hostname, config.Domainname)
|
||||||
}
|
}
|
||||||
if config, _ := mustParse(t, "--hostname=another-prefix --domainname=domainname.tld"); config.Hostname != "another-prefix" || config.Domainname != "domainname.tld" {
|
if config, _, _ := mustParse(t, "--hostname=another-prefix --domainname=domainname.tld"); config.Hostname != "another-prefix" || config.Domainname != "domainname.tld" {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as 'another-prefix' and 'domainname' as 'domainname.tld', got %q and %q", config.Hostname, config.Domainname)
|
t.Fatalf("Expected the config to have 'hostname' as 'another-prefix' and 'domainname' as 'domainname.tld', got %q and %q", config.Hostname, config.Domainname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseWithExpose(t *testing.T) {
|
func TestParseWithExpose(t *testing.T) {
|
||||||
invalids := map[string]string{
|
invalids := []string{
|
||||||
":": "invalid port format for --expose: :",
|
":",
|
||||||
"8080:9090": "invalid port format for --expose: 8080:9090",
|
"8080:9090",
|
||||||
"NaN/tcp": `invalid range format for --expose: NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
|
"/tcp",
|
||||||
"NaN-NaN/tcp": `invalid range format for --expose: NaN-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
|
"/udp",
|
||||||
"8080-NaN/tcp": `invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
|
"NaN/tcp",
|
||||||
"1234567890-8080/tcp": `invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing "1234567890": value out of range`,
|
"NaN-NaN/tcp",
|
||||||
|
"8080-NaN/tcp",
|
||||||
|
"1234567890-8080/tcp",
|
||||||
}
|
}
|
||||||
valids := map[string][]nat.Port{
|
valids := map[string][]nat.Port{
|
||||||
"8080/tcp": {"8080/tcp"},
|
"8080/tcp": {"8080/tcp"},
|
||||||
@@ -390,9 +425,9 @@ func TestParseWithExpose(t *testing.T) {
|
|||||||
"8080-8080/udp": {"8080/udp"},
|
"8080-8080/udp": {"8080/udp"},
|
||||||
"8080-8082/tcp": {"8080/tcp", "8081/tcp", "8082/tcp"},
|
"8080-8082/tcp": {"8080/tcp", "8081/tcp", "8082/tcp"},
|
||||||
}
|
}
|
||||||
for expose, expectedError := range invalids {
|
for _, expose := range invalids {
|
||||||
if _, _, _, err := parseRun([]string{fmt.Sprintf("--expose=%v", expose), "img", "cmd"}); err == nil || err.Error() != expectedError {
|
if _, _, _, err := parseRun([]string{fmt.Sprintf("--expose=%v", expose), "img", "cmd"}); err == nil {
|
||||||
t.Fatalf("Expected error '%v' with '--expose=%v', got '%v'", expectedError, expose, err)
|
t.Fatalf("Expected error with '--expose=%v', got none", expose)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for expose, exposedPorts := range valids {
|
for expose, exposedPorts := range valids {
|
||||||
@@ -404,7 +439,7 @@ func TestParseWithExpose(t *testing.T) {
|
|||||||
t.Fatalf("Expected %v exposed port, got %v", len(exposedPorts), len(config.ExposedPorts))
|
t.Fatalf("Expected %v exposed port, got %v", len(exposedPorts), len(config.ExposedPorts))
|
||||||
}
|
}
|
||||||
for _, port := range exposedPorts {
|
for _, port := range exposedPorts {
|
||||||
if _, ok := config.ExposedPorts[port]; !ok {
|
if _, ok := config.ExposedPorts[mustNetworkPort(t, string(port))]; !ok {
|
||||||
t.Fatalf("Expected %v, got %v", exposedPorts, config.ExposedPorts)
|
t.Fatalf("Expected %v, got %v", exposedPorts, config.ExposedPorts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -419,7 +454,7 @@ func TestParseWithExpose(t *testing.T) {
|
|||||||
}
|
}
|
||||||
ports := []nat.Port{"80/tcp", "81/tcp"}
|
ports := []nat.Port{"80/tcp", "81/tcp"}
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
if _, ok := config.ExposedPorts[port]; !ok {
|
if _, ok := config.ExposedPorts[mustNetworkPort(t, string(port))]; !ok {
|
||||||
t.Fatalf("Expected %v, got %v", ports, config.ExposedPorts)
|
t.Fatalf("Expected %v, got %v", ports, config.ExposedPorts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -463,6 +498,79 @@ func TestParseDevice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseDeviceByServerOS(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
device string
|
||||||
|
serverOS string
|
||||||
|
want container.DeviceMapping
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "linux source only",
|
||||||
|
device: "/dev/snd",
|
||||||
|
serverOS: "linux",
|
||||||
|
want: container.DeviceMapping{
|
||||||
|
PathOnHost: "/dev/snd",
|
||||||
|
PathInContainer: "/dev/snd",
|
||||||
|
CgroupPermissions: "rwm",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "linux source and mode",
|
||||||
|
device: "/dev/snd:rw",
|
||||||
|
serverOS: "linux",
|
||||||
|
want: container.DeviceMapping{
|
||||||
|
PathOnHost: "/dev/snd",
|
||||||
|
PathInContainer: "/dev/snd",
|
||||||
|
CgroupPermissions: "rw",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "linux source target and mode",
|
||||||
|
device: "/dev/snd:/container/snd:m",
|
||||||
|
serverOS: "linux",
|
||||||
|
want: container.DeviceMapping{
|
||||||
|
PathOnHost: "/dev/snd",
|
||||||
|
PathInContainer: "/container/snd",
|
||||||
|
CgroupPermissions: "m",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "windows passes value through",
|
||||||
|
device: `class/GUID`,
|
||||||
|
serverOS: "windows",
|
||||||
|
want: container.DeviceMapping{
|
||||||
|
PathOnHost: `class/GUID`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid server OS",
|
||||||
|
device: "/dev/snd",
|
||||||
|
serverOS: "plan9",
|
||||||
|
wantErr: "unknown server OS: plan9",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "too many linux fields",
|
||||||
|
device: "/dev/snd:/container/snd:rw:extra",
|
||||||
|
serverOS: "linux",
|
||||||
|
wantErr: "invalid device specification: /dev/snd:/container/snd:rw:extra",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got, err := parseDevice(tc.device, tc.serverOS)
|
||||||
|
if tc.wantErr != "" {
|
||||||
|
assert.Error(t, err, tc.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, got, tc.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseNetworkConfig(t *testing.T) {
|
func TestParseNetworkConfig(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -499,9 +607,9 @@ func TestParseNetworkConfig(t *testing.T) {
|
|||||||
expected: map[string]*networktypes.EndpointSettings{
|
expected: map[string]*networktypes.EndpointSettings{
|
||||||
"net1": {
|
"net1": {
|
||||||
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
||||||
IPv4Address: "172.20.88.22",
|
IPv4Address: mustAddr(t, "172.20.88.22"),
|
||||||
IPv6Address: "2001:db8::8822",
|
IPv6Address: mustAddr(t, "2001:db8::8822"),
|
||||||
LinkLocalIPs: []string{"169.254.2.2", "fe80::169:254:2:2"},
|
LinkLocalIPs: mustAddrs(t, "169.254.2.2", "fe80::169:254:2:2"),
|
||||||
},
|
},
|
||||||
Links: []string{"foo:bar", "bar:baz"},
|
Links: []string{"foo:bar", "bar:baz"},
|
||||||
Aliases: []string{"web1", "web2"},
|
Aliases: []string{"web1", "web2"},
|
||||||
@@ -528,9 +636,9 @@ func TestParseNetworkConfig(t *testing.T) {
|
|||||||
"net1": {
|
"net1": {
|
||||||
DriverOpts: map[string]string{"field1": "value1"},
|
DriverOpts: map[string]string{"field1": "value1"},
|
||||||
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
||||||
IPv4Address: "172.20.88.22",
|
IPv4Address: mustAddr(t, "172.20.88.22"),
|
||||||
IPv6Address: "2001:db8::8822",
|
IPv6Address: mustAddr(t, "2001:db8::8822"),
|
||||||
LinkLocalIPs: []string{"169.254.2.2", "fe80::169:254:2:2"},
|
LinkLocalIPs: mustAddrs(t, "169.254.2.2", "fe80::169:254:2:2"),
|
||||||
},
|
},
|
||||||
Links: []string{"foo:bar", "bar:baz"},
|
Links: []string{"foo:bar", "bar:baz"},
|
||||||
Aliases: []string{"web1", "web2"},
|
Aliases: []string{"web1", "web2"},
|
||||||
@@ -539,8 +647,8 @@ func TestParseNetworkConfig(t *testing.T) {
|
|||||||
"net3": {
|
"net3": {
|
||||||
DriverOpts: map[string]string{"field3": "value3"},
|
DriverOpts: map[string]string{"field3": "value3"},
|
||||||
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
||||||
IPv4Address: "172.20.88.22",
|
IPv4Address: mustAddr(t, "172.20.88.22"),
|
||||||
IPv6Address: "2001:db8::8822",
|
IPv6Address: mustAddr(t, "2001:db8::8822"),
|
||||||
},
|
},
|
||||||
Aliases: []string{"web3"},
|
Aliases: []string{"web3"},
|
||||||
},
|
},
|
||||||
@@ -557,8 +665,8 @@ func TestParseNetworkConfig(t *testing.T) {
|
|||||||
"field2": "value2",
|
"field2": "value2",
|
||||||
},
|
},
|
||||||
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
IPAMConfig: &networktypes.EndpointIPAMConfig{
|
||||||
IPv4Address: "172.20.88.22",
|
IPv4Address: mustAddr(t, "172.20.88.22"),
|
||||||
IPv6Address: "2001:db8::8822",
|
IPv6Address: mustAddr(t, "2001:db8::8822"),
|
||||||
},
|
},
|
||||||
Aliases: []string{"web1", "web2"},
|
Aliases: []string{"web1", "web2"},
|
||||||
},
|
},
|
||||||
@@ -611,7 +719,9 @@ func TestParseNetworkConfig(t *testing.T) {
|
|||||||
|
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.DeepEqual(t, hConfig.NetworkMode, tc.expectedCfg.NetworkMode)
|
assert.DeepEqual(t, hConfig.NetworkMode, tc.expectedCfg.NetworkMode)
|
||||||
assert.DeepEqual(t, nwConfig.EndpointsConfig, tc.expected)
|
if diff := cmp.Diff(tc.expected, nwConfig.EndpointsConfig, cmpopts.EquateComparable(netip.Addr{})); diff != "" {
|
||||||
|
t.Fatalf("unexpected endpoints (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -632,7 +742,7 @@ func TestParseModes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// uts ko
|
// uts ko
|
||||||
_, _, _, err = parseRun([]string{"--uts=container:", "img", "cmd"}) //nolint:dogsled // ignoring multiple returns in test helpers
|
_, _, _, err = parseRun([]string{"--uts=container:", "img", "cmd"}) //nolint:dogsled // verbatim copy from docker/cli tests
|
||||||
assert.ErrorContains(t, err, "--uts: invalid UTS mode")
|
assert.ErrorContains(t, err, "--uts: invalid UTS mode")
|
||||||
|
|
||||||
// uts ok
|
// uts ok
|
||||||
@@ -692,10 +802,9 @@ func TestParseRestartPolicy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseRestartPolicyAutoRemove(t *testing.T) {
|
func TestParseRestartPolicyAutoRemove(t *testing.T) {
|
||||||
expected := "Conflicting options: --restart and --rm"
|
_, _, _, err := parseRun([]string{"--rm", "--restart=always", "img", "cmd"}) //nolint:dogsled // verbatim copy from docker/cli tests
|
||||||
_, _, _, err := parseRun([]string{"--rm", "--restart=always", "img", "cmd"}) //nolint:dogsled // ignoring multiple returns in test helpers
|
if err == nil {
|
||||||
if err == nil || err.Error() != expected {
|
t.Fatal("Expected error for conflicting --restart and --rm, but got none")
|
||||||
t.Fatalf("Expected error %v, but got none", expected)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -753,7 +862,7 @@ func TestParseLoggingOpts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseEnvfileVariables(t *testing.T) { //nolint:dupl // pre-existing issue from nektos/act
|
func TestParseEnvfileVariables(t *testing.T) { //nolint:dupl // verbatim copy from docker/cli tests
|
||||||
e := "open nonexistent: no such file or directory"
|
e := "open nonexistent: no such file or directory"
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
e = "open nonexistent: The system cannot find the file specified."
|
e = "open nonexistent: The system cannot find the file specified."
|
||||||
@@ -796,7 +905,7 @@ func TestParseEnvfileVariablesWithBOMUnicode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UTF16 with BOM
|
// UTF16 with BOM
|
||||||
e := "contains invalid utf8 bytes at line"
|
e := "invalid env file"
|
||||||
if _, _, _, err := parseRun([]string{"--env-file=testdata/utf16.env", "img", "cmd"}); err == nil || !strings.Contains(err.Error(), e) {
|
if _, _, _, err := parseRun([]string{"--env-file=testdata/utf16.env", "img", "cmd"}); err == nil || !strings.Contains(err.Error(), e) {
|
||||||
t.Fatalf("Expected an error with message '%s', got %v", e, err)
|
t.Fatalf("Expected an error with message '%s', got %v", e, err)
|
||||||
}
|
}
|
||||||
@@ -806,7 +915,7 @@ func TestParseEnvfileVariablesWithBOMUnicode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseLabelfileVariables(t *testing.T) { //nolint:dupl // pre-existing issue from nektos/act
|
func TestParseLabelfileVariables(t *testing.T) { //nolint:dupl // verbatim copy from docker/cli tests
|
||||||
e := "open nonexistent: no such file or directory"
|
e := "open nonexistent: no such file or directory"
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
e = "open nonexistent: The system cannot find the file specified."
|
e = "open nonexistent: The system cannot find the file specified."
|
||||||
@@ -894,6 +1003,82 @@ func TestValidateDevice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateDeviceByServerOS(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
serverOS string
|
||||||
|
want string
|
||||||
|
wantError string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "linux preserves three-field container path",
|
||||||
|
value: "/host:/container/../device:rw",
|
||||||
|
serverOS: "linux",
|
||||||
|
want: "/host:/container/../device:rw",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "linux source path can be relative when target is absolute",
|
||||||
|
value: "relative-host:/container/device",
|
||||||
|
serverOS: "linux",
|
||||||
|
want: "relative-host:/container/device",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "windows defers validation",
|
||||||
|
value: `class/GUID`,
|
||||||
|
serverOS: "windows",
|
||||||
|
want: `class/GUID`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "linux rejects bad mode",
|
||||||
|
value: "/host:/container:ro",
|
||||||
|
serverOS: "linux",
|
||||||
|
wantError: "bad mode specified: ro",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "linux target must be absolute",
|
||||||
|
value: "/host:relative",
|
||||||
|
serverOS: "linux",
|
||||||
|
wantError: "relative is not an absolute path",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unknown server OS",
|
||||||
|
value: "/dev/snd",
|
||||||
|
serverOS: "plan9",
|
||||||
|
wantError: "unknown server OS: plan9",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got, err := validateDevice(tc.value, tc.serverOS)
|
||||||
|
if tc.wantError != "" {
|
||||||
|
assert.Error(t, err, tc.wantError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, got, tc.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeviceCgroupRulesAndInvalidParameter(t *testing.T) {
|
||||||
|
got, err := validateDeviceCgroupRule("c 1:3 rwm")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, got, "c 1:3 rwm")
|
||||||
|
|
||||||
|
_, err = validateDeviceCgroupRule("invalid")
|
||||||
|
assert.Error(t, err, "invalid device cgroup format 'invalid'")
|
||||||
|
|
||||||
|
if invalidParameter(nil) != nil {
|
||||||
|
t.Fatal("invalidParameter(nil) should be nil")
|
||||||
|
}
|
||||||
|
err = invalidParameter(errors.New("bad input"))
|
||||||
|
assert.Assert(t, err != nil)
|
||||||
|
var invalid interface{ InvalidParameter() }
|
||||||
|
assert.Assert(t, errors.As(err, &invalid))
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseSystemPaths(t *testing.T) {
|
func TestParseSystemPaths(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
doc string
|
doc string
|
||||||
|
|||||||
86
act/container/docker_create_flags.go
Normal file
86
act/container/docker_create_flags.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"github.com/kballard/go-shellquote"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
pullPolicyAlways = "always"
|
||||||
|
pullPolicyMissing = "missing"
|
||||||
|
pullPolicyNever = "never"
|
||||||
|
)
|
||||||
|
|
||||||
|
var pullPolicies = []string{pullPolicyAlways, pullPolicyMissing, pullPolicyNever}
|
||||||
|
|
||||||
|
// createFlags are the flags docker/cli registers on the `create` and `run` commands
|
||||||
|
// instead of in addFlags, so they are not part of containerOptions.
|
||||||
|
type createFlags struct {
|
||||||
|
platform string
|
||||||
|
pull string
|
||||||
|
name string
|
||||||
|
useAPISocket bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerCreateFlags(flags *pflag.FlagSet) *createFlags {
|
||||||
|
cf := new(createFlags)
|
||||||
|
flags.StringVar(&cf.platform, "platform", "", "Set platform if server is multi-platform capable")
|
||||||
|
flags.StringVar(&cf.pull, "pull", pullPolicyMissing, `Pull image before creating ("always", "missing", "never")`)
|
||||||
|
flags.StringVar(&cf.name, "name", "", "Assign a name to the container")
|
||||||
|
flags.BoolVar(&cf.useAPISocket, "use-api-socket", false, "Bind mount Docker API socket and required auth")
|
||||||
|
// Accepted without effect: pull progress is only logged at debug level, and docker
|
||||||
|
// no longer implements content trust.
|
||||||
|
flags.BoolP("quiet", "q", false, "Suppress the pull output")
|
||||||
|
flags.Bool("disable-content-trust", true, "Skip image verification (deprecated)")
|
||||||
|
return cf
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseContainerOptions parses a container options string. The flags are returned even
|
||||||
|
// on error, holding whatever was read before the failure.
|
||||||
|
func parseContainerOptions(options string) (*pflag.FlagSet, *containerOptions, *createFlags, error) {
|
||||||
|
flags := pflag.NewFlagSet("container_flags", pflag.ContinueOnError)
|
||||||
|
flags.SetOutput(io.Discard)
|
||||||
|
copts := addFlags(flags)
|
||||||
|
cf := registerCreateFlags(flags)
|
||||||
|
|
||||||
|
args, err := shellquote.Split(options)
|
||||||
|
if err != nil {
|
||||||
|
return flags, copts, cf, fmt.Errorf("Cannot split container options: '%s': '%w'", options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := flags.Parse(args); err != nil {
|
||||||
|
return flags, copts, cf, fmt.Errorf("Cannot parse container options: '%s': '%w'", options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return flags, copts, cf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// createFlagsFromOptions reads the create-level flags that have to be known before the
|
||||||
|
// container is created. Malformed options keep the defaults here and are reported by
|
||||||
|
// mergeContainerConfigs at create time.
|
||||||
|
func createFlagsFromOptions(options string) *createFlags {
|
||||||
|
_, _, cf, _ := parseContainerOptions(options)
|
||||||
|
return cf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cf *createFlags) validate() error {
|
||||||
|
if !slices.Contains(pullPolicies, cf.pull) {
|
||||||
|
return fmt.Errorf("invalid --pull option %q: must be one of %q", cf.pull, pullPolicies)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cf.useAPISocket {
|
||||||
|
return errors.New("--use-api-socket is not supported, use the runner's container.docker_host setting to expose a docker socket")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
62
act/container/docker_create_flags_test.go
Normal file
62
act/container/docker_create_flags_test.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateFlagsFromOptions(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
options string
|
||||||
|
platform string
|
||||||
|
pull string
|
||||||
|
}{
|
||||||
|
{"", "", pullPolicyMissing},
|
||||||
|
{"-v /a:/b --platform=linux/arm64 --pull always", "linux/arm64", pullPolicyAlways},
|
||||||
|
{"--platform linux/arm/v7 --pull never", "linux/arm/v7", pullPolicyNever},
|
||||||
|
{`--platform "linux/amd64`, "", pullPolicyMissing}, // malformed, defaults kept
|
||||||
|
} {
|
||||||
|
t.Run(tc.options, func(t *testing.T) {
|
||||||
|
cf := createFlagsFromOptions(tc.options)
|
||||||
|
assert.Equal(t, tc.platform, cf.platform)
|
||||||
|
assert.Equal(t, tc.pull, cf.pull)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFlagsValidate(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
options string
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{"--quiet --disable-content-trust --name mine", ""},
|
||||||
|
{"--pull sometimes", `invalid --pull option "sometimes"`},
|
||||||
|
{"--use-api-socket", "--use-api-socket is not supported"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.options, func(t *testing.T) {
|
||||||
|
err := createFlagsFromOptions(tc.options).validate()
|
||||||
|
if tc.wantErr == "" {
|
||||||
|
require.NoError(t, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
require.ErrorContains(t, err, tc.wantErr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewContainerAppliesCreateFlags(t *testing.T) {
|
||||||
|
input := &NewContainerInput{Platform: "linux/amd64", Options: "--platform linux/arm64 --pull never"}
|
||||||
|
cr, ok := NewContainer(input).(*containerReference)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, "linux/arm64", input.Platform)
|
||||||
|
assert.Equal(t, pullPolicyNever, cr.pullPolicy)
|
||||||
|
|
||||||
|
kept := &NewContainerInput{Platform: "linux/amd64", Options: "--privileged"}
|
||||||
|
NewContainer(kept)
|
||||||
|
assert.Equal(t, "linux/amd64", kept.Platform)
|
||||||
|
}
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/docker/docker/client"
|
"github.com/moby/moby/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageExistsLocally returns a boolean indicating if an image with the
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
||||||
@@ -23,8 +23,8 @@ func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool,
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
||||||
if client.IsErrNotFound(err) {
|
if cerrdefs.IsNotFound(err) {
|
||||||
return false, nil
|
return false, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@@ -46,14 +46,14 @@ func RemoveImage(ctx context.Context, imageName string, force, pruneChildren boo
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
||||||
if client.IsErrNotFound(err) {
|
if cerrdefs.IsNotFound(err) {
|
||||||
return false, nil
|
return false, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = cli.ImageRemove(ctx, inspectImage.ID, types.ImageRemoveOptions{
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, client.ImageRemoveOptions{
|
||||||
Force: force,
|
Force: force,
|
||||||
PruneChildren: pruneChildren,
|
PruneChildren: pruneChildren,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|||||||
@@ -6,66 +6,64 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
|
||||||
"github.com/docker/docker/client"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImageExistsLocally(t *testing.T) {
|
// buildScratchImage builds a tiny empty image for the given platform locally (FROM scratch, no
|
||||||
if testing.Short() {
|
// network or emulation since there is nothing to run) and returns its tag, removing it after
|
||||||
t.Skip("skipping integration test")
|
// the test.
|
||||||
}
|
func buildScratchImage(t *testing.T, platform string) string {
|
||||||
ctx := context.Background()
|
t.Helper()
|
||||||
// to help make this test reliable and not flaky, we need to have
|
tag := fmt.Sprintf("act-test-exists-%s:latest", strings.TrimPrefix(platform, "linux/"))
|
||||||
// an image that will exist, and onew that won't exist
|
cmd := exec.Command("docker", "build", "--platform", platform, "-t", tag, "-")
|
||||||
|
cmd.Stdin = strings.NewReader("FROM scratch\nLABEL act-test=1\n")
|
||||||
// Test if image exists with specific tag
|
// Force BuildKit: it records the requested architecture in the image config for a
|
||||||
invalidImageTag, err := ImageExistsLocally(ctx, "library/alpine:this-random-tag-will-never-exist", "linux/amd64")
|
// FROM-scratch build, whereas the classic builder ignores --platform and tags it with the
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
// host arch, which would break the per-platform existence assertions below.
|
||||||
assert.Equal(t, false, invalidImageTag) //nolint:testifylint // pre-existing issue from nektos/act
|
cmd.Env = append(os.Environ(), "DOCKER_BUILDKIT=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
// Test if image exists with specific architecture (image platform)
|
require.NoError(t, err, string(out))
|
||||||
invalidImagePlatform, err := ImageExistsLocally(ctx, "alpine:latest", "windows/amd64")
|
t.Cleanup(func() { _ = exec.Command("docker", "rmi", "-f", tag).Run() })
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
return tag
|
||||||
assert.Equal(t, false, invalidImagePlatform) //nolint:testifylint // pre-existing issue from nektos/act
|
}
|
||||||
|
|
||||||
// pull an image
|
func TestImageExistsLocally(t *testing.T) {
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
requireDocker(t)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
ctx := context.Background()
|
||||||
cli.NegotiateAPIVersion(context.Background())
|
|
||||||
|
// a non-existent image is reported absent
|
||||||
// Chose alpine latest because it's so small
|
missing, err := ImageExistsLocally(ctx, "library/alpine:this-random-tag-will-never-exist", "linux/amd64")
|
||||||
// maybe we should build an image instead so that tests aren't reliable on dockerhub
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
readerDefault, err := cli.ImagePull(ctx, "node:16-buster-slim", types.ImagePullOptions{
|
assert.False(t, missing)
|
||||||
Platform: "linux/amd64",
|
|
||||||
})
|
// Build tiny images for two architectures locally so per-platform existence can be checked
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
// offline (formerly pulled node:24-bookworm-slim for amd64 and arm64 over the network).
|
||||||
defer readerDefault.Close()
|
amd64Ref := buildScratchImage(t, "linux/amd64")
|
||||||
_, err = io.ReadAll(readerDefault)
|
arm64Ref := buildScratchImage(t, "linux/arm64")
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
amd64Exists, err := ImageExistsLocally(ctx, amd64Ref, "linux/amd64")
|
||||||
imageDefaultArchExists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/amd64")
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.True(t, amd64Exists)
|
||||||
assert.Equal(t, true, imageDefaultArchExists) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
// a non-host architecture image is detected for its own architecture
|
||||||
// Validate if another architecture platform can be pulled
|
arm64Exists, err := ImageExistsLocally(ctx, arm64Ref, "linux/arm64")
|
||||||
readerArm64, err := cli.ImagePull(ctx, "node:16-buster-slim", types.ImagePullOptions{
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
Platform: "linux/arm64",
|
assert.True(t, arm64Exists)
|
||||||
})
|
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
// a present image is reported absent for a different platform
|
||||||
defer readerArm64.Close()
|
wrongPlatform, err := ImageExistsLocally(ctx, amd64Ref, "linux/arm64")
|
||||||
_, err = io.ReadAll(readerArm64)
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.False(t, wrongPlatform)
|
||||||
|
|
||||||
imageArm64Exists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/arm64")
|
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Equal(t, true, imageArm64Exists) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ type dockerMessage struct {
|
|||||||
Progress string `json:"progress"`
|
Progress string `json:"progress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const logPrefix = " \U0001F433 "
|
|
||||||
|
|
||||||
func logDockerResponse(logger logrus.FieldLogger, dockerResponse io.ReadCloser, isError bool) error {
|
func logDockerResponse(logger logrus.FieldLogger, dockerResponse io.ReadCloser, isError bool) error {
|
||||||
if dockerResponse == nil {
|
if dockerResponse == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -59,7 +57,7 @@ func logDockerResponse(logger logrus.FieldLogger, dockerResponse io.ReadCloser,
|
|||||||
|
|
||||||
if msg.ErrorDetail.Message != "" {
|
if msg.ErrorDetail.Message != "" {
|
||||||
writeLog(logger, isError, "%s", msg.ErrorDetail.Message)
|
writeLog(logger, isError, "%s", msg.ErrorDetail.Message)
|
||||||
return errors.New(msg.Error)
|
return errors.New(msg.ErrorDetail.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Status != "" {
|
if msg.Status != "" {
|
||||||
|
|||||||
@@ -8,13 +8,70 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/moby/moby/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDockerNetworkCreateExecutor(name string) common.Executor {
|
const (
|
||||||
|
networkCreateAttempts = 3
|
||||||
|
networkCreateRetryDelay = time.Second
|
||||||
|
|
||||||
|
// marks the networks a runner creates for its jobs, so it can tell its own leftovers from
|
||||||
|
// those of another runner sharing the daemon
|
||||||
|
runnerUUIDLabel = "com.gitea.runner.uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RemoveOrphanNetworks removes the networks this runner created for jobs whose teardown did
|
||||||
|
// not get to them: the runner died with the job, the teardown timed out, or the network still
|
||||||
|
// had an endpoint on it at the time. Each one holds a subnet of the daemon's address pool
|
||||||
|
// until it is removed. Networks created after createdBefore are left alone, so a job starting
|
||||||
|
// while this runs cannot lose the network it has created but not yet attached a container to.
|
||||||
|
func RemoveOrphanNetworks(ctx context.Context, runnerUUID string, createdBefore time.Time) error {
|
||||||
|
cli, err := GetDockerClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to connect to the docker daemon: %w", err)
|
||||||
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
|
||||||
|
return removeOrphanNetworks(ctx, cli, runnerUUID, createdBefore)
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeOrphanNetworks(ctx context.Context, cli client.APIClient, runnerUUID string, createdBefore time.Time) error {
|
||||||
|
networks, err := cli.NetworkList(ctx, client.NetworkListOptions{
|
||||||
|
Filters: make(client.Filters).Add("label", runnerUUIDLabel+"="+runnerUUID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var errs []error
|
||||||
|
for _, n := range networks.Items {
|
||||||
|
result, err := cli.NetworkInspect(ctx, n.ID, client.NetworkInspectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("failed to inspect network %s: %w", n.Name, err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// the emptiness check, not the label, is what keeps a live job of another process
|
||||||
|
// sharing this registration safe
|
||||||
|
if len(result.Network.Containers) != 0 || result.Network.Created.After(createdBefore) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := cli.NetworkRemove(ctx, n.ID, client.NetworkRemoveOptions{}); err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("failed to remove network %s: %w", n.Name, err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
common.Logger(ctx).Infof("removed docker network %s left behind by an earlier job", n.Name)
|
||||||
|
}
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDockerNetworkCreateExecutor(name string, opts NewDockerNetworkCreateExecutorInput) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
cli, err := GetDockerClient(ctx)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,29 +80,58 @@ func NewDockerNetworkCreateExecutor(name string) common.Executor {
|
|||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
// Only create the network if it doesn't exist
|
// Only create the network if it doesn't exist
|
||||||
networks, err := cli.NetworkList(ctx, types.NetworkListOptions{})
|
networks, err := cli.NetworkList(ctx, client.NetworkListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// For Gitea, reduce log noise
|
// For Gitea, reduce log noise
|
||||||
// common.Logger(ctx).Debugf("%v", networks)
|
// common.Logger(ctx).Debugf("%v", networks)
|
||||||
for _, network := range networks {
|
for _, n := range networks.Items {
|
||||||
if network.Name == name {
|
if n.Name == name {
|
||||||
common.Logger(ctx).Debugf("Network %v exists", name)
|
common.Logger(ctx).Debugf("Network %v exists", name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = cli.NetworkCreate(ctx, name, types.NetworkCreate{
|
for i := range networkCreateAttempts {
|
||||||
Driver: "bridge",
|
if i > 0 {
|
||||||
Scope: "local",
|
common.Logger(ctx).Infof("Waiting for a free docker address pool to create network %s", name)
|
||||||
})
|
select {
|
||||||
if err != nil {
|
case <-ctx.Done():
|
||||||
return err
|
return ctx.Err()
|
||||||
|
case <-time.After(time.Duration(i) * networkCreateRetryDelay):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err = cli.NetworkCreate(ctx, name, client.NetworkCreateOptions{
|
||||||
|
Driver: "bridge",
|
||||||
|
Scope: "local",
|
||||||
|
EnableIPv4: opts.EnableIPv4,
|
||||||
|
EnableIPv6: opts.EnableIPv6,
|
||||||
|
Labels: runnerLabels(opts.RunnerUUID),
|
||||||
|
}); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !isAddressPoolExhausted(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("docker has no address pool left for this job's network, lower runner.capacity or widen default-address-pools in the docker daemon config: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runnerLabels(runnerUUID string) map[string]string {
|
||||||
|
if runnerUUID == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return map[string]string{runnerUUIDLabel: runnerUUID}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The daemon reports this as a plain invalid-parameter error, the same kind it uses for every
|
||||||
|
// malformed request, so the message is the only discriminator.
|
||||||
|
func isAddressPoolExhausted(err error) bool {
|
||||||
|
msg := err.Error()
|
||||||
|
return strings.Contains(msg, "all predefined address pools have been fully subnetted") ||
|
||||||
|
strings.Contains(msg, "could not find an available, non-overlapping IPv4 address pool among the defaults") // docker 24 and older
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerNetworkRemoveExecutor(name string) common.Executor {
|
func NewDockerNetworkRemoveExecutor(name string) common.Executor {
|
||||||
@@ -56,31 +142,33 @@ func NewDockerNetworkRemoveExecutor(name string) common.Executor {
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
// Make shure that all network of the specified name are removed
|
// Make sure that all network of the specified name are removed
|
||||||
// cli.NetworkRemove refuses to remove a network if there are duplicates
|
// cli.NetworkRemove refuses to remove a network if there are duplicates
|
||||||
networks, err := cli.NetworkList(ctx, types.NetworkListOptions{})
|
networks, err := cli.NetworkList(ctx, client.NetworkListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// For Gitea, reduce log noise
|
// For Gitea, reduce log noise
|
||||||
// common.Logger(ctx).Debugf("%v", networks)
|
// common.Logger(ctx).Debugf("%v", networks)
|
||||||
for _, network := range networks {
|
var errs []error
|
||||||
if network.Name == name {
|
for _, n := range networks.Items {
|
||||||
result, err := cli.NetworkInspect(ctx, network.ID, types.NetworkInspectOptions{})
|
if n.Name == name {
|
||||||
|
result, err := cli.NetworkInspect(ctx, n.ID, client.NetworkInspectOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(result.Containers) == 0 {
|
// it holds a subnet out of the daemon's pool until something reclaims it
|
||||||
if err = cli.NetworkRemove(ctx, network.ID); err != nil {
|
if len(result.Network.Containers) != 0 {
|
||||||
common.Logger(ctx).Debugf("%v", err)
|
common.Logger(ctx).Warnf("Refusing to remove network %s because it still has active endpoints, the idle cleanup reclaims it once they are gone", name)
|
||||||
}
|
continue
|
||||||
} else {
|
}
|
||||||
common.Logger(ctx).Debugf("Refusing to remove network %v because it still has active endpoints", name)
|
if _, err = cli.NetworkRemove(ctx, n.ID, client.NetworkRemoveOptions{}); err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("failed to remove network %s: %w", name, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return errors.Join(errs...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
act/container/docker_network_test.go
Normal file
50
act/container/docker_network_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
|
"github.com/moby/moby/api/types/network"
|
||||||
|
mobyclient "github.com/moby/moby/client"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsAddressPoolExhausted(t *testing.T) {
|
||||||
|
assert.True(t, isAddressPoolExhausted(cerrdefs.ErrInvalidArgument.WithMessage("Error response from daemon: all predefined address pools have been fully subnetted")))
|
||||||
|
assert.True(t, isAddressPoolExhausted(errors.New("could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network")))
|
||||||
|
assert.False(t, isAddressPoolExhausted(cerrdefs.ErrInvalidArgument.WithMessage("invalid subnet 10.0.0.0/8: it overlaps with an existing network")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Of this runner's networks, only the ones nothing is attached to and old enough to predate
|
||||||
|
// any job now starting are the runner's to reclaim. An unexpected NetworkRemove fails the
|
||||||
|
// test on its own, since testify has no expectation to match it against.
|
||||||
|
func TestRemoveOrphanNetworks(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
cutoff := time.Date(2026, time.April, 29, 20, 0, 0, 0, time.UTC)
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("NetworkList", ctx, mobyclient.NetworkListOptions{
|
||||||
|
Filters: make(mobyclient.Filters).Add("label", runnerUUIDLabel+"=runner-1"),
|
||||||
|
}).Return(mobyclient.NetworkListResult{Items: []network.Summary{
|
||||||
|
{Network: network.Network{ID: "orphan"}},
|
||||||
|
{Network: network.Network{ID: "busy"}},
|
||||||
|
{Network: network.Network{ID: "starting"}},
|
||||||
|
}}, nil)
|
||||||
|
client.On("NetworkInspect", ctx, "orphan", mobyclient.NetworkInspectOptions{}).
|
||||||
|
Return(mobyclient.NetworkInspectResult{}, nil)
|
||||||
|
client.On("NetworkInspect", ctx, "busy", mobyclient.NetworkInspectOptions{}).
|
||||||
|
Return(mobyclient.NetworkInspectResult{Network: network.Inspect{Containers: map[string]network.EndpointResource{"c": {}}}}, nil)
|
||||||
|
client.On("NetworkInspect", ctx, "starting", mobyclient.NetworkInspectOptions{}).
|
||||||
|
Return(mobyclient.NetworkInspectResult{Network: network.Inspect{Network: network.Network{Created: cutoff.Add(time.Second)}}}, nil)
|
||||||
|
client.On("NetworkRemove", ctx, "orphan", mobyclient.NetworkRemoveOptions{}).
|
||||||
|
Return(mobyclient.NetworkRemoveResult{}, nil)
|
||||||
|
|
||||||
|
require.NoError(t, removeOrphanNetworks(ctx, client, "runner-1", cutoff))
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
}
|
||||||
39
act/container/docker_platform.go
Normal file
39
act/container/docker_platform.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// Copyright 2025 The nektos/act Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// parsePlatform parses an "os/arch[/variant]" string into a Platform. An empty input
|
||||||
|
// returns (nil, nil), meaning "no platform constraint". A non-empty but malformed
|
||||||
|
// string is rejected explicitly so it cannot silently fall through to the daemon's
|
||||||
|
// default architecture.
|
||||||
|
func parsePlatform(platform string) (*specs.Platform, error) {
|
||||||
|
if platform == "" {
|
||||||
|
return nil, nil //nolint:nilnil // no platform constraint requested
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(platform, "/")
|
||||||
|
if len(parts) < 2 || len(parts) > 3 || parts[0] == "" || parts[1] == "" || (len(parts) == 3 && parts[2] == "") {
|
||||||
|
return nil, fmt.Errorf("invalid platform %q: expected os/arch[/variant]", platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
spec := &specs.Platform{
|
||||||
|
OS: strings.ToLower(parts[0]),
|
||||||
|
Architecture: strings.ToLower(parts[1]),
|
||||||
|
}
|
||||||
|
if len(parts) == 3 {
|
||||||
|
spec.Variant = strings.ToLower(parts[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
return spec, nil
|
||||||
|
}
|
||||||
63
act/container/docker_platform_test.go
Normal file
63
act/container/docker_platform_test.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParsePlatform(t *testing.T) {
|
||||||
|
t.Run("empty input returns nil platform without error", func(t *testing.T) {
|
||||||
|
got, err := parsePlatform("")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Nil(t, got)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("os/arch", func(t *testing.T) {
|
||||||
|
got, err := parsePlatform("linux/amd64")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, got)
|
||||||
|
assert.Equal(t, "linux", got.OS)
|
||||||
|
assert.Equal(t, "amd64", got.Architecture)
|
||||||
|
assert.Empty(t, got.Variant)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("os/arch/variant", func(t *testing.T) {
|
||||||
|
got, err := parsePlatform("linux/arm/v7")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, got)
|
||||||
|
assert.Equal(t, "linux", got.OS)
|
||||||
|
assert.Equal(t, "arm", got.Architecture)
|
||||||
|
assert.Equal(t, "v7", got.Variant)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("input is lowercased", func(t *testing.T) {
|
||||||
|
got, err := parsePlatform("Linux/AMD64/V8")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, got)
|
||||||
|
assert.Equal(t, "linux", got.OS)
|
||||||
|
assert.Equal(t, "amd64", got.Architecture)
|
||||||
|
assert.Equal(t, "v8", got.Variant)
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, bad := range []string{
|
||||||
|
"amd64",
|
||||||
|
"linux",
|
||||||
|
"linux/",
|
||||||
|
"/amd64",
|
||||||
|
"/",
|
||||||
|
"//",
|
||||||
|
"linux/arm/",
|
||||||
|
"linux/arm/v7/extra",
|
||||||
|
} {
|
||||||
|
t.Run("rejects "+bad, func(t *testing.T) {
|
||||||
|
got, err := parsePlatform(bad)
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,45 +8,41 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/distribution/reference"
|
"github.com/distribution/reference"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/moby/moby/api/pkg/authconfig"
|
||||||
"github.com/docker/docker/api/types/registry"
|
"github.com/moby/moby/api/types/registry"
|
||||||
|
"github.com/moby/moby/client"
|
||||||
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDockerPullExecutor function to create a run executor for the container
|
// NewDockerPullExecutor function to create a run executor for the container
|
||||||
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
logger.Debugf("%sdocker pull %v", logPrefix, input.Image)
|
logger.Debugf("docker pull %v", input.Image)
|
||||||
|
|
||||||
if common.Dryrun(ctx) {
|
if common.Dryrun(ctx) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pull := input.ForcePull
|
// skip the pull when the image is already here: either none was forced, or a digest
|
||||||
if !pull {
|
// pins the content so a forced pull could only fetch the same bytes again
|
||||||
|
if !input.ForcePull || isPinnedImage(input.Image) {
|
||||||
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
|
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
|
||||||
logger.Debugf("Image exists? %v", imageExists)
|
logger.Debugf("Image exists? %v", imageExists)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to determine if image already exists for image '%s' (%s): %w", input.Image, input.Platform, err)
|
return fmt.Errorf("unable to determine if image already exists for image '%s' (%s): %w", input.Image, input.Platform, err)
|
||||||
}
|
}
|
||||||
|
if imageExists {
|
||||||
if !imageExists {
|
return nil
|
||||||
pull = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !pull {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
imageRef := cleanImage(ctx, input.Image)
|
imageRef := cleanImage(ctx, input.Image)
|
||||||
logger.Debugf("pulling image '%v' (%s)", imageRef, input.Platform)
|
logger.Debugf("pulling image '%v' (%s)", imageRef, input.Platform)
|
||||||
|
|
||||||
@@ -61,43 +57,58 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
reader, err := cli.ImagePull(ctx, imageRef, imagePullOptions)
|
// the daemon reports a failure that happens after the first progress line in the
|
||||||
|
// stream rather than on the call itself, so both have to be checked
|
||||||
_ = logDockerResponse(logger, reader, err != nil)
|
pullOnce := func(opts client.ImagePullOptions) error {
|
||||||
if err != nil {
|
reader, err := cli.ImagePull(ctx, imageRef, opts)
|
||||||
if imagePullOptions.RegistryAuth != "" && strings.Contains(err.Error(), "unauthorized") {
|
streamErr := logDockerResponse(logger, reader, err != nil)
|
||||||
logger.Errorf("pulling image '%v' (%s) failed with credentials %s retrying without them, please check for stale docker config files", imageRef, input.Platform, err.Error())
|
if err != nil {
|
||||||
imagePullOptions.RegistryAuth = ""
|
return err
|
||||||
reader, err = cli.ImagePull(ctx, imageRef, imagePullOptions)
|
|
||||||
|
|
||||||
_ = logDockerResponse(logger, reader, err != nil)
|
|
||||||
}
|
}
|
||||||
return err
|
return streamErr
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
err = pullOnce(imagePullOptions)
|
||||||
|
if err != nil && imagePullOptions.RegistryAuth != "" && strings.Contains(err.Error(), "unauthorized") {
|
||||||
|
logger.Errorf("pulling image '%v' (%s) failed with credentials %s retrying without them, please check for stale docker config files", imageRef, input.Platform, err.Error())
|
||||||
|
imagePullOptions.RegistryAuth = ""
|
||||||
|
err = pullOnce(imagePullOptions)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// a registry that is down should not fail a job whose image is already here
|
||||||
|
if exists, existsErr := ImageExistsLocally(ctx, input.Image, input.Platform); existsErr == nil && exists {
|
||||||
|
logger.Warnf("could not update image '%s' (%s), continuing with the local copy: %v", imageRef, input.Platform, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to pull image '%s' (%s): %w", imageRef, input.Platform, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput) (types.ImagePullOptions, error) {
|
func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput) (client.ImagePullOptions, error) {
|
||||||
imagePullOptions := types.ImagePullOptions{
|
imagePullOptions := client.ImagePullOptions{}
|
||||||
Platform: input.Platform,
|
platform, err := parsePlatform(input.Platform)
|
||||||
|
if err != nil {
|
||||||
|
return imagePullOptions, err
|
||||||
|
}
|
||||||
|
if platform != nil {
|
||||||
|
imagePullOptions.Platforms = []specs.Platform{*platform}
|
||||||
}
|
}
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
if input.Username != "" && input.Password != "" {
|
if input.Username != "" && input.Password != "" {
|
||||||
logger.Debugf("using authentication for docker pull")
|
logger.Debugf("using authentication for docker pull")
|
||||||
|
|
||||||
authConfig := registry.AuthConfig{
|
encodedAuth, err := authconfig.Encode(registry.AuthConfig{
|
||||||
Username: input.Username,
|
Username: input.Username,
|
||||||
Password: input.Password,
|
Password: input.Password,
|
||||||
}
|
})
|
||||||
|
|
||||||
encodedJSON, err := json.Marshal(authConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return imagePullOptions, err
|
return imagePullOptions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
|
imagePullOptions.RegistryAuth = encodedAuth
|
||||||
} else {
|
} else {
|
||||||
authConfig, err := LoadDockerAuthConfig(ctx, input.Image)
|
authConfig, err := LoadDockerAuthConfig(ctx, input.Image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -108,19 +119,26 @@ func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput)
|
|||||||
}
|
}
|
||||||
logger.Info("using DockerAuthConfig authentication for docker pull")
|
logger.Info("using DockerAuthConfig authentication for docker pull")
|
||||||
|
|
||||||
encodedJSON, err := json.Marshal(authConfig)
|
imagePullOptions.RegistryAuth, err = authconfig.Encode(authConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return imagePullOptions, err
|
return imagePullOptions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return imagePullOptions, nil
|
return imagePullOptions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanImage(ctx context.Context, image string) string {
|
func isPinnedImage(image string) bool {
|
||||||
ref, err := reference.ParseAnyReference(image)
|
ref, err := reference.ParseAnyReference(image)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, pinned := ref.(reference.Canonical)
|
||||||
|
return pinned
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanImage(ctx context.Context, imageName string) string {
|
||||||
|
ref, err := reference.ParseAnyReference(imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.Logger(ctx).Error(err)
|
common.Logger(ctx).Error(err)
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
@@ -6,11 +6,15 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/config"
|
"github.com/docker/cli/cli/config"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -40,10 +44,13 @@ func TestCleanImage(t *testing.T) {
|
|||||||
func TestGetImagePullOptions(t *testing.T) {
|
func TestGetImagePullOptions(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
orig := config.Dir()
|
||||||
|
t.Cleanup(func() { config.SetDir(orig) })
|
||||||
|
|
||||||
config.SetDir("/non-existent/docker")
|
config.SetDir("/non-existent/docker")
|
||||||
|
|
||||||
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
|
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
|
||||||
assert.Nil(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, "", options.RegistryAuth, "RegistryAuth should be empty if no username or password is set") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, "", options.RegistryAuth, "RegistryAuth should be empty if no username or password is set") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
||||||
@@ -51,7 +58,7 @@ func TestGetImagePullOptions(t *testing.T) {
|
|||||||
Username: "username",
|
Username: "username",
|
||||||
Password: "password",
|
Password: "password",
|
||||||
})
|
})
|
||||||
assert.Nil(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", options.RegistryAuth, "Username and Password should be provided")
|
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", options.RegistryAuth, "Username and Password should be provided")
|
||||||
|
|
||||||
config.SetDir("testdata/docker-pull-options")
|
config.SetDir("testdata/docker-pull-options")
|
||||||
@@ -59,6 +66,24 @@ func TestGetImagePullOptions(t *testing.T) {
|
|||||||
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
||||||
Image: "nektos/act",
|
Image: "nektos/act",
|
||||||
})
|
})
|
||||||
assert.Nil(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZFxuIiwic2VydmVyYWRkcmVzcyI6Imh0dHBzOi8vaW5kZXguZG9ja2VyLmlvL3YxLyJ9", options.RegistryAuth, "RegistryAuth should be taken from local docker config")
|
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZFxuIiwic2VydmVyYWRkcmVzcyI6Imh0dHBzOi8vaW5kZXguZG9ja2VyLmlvL3YxLyJ9", options.RegistryAuth, "RegistryAuth should be taken from local docker config")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A digest-pinned image is immutable, so its local copy is always current.
|
||||||
|
func TestIsPinnedImage(t *testing.T) {
|
||||||
|
assert.True(t, isPinnedImage("alpine@sha256:28bd5fe8b56d1bd048e5babf5b10710ebe0bae67db86916198a6eec434943f8b"))
|
||||||
|
assert.False(t, isPinnedImage("alpine:latest"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// The pull path reports a failure the daemon sent mid-stream, so it must carry the reason
|
||||||
|
// whichever of the two shapes the daemon used.
|
||||||
|
func TestLogDockerResponseError(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
streamErr := func(line string) error {
|
||||||
|
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), false)
|
||||||
|
}
|
||||||
|
require.EqualError(t, streamErr(`{"error":"toomanyrequests: rate limit exceeded"}`), "toomanyrequests: rate limit exceeded")
|
||||||
|
require.EqualError(t, streamErr(`{"errorDetail":{"message":"unexpected EOF"}}`), "unexpected EOF")
|
||||||
|
require.NoError(t, streamErr(`{"status":"Downloading"}`))
|
||||||
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,30 +5,38 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/moby/moby/api/pkg/stdcopy"
|
||||||
"github.com/docker/docker/client"
|
"github.com/moby/moby/api/types/container"
|
||||||
|
"github.com/moby/moby/api/types/mount"
|
||||||
|
mobyclient "github.com/moby/moby/client"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDocker(t *testing.T) {
|
func TestDocker(t *testing.T) {
|
||||||
|
requireDocker(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
client, err := GetDockerClient(ctx)
|
client, err := GetDockerClient(ctx)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
require.NoError(t, err)
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
dockerBuild := NewDockerBuildExecutor(NewDockerBuildExecutorInput{
|
dockerBuild := NewDockerBuildExecutor(NewDockerBuildExecutorInput{
|
||||||
@@ -66,28 +74,78 @@ func TestDocker(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type mockDockerClient struct {
|
type mockDockerClient struct {
|
||||||
client.APIClient
|
mobyclient.APIClient
|
||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) ContainerExecCreate(ctx context.Context, id string, opts types.ExecConfig) (types.IDResponse, error) {
|
func (m *mockDockerClient) ExecCreate(ctx context.Context, id string, opts mobyclient.ExecCreateOptions) (mobyclient.ExecCreateResult, error) {
|
||||||
args := m.Called(ctx, id, opts)
|
args := m.Called(ctx, id, opts)
|
||||||
return args.Get(0).(types.IDResponse), args.Error(1)
|
return args.Get(0).(mobyclient.ExecCreateResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) ContainerExecAttach(ctx context.Context, id string, opts types.ExecStartCheck) (types.HijackedResponse, error) {
|
func (m *mockDockerClient) ExecAttach(ctx context.Context, id string, opts mobyclient.ExecAttachOptions) (mobyclient.ExecAttachResult, error) {
|
||||||
args := m.Called(ctx, id, opts)
|
args := m.Called(ctx, id, opts)
|
||||||
return args.Get(0).(types.HijackedResponse), args.Error(1)
|
return args.Get(0).(mobyclient.ExecAttachResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
|
func (m *mockDockerClient) ExecInspect(ctx context.Context, execID string, opts mobyclient.ExecInspectOptions) (mobyclient.ExecInspectResult, error) {
|
||||||
args := m.Called(ctx, execID)
|
args := m.Called(ctx, execID, opts)
|
||||||
return args.Get(0).(types.ContainerExecInspect), args.Error(1)
|
return args.Get(0).(mobyclient.ExecInspectResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) CopyToContainer(ctx context.Context, id, path string, content io.Reader, options types.CopyToContainerOptions) error {
|
func (m *mockDockerClient) ContainerStatPath(ctx context.Context, containerID string, opts mobyclient.ContainerStatPathOptions) (mobyclient.ContainerStatPathResult, error) {
|
||||||
args := m.Called(ctx, id, path, content, options)
|
args := m.Called(ctx, containerID, opts)
|
||||||
return args.Error(0)
|
return args.Get(0).(mobyclient.ContainerStatPathResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerAttach(ctx context.Context, containerID string, opts mobyclient.ContainerAttachOptions) (mobyclient.ContainerAttachResult, error) {
|
||||||
|
args := m.Called(ctx, containerID, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerAttachResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerWait(ctx context.Context, containerID string, opts mobyclient.ContainerWaitOptions) mobyclient.ContainerWaitResult {
|
||||||
|
args := m.Called(ctx, containerID, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerWaitResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) CopyToContainer(ctx context.Context, id string, options mobyclient.CopyToContainerOptions) (mobyclient.CopyToContainerResult, error) {
|
||||||
|
args := m.Called(ctx, id, options)
|
||||||
|
return args.Get(0).(mobyclient.CopyToContainerResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerInspect(ctx context.Context, id string, opts mobyclient.ContainerInspectOptions) (mobyclient.ContainerInspectResult, error) {
|
||||||
|
args := m.Called(ctx, id, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerInspectResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerList(ctx context.Context, opts mobyclient.ContainerListOptions) (mobyclient.ContainerListResult, error) {
|
||||||
|
args := m.Called(ctx, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerListResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerRemove(ctx context.Context, id string, opts mobyclient.ContainerRemoveOptions) (mobyclient.ContainerRemoveResult, error) {
|
||||||
|
args := m.Called(ctx, id, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerRemoveResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) ContainerKill(ctx context.Context, id string, opts mobyclient.ContainerKillOptions) (mobyclient.ContainerKillResult, error) {
|
||||||
|
args := m.Called(ctx, id, opts)
|
||||||
|
return args.Get(0).(mobyclient.ContainerKillResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) NetworkList(ctx context.Context, opts mobyclient.NetworkListOptions) (mobyclient.NetworkListResult, error) {
|
||||||
|
args := m.Called(ctx, opts)
|
||||||
|
return args.Get(0).(mobyclient.NetworkListResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) NetworkInspect(ctx context.Context, id string, opts mobyclient.NetworkInspectOptions) (mobyclient.NetworkInspectResult, error) {
|
||||||
|
args := m.Called(ctx, id, opts)
|
||||||
|
return args.Get(0).(mobyclient.NetworkInspectResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) NetworkRemove(ctx context.Context, id string, opts mobyclient.NetworkRemoveOptions) (mobyclient.NetworkRemoveResult, error) {
|
||||||
|
args := m.Called(ctx, id, opts)
|
||||||
|
return args.Get(0).(mobyclient.NetworkRemoveResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
type endlessReader struct {
|
type endlessReader struct {
|
||||||
@@ -119,10 +177,12 @@ func TestDockerExecAbort(t *testing.T) {
|
|||||||
conn.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
|
conn.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
|
||||||
|
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerExecCreate", ctx, "123", mock.AnythingOfType("types.ExecConfig")).Return(types.IDResponse{ID: "id"}, nil)
|
client.On("ExecCreate", ctx, "123", mock.AnythingOfType("client.ExecCreateOptions")).Return(mobyclient.ExecCreateResult{ID: "id"}, nil)
|
||||||
client.On("ContainerExecAttach", ctx, "id", mock.AnythingOfType("types.ExecStartCheck")).Return(types.HijackedResponse{
|
client.On("ExecAttach", ctx, "id", mock.AnythingOfType("client.ExecAttachOptions")).Return(mobyclient.ExecAttachResult{
|
||||||
Conn: conn,
|
HijackedResponse: mobyclient.HijackedResponse{
|
||||||
Reader: bufio.NewReader(endlessReader{}),
|
Conn: conn,
|
||||||
|
Reader: bufio.NewReader(endlessReader{}),
|
||||||
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
cr := &containerReference{
|
cr := &containerReference{
|
||||||
@@ -156,12 +216,14 @@ func TestDockerExecFailure(t *testing.T) {
|
|||||||
conn := &mockConn{}
|
conn := &mockConn{}
|
||||||
|
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerExecCreate", ctx, "123", mock.AnythingOfType("types.ExecConfig")).Return(types.IDResponse{ID: "id"}, nil)
|
client.On("ExecCreate", ctx, "123", mock.AnythingOfType("client.ExecCreateOptions")).Return(mobyclient.ExecCreateResult{ID: "id"}, nil)
|
||||||
client.On("ContainerExecAttach", ctx, "id", mock.AnythingOfType("types.ExecStartCheck")).Return(types.HijackedResponse{
|
client.On("ExecAttach", ctx, "id", mock.AnythingOfType("client.ExecAttachOptions")).Return(mobyclient.ExecAttachResult{
|
||||||
Conn: conn,
|
HijackedResponse: mobyclient.HijackedResponse{
|
||||||
Reader: bufio.NewReader(strings.NewReader("output")),
|
Conn: conn,
|
||||||
|
Reader: bufio.NewReader(strings.NewReader("output")),
|
||||||
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
client.On("ContainerExecInspect", ctx, "id").Return(types.ContainerExecInspect{
|
client.On("ExecInspect", ctx, "id", mobyclient.ExecInspectOptions{}).Return(mobyclient.ExecInspectResult{
|
||||||
ExitCode: 1,
|
ExitCode: 1,
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
@@ -174,20 +236,143 @@ func TestDockerExecFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx)
|
err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx)
|
||||||
assert.Error(t, err, "exit with `FAILURE`: 1") //nolint:testifylint // pre-existing issue from nektos/act
|
var exitErr ExitCodeError
|
||||||
|
require.ErrorAs(t, err, &exitErr)
|
||||||
|
assert.Equal(t, ExitCodeError(1), exitErr)
|
||||||
|
assert.Equal(t, "Process completed with exit code 1.", err.Error())
|
||||||
|
|
||||||
conn.AssertExpectations(t)
|
conn.AssertExpectations(t)
|
||||||
client.AssertExpectations(t)
|
client.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stdcopyFrame wraps payload in a single Docker multiplexed-stream frame, the
|
||||||
|
// format StdCopy expects: an 8-byte header (stream type + 4-byte big-endian
|
||||||
|
// length) followed by the payload.
|
||||||
|
func stdcopyFrame(stream stdcopy.StdType, payload string) []byte {
|
||||||
|
b := make([]byte, 8+len(payload))
|
||||||
|
b[0] = byte(stream)
|
||||||
|
binary.BigEndian.PutUint32(b[4:8], uint32(len(payload)))
|
||||||
|
copy(b[8:], payload)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDockerAttachFlushesTrailingLine verifies that wait() blocks until the
|
||||||
|
// attach() streaming goroutine has drained and flushed the container's output,
|
||||||
|
// so a final line without a trailing newline is not lost.
|
||||||
|
func TestDockerAttachFlushesTrailingLine(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
framed := bytes.NewBuffer(stdcopyFrame(stdcopy.Stdout, "line one\nlast line without newline"))
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
logWriter := common.NewLineWriter(func(s string) bool {
|
||||||
|
lines = append(lines, s)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerAttach", ctx, "123", mock.AnythingOfType("client.ContainerAttachOptions")).
|
||||||
|
Return(mobyclient.ContainerAttachResult{
|
||||||
|
HijackedResponse: mobyclient.HijackedResponse{
|
||||||
|
Conn: &mockConn{},
|
||||||
|
Reader: bufio.NewReader(framed),
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
statusCh := make(chan container.WaitResponse, 1)
|
||||||
|
statusCh <- container.WaitResponse{StatusCode: 0}
|
||||||
|
errCh := make(chan error, 1)
|
||||||
|
client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}).
|
||||||
|
Return(mobyclient.ContainerWaitResult{
|
||||||
|
Result: (<-chan container.WaitResponse)(statusCh),
|
||||||
|
Error: (<-chan error)(errCh),
|
||||||
|
})
|
||||||
|
|
||||||
|
cr := &containerReference{
|
||||||
|
id: "123",
|
||||||
|
cli: client,
|
||||||
|
input: &NewContainerInput{
|
||||||
|
Image: "image",
|
||||||
|
Stdout: logWriter,
|
||||||
|
Stderr: logWriter,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, cr.attach()(ctx))
|
||||||
|
require.NoError(t, cr.wait()(ctx))
|
||||||
|
|
||||||
|
// wait() must have blocked until the goroutine drained AND flushed; the
|
||||||
|
// trailing, non-newline-terminated line must therefore be present. Reading
|
||||||
|
// lines here is race-free because wait() synchronizes on attachDone, which
|
||||||
|
// the goroutine closes after the final append.
|
||||||
|
assert.Equal(t, []string{"line one\n", "last line without newline"}, lines)
|
||||||
|
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDockerWaitFailure(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
statusCh := make(chan container.WaitResponse, 1)
|
||||||
|
statusCh <- container.WaitResponse{StatusCode: 2}
|
||||||
|
errCh := make(chan error, 1)
|
||||||
|
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}).
|
||||||
|
Return(mobyclient.ContainerWaitResult{
|
||||||
|
Result: (<-chan container.WaitResponse)(statusCh),
|
||||||
|
Error: (<-chan error)(errCh),
|
||||||
|
})
|
||||||
|
|
||||||
|
cr := &containerReference{
|
||||||
|
id: "123",
|
||||||
|
cli: client,
|
||||||
|
input: &NewContainerInput{
|
||||||
|
Image: "image",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cr.wait()(ctx)
|
||||||
|
var exitErr ExitCodeError
|
||||||
|
require.ErrorAs(t, err, &exitErr)
|
||||||
|
assert.Equal(t, ExitCodeError(2), exitErr)
|
||||||
|
assert.Equal(t, "Process completed with exit code 2.", err.Error())
|
||||||
|
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stubStatPath answers path resolution: the given paths exist, mapped to their target
|
||||||
|
// when they are a symlink, everything else does not exist.
|
||||||
|
func stubStatPath(client *mockDockerClient, existing map[string]string) {
|
||||||
|
for containerPath, target := range existing {
|
||||||
|
client.On("ContainerStatPath", mock.Anything, "123", mobyclient.ContainerStatPathOptions{Path: containerPath}).
|
||||||
|
Return(mobyclient.ContainerStatPathResult{Stat: container.PathStat{LinkTarget: target}}, nil).Maybe()
|
||||||
|
}
|
||||||
|
client.On("ContainerStatPath", mock.Anything, "123", mock.Anything).
|
||||||
|
Return(mobyclient.ContainerStatPathResult{}, cerrdefs.ErrNotFound).Maybe()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The mkdir tarball is extracted at the deepest existing ancestor, with entries relative
|
||||||
|
// to it that never traverse the "/var/run" symlink, see moby/moby#53258.
|
||||||
func TestDockerCopyTarStream(t *testing.T) {
|
func TestDockerCopyTarStream(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
conn := &mockConn{}
|
var mkdirNames []string
|
||||||
|
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("CopyToContainer", ctx, "123", "/", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(nil)
|
stubStatPath(client, map[string]string{"/var": "", "/var/run": "/run", "/run": ""})
|
||||||
client.On("CopyToContainer", ctx, "123", "/var/run/act", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(nil)
|
client.On("CopyToContainer", ctx, "123", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
||||||
|
if opts.DestinationPath != "/run" || opts.Content == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
tr := tar.NewReader(opts.Content)
|
||||||
|
for hdr, err := tr.Next(); err == nil; hdr, err = tr.Next() {
|
||||||
|
mkdirNames = append(mkdirNames, hdr.Name)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})).Return(mobyclient.CopyToContainerResult{}, nil)
|
||||||
|
client.On("CopyToContainer", ctx, "123", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
||||||
|
return opts.DestinationPath == "/run/act" && opts.Content != nil
|
||||||
|
})).Return(mobyclient.CopyToContainerResult{}, nil)
|
||||||
cr := &containerReference{
|
cr := &containerReference{
|
||||||
id: "123",
|
id: "123",
|
||||||
cli: client,
|
cli: client,
|
||||||
@@ -196,60 +381,231 @@ func TestDockerCopyTarStream(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{})
|
require.NoError(t, cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{}))
|
||||||
|
assert.Equal(t, []string{"act"}, mkdirNames)
|
||||||
|
|
||||||
conn.AssertExpectations(t)
|
|
||||||
client.AssertExpectations(t)
|
client.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerCopyTarStreamErrorInCopyFiles(t *testing.T) {
|
func TestDockerCopyTarStreamErrors(t *testing.T) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
conn := &mockConn{}
|
|
||||||
|
|
||||||
merr := errors.New("Failure")
|
merr := errors.New("Failure")
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
mkdirErr error
|
||||||
|
copyErr error
|
||||||
|
}{
|
||||||
|
{"mkdir", merr, nil},
|
||||||
|
{"copy content", nil, merr},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("CopyToContainer", ctx, "123", "/", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(merr)
|
stubStatPath(client, map[string]string{"/var": "", "/var/run": ""})
|
||||||
client.On("CopyToContainer", ctx, "123", "/", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(merr)
|
client.On("CopyToContainer", ctx, "123", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
||||||
cr := &containerReference{
|
return opts.DestinationPath == "/var/run" && opts.Content != nil
|
||||||
id: "123",
|
})).Return(mobyclient.CopyToContainerResult{}, testCase.mkdirErr)
|
||||||
cli: client,
|
client.On("CopyToContainer", ctx, "123", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
||||||
input: &NewContainerInput{
|
return opts.DestinationPath == "/var/run/act" && opts.Content != nil
|
||||||
Image: "image",
|
})).Return(mobyclient.CopyToContainerResult{}, testCase.copyErr).Maybe()
|
||||||
},
|
cr := &containerReference{
|
||||||
|
id: "123",
|
||||||
|
cli: client,
|
||||||
|
input: &NewContainerInput{
|
||||||
|
Image: "image",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
require.ErrorIs(t, cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{}), merr)
|
||||||
|
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err := cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{})
|
// A remove that raced the daemon's AutoRemove teardown is not a failure and must not
|
||||||
assert.ErrorIs(t, err, merr) //nolint:testifylint // pre-existing issue from nektos/act
|
// be logged as one.
|
||||||
|
func TestRemoveIgnoresAutoRemoveRace(t *testing.T) {
|
||||||
|
removeOpts := mobyclient.ContainerRemoveOptions{RemoveVolumes: true, Force: true}
|
||||||
|
killOpts := mobyclient.ContainerKillOptions{Signal: "SIGKILL"}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
wantWait bool
|
||||||
|
wantFailure bool
|
||||||
|
}{
|
||||||
|
{name: "removal in progress", err: cerrdefs.ErrConflict.WithMessage("removal of container abc is already in progress"), wantWait: true},
|
||||||
|
{name: "already removed", err: cerrdefs.ErrNotFound.WithMessage("No such container: abc")},
|
||||||
|
{name: "removed cleanly", err: nil},
|
||||||
|
{name: "real failure", err: errors.New("driver failed to remove root filesystem"), wantFailure: true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
logger, hook := test.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerKill", ctx, "abc", killOpts).Return(mobyclient.ContainerKillResult{}, nil)
|
||||||
|
client.On("ContainerRemove", ctx, "abc", removeOpts).Return(mobyclient.ContainerRemoveResult{}, tc.err)
|
||||||
|
if tc.wantWait {
|
||||||
|
removed := make(chan container.WaitResponse, 1)
|
||||||
|
removed <- container.WaitResponse{}
|
||||||
|
client.On("ContainerWait", mock.Anything, "abc", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionRemoved}).
|
||||||
|
Return(mobyclient.ContainerWaitResult{Result: removed})
|
||||||
|
}
|
||||||
|
cr := &containerReference{id: "abc", cli: client}
|
||||||
|
|
||||||
conn.AssertExpectations(t)
|
require.NoError(t, cr.remove()(ctx))
|
||||||
|
// a failure keeps the id, so a later Remove() can retry it
|
||||||
|
if tc.wantFailure {
|
||||||
|
assert.Equal(t, "abc", cr.id)
|
||||||
|
assert.Len(t, hook.AllEntries(), 1)
|
||||||
|
} else {
|
||||||
|
assert.Empty(t, cr.id)
|
||||||
|
assert.Empty(t, hook.AllEntries())
|
||||||
|
}
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A container whose id was never learned, because find() could not reach the daemon or
|
||||||
|
// create() lost its reply, must still be removed rather than leaking with its network. It
|
||||||
|
// was never started here, so it is not worth a kill of its own.
|
||||||
|
func TestRemoveWithoutIDUsesName(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerRemove", ctx, "job-1", mobyclient.ContainerRemoveOptions{RemoveVolumes: true, Force: true}).
|
||||||
|
Return(mobyclient.ContainerRemoveResult{}, nil)
|
||||||
|
cr := &containerReference{cli: client, input: &NewContainerInput{Name: "job-1"}}
|
||||||
|
|
||||||
|
require.NoError(t, cr.remove()(ctx))
|
||||||
client.AssertExpectations(t)
|
client.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerCopyTarStreamErrorInMkdir(t *testing.T) {
|
// find() must drop a stale cached id so later Copy/Exec don't hit the
|
||||||
|
// daemon with a torn-down container.
|
||||||
|
func TestFindRevalidatesStaleID(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
notFound := cerrdefs.ErrNotFound.WithMessage("No such container")
|
||||||
|
boom := errors.New("daemon unreachable")
|
||||||
|
newCR := func(id string) (*containerReference, *mockDockerClient) {
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
return &containerReference{id: id, cli: client, input: &NewContainerInput{Name: "job-1"}}, client
|
||||||
|
}
|
||||||
|
listOpts := mobyclient.ContainerListOptions{All: true}
|
||||||
|
inspectOpts := mobyclient.ContainerInspectOptions{}
|
||||||
|
|
||||||
|
t.Run("stale id cleared, name lookup empty", func(t *testing.T) {
|
||||||
|
cr, client := newCR("stale")
|
||||||
|
client.On("ContainerInspect", ctx, "stale", inspectOpts).Return(mobyclient.ContainerInspectResult{}, notFound)
|
||||||
|
client.On("ContainerList", ctx, listOpts).Return(mobyclient.ContainerListResult{}, nil)
|
||||||
|
require.NoError(t, cr.find()(ctx))
|
||||||
|
assert.Empty(t, cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("stale id cleared, name lookup repopulates", func(t *testing.T) {
|
||||||
|
cr, client := newCR("stale")
|
||||||
|
client.On("ContainerInspect", ctx, "stale", inspectOpts).Return(mobyclient.ContainerInspectResult{}, notFound)
|
||||||
|
client.On("ContainerList", ctx, listOpts).Return(mobyclient.ContainerListResult{Items: []container.Summary{
|
||||||
|
{ID: "other", Names: []string{"/somebody-else"}},
|
||||||
|
{ID: "fresh", Names: []string{"/job-1"}},
|
||||||
|
}}, nil)
|
||||||
|
require.NoError(t, cr.find()(ctx))
|
||||||
|
assert.Equal(t, "fresh", cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("live id kept", func(t *testing.T) {
|
||||||
|
cr, client := newCR("live")
|
||||||
|
client.On("ContainerInspect", ctx, "live", inspectOpts).Return(mobyclient.ContainerInspectResult{}, nil)
|
||||||
|
require.NoError(t, cr.find()(ctx))
|
||||||
|
assert.Equal(t, "live", cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("transient inspect error trusts cache", func(t *testing.T) {
|
||||||
|
cr, client := newCR("live")
|
||||||
|
client.On("ContainerInspect", ctx, "live", inspectOpts).Return(mobyclient.ContainerInspectResult{}, boom)
|
||||||
|
require.NoError(t, cr.find()(ctx))
|
||||||
|
assert.Equal(t, "live", cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("list error propagates", func(t *testing.T) {
|
||||||
|
cr, client := newCR("")
|
||||||
|
client.On("ContainerList", ctx, listOpts).Return(mobyclient.ContainerListResult{}, boom)
|
||||||
|
require.ErrorIs(t, cr.find()(ctx), boom)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every daemon entry point fails fast with a clear, container-named
|
||||||
|
// error when no live cr.id is known.
|
||||||
|
func TestRejectsMissingContainer(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{}, nil)
|
||||||
|
cr := &containerReference{cli: client, input: &NewContainerInput{Name: "job-1"}}
|
||||||
|
check := func(op string, err error) {
|
||||||
|
t.Helper()
|
||||||
|
require.Error(t, err, op)
|
||||||
|
assert.Contains(t, err.Error(), `container "job-1" does not exist`, op)
|
||||||
|
}
|
||||||
|
check("copyContent", cr.copyContent("/var/run/act", &FileEntry{Name: "x", Mode: 0o644})(ctx))
|
||||||
|
check("copyDir", cr.copyDir("/var/run/act", "/src", false)(ctx))
|
||||||
|
check("CopyTarStream", cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{}))
|
||||||
|
check("exec", cr.exec([]string{"echo"}, nil, "", "")(ctx))
|
||||||
|
_, err := cr.GetContainerArchive(ctx, "/var/run/act/x")
|
||||||
|
check("GetContainerArchive", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// End-to-end: a stale cr.id is cleared, repopulated from name lookup,
|
||||||
|
// and the Copy completes against the fresh id.
|
||||||
|
func TestPublicCopyPipelineHandlesStaleID(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerInspect", ctx, "stale", mobyclient.ContainerInspectOptions{}).
|
||||||
|
Return(mobyclient.ContainerInspectResult{}, cerrdefs.ErrNotFound.WithMessage("gone"))
|
||||||
|
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).
|
||||||
|
Return(mobyclient.ContainerListResult{Items: []container.Summary{
|
||||||
|
{ID: "fresh", Names: []string{"/job-1"}},
|
||||||
|
}}, nil)
|
||||||
|
client.On("CopyToContainer", ctx, "fresh", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
||||||
|
return opts.DestinationPath == "/var/run/act"
|
||||||
|
})).Return(mobyclient.CopyToContainerResult{}, nil)
|
||||||
|
|
||||||
|
cr := &containerReference{id: "stale", cli: client, input: &NewContainerInput{Name: "job-1"}}
|
||||||
|
require.NoError(t, cr.Copy("/var/run/act", &FileEntry{Name: "x", Mode: 0o644})(ctx))
|
||||||
|
assert.Equal(t, "fresh", cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDockerCopyToSymlinkPath is a regression test for gitea/runner#981. Most base images
|
||||||
|
// symlink /var/run to /run, so copying into /var/run/act traverses that symlink. The broken
|
||||||
|
// docker 29.5.1 daemon fails the extraction with "mkdirat var/run: file exists" (fixed in
|
||||||
|
// 29.5.2). Running against the daemon shipped in the dind image, this catches a bad bump.
|
||||||
|
func TestDockerCopyToSymlinkPath(t *testing.T) {
|
||||||
|
requireDocker(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
conn := &mockConn{}
|
rc := NewContainer(&NewContainerInput{
|
||||||
|
Image: "alpine:latest",
|
||||||
|
Entrypoint: []string{"sleep", "30"},
|
||||||
|
Name: "act-test-symlink-" + time.Now().Format("20060102150405.000000"),
|
||||||
|
AutoRemove: true,
|
||||||
|
})
|
||||||
|
require.NoError(t, rc.Pull(false)(ctx))
|
||||||
|
require.NoError(t, rc.Create(nil, nil)(ctx))
|
||||||
|
require.NoError(t, rc.Start(false)(ctx))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = rc.Remove()(ctx)
|
||||||
|
_ = rc.Close()(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
merr := errors.New("Failure")
|
// CopyTarStream resolves the var/run symlink and creates act below its target, the
|
||||||
|
// exact step that fails on a broken daemon.
|
||||||
client := &mockDockerClient{}
|
err := rc.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{})
|
||||||
client.On("CopyToContainer", ctx, "123", "/", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(nil)
|
require.NoError(t, err)
|
||||||
client.On("CopyToContainer", ctx, "123", "/var/run/act", mock.Anything, mock.AnythingOfType("types.CopyToContainerOptions")).Return(merr)
|
|
||||||
cr := &containerReference{
|
|
||||||
id: "123",
|
|
||||||
cli: client,
|
|
||||||
input: &NewContainerInput{
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := cr.CopyTarStream(ctx, "/var/run/act", &bytes.Buffer{})
|
|
||||||
assert.ErrorIs(t, err, merr) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
conn.AssertExpectations(t)
|
|
||||||
client.AssertExpectations(t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type assert containerReference implements ExecutionsEnvironment
|
// Type assert containerReference implements ExecutionsEnvironment
|
||||||
@@ -327,3 +683,163 @@ func TestCheckVolumes(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitizeOptionsHostConfig(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
|
||||||
|
dangerous := func() *container.HostConfig {
|
||||||
|
return &container.HostConfig{
|
||||||
|
PidMode: "host",
|
||||||
|
IpcMode: "host",
|
||||||
|
UTSMode: "host",
|
||||||
|
CgroupnsMode: "host",
|
||||||
|
UsernsMode: "host",
|
||||||
|
CapAdd: []string{"ALL"},
|
||||||
|
SecurityOpt: []string{"seccomp=unconfined", "apparmor=unconfined"},
|
||||||
|
VolumesFrom: []string{"other"},
|
||||||
|
Runtime: "runc",
|
||||||
|
Resources: container.Resources{
|
||||||
|
CgroupParent: "/custom",
|
||||||
|
Devices: []container.DeviceMapping{{PathOnHost: "/dev/sda", PathInContainer: "/dev/sda", CgroupPermissions: "rwm"}},
|
||||||
|
DeviceCgroupRules: []string{"a *:* rwm"},
|
||||||
|
},
|
||||||
|
Sysctls: map[string]string{"net.ipv4.ip_forward": "1"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hostConfig := dangerous()
|
||||||
|
sanitizeOptionsHostConfig(logger, hostConfig)
|
||||||
|
|
||||||
|
assert.Empty(t, string(hostConfig.PidMode))
|
||||||
|
assert.Empty(t, string(hostConfig.IpcMode))
|
||||||
|
assert.Empty(t, string(hostConfig.UTSMode))
|
||||||
|
assert.Empty(t, string(hostConfig.CgroupnsMode))
|
||||||
|
assert.Empty(t, string(hostConfig.UsernsMode))
|
||||||
|
assert.Empty(t, hostConfig.CapAdd)
|
||||||
|
assert.Empty(t, hostConfig.SecurityOpt)
|
||||||
|
assert.Empty(t, hostConfig.Devices)
|
||||||
|
assert.Empty(t, hostConfig.DeviceCgroupRules)
|
||||||
|
assert.Empty(t, hostConfig.VolumesFrom)
|
||||||
|
assert.Empty(t, hostConfig.Runtime)
|
||||||
|
assert.Empty(t, hostConfig.CgroupParent)
|
||||||
|
assert.Empty(t, hostConfig.Sysctls)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergeContainerConfigsStripsDangerousOptionsWhenUnprivileged(t *testing.T) {
|
||||||
|
// OS-independent options only: --device parsing requires a linux/windows
|
||||||
|
// server OS, which is not guaranteed for the test host.
|
||||||
|
const dangerousOptions = "--pid=host --ipc=host --uts=host --cgroupns=host " +
|
||||||
|
"--userns=host --cap-add=ALL --security-opt seccomp=unconfined " +
|
||||||
|
"--security-opt apparmor=unconfined --volumes-from other " +
|
||||||
|
"--runtime runc --cgroup-parent /custom --sysctl net.ipv4.ip_forward=1"
|
||||||
|
|
||||||
|
t.Run("unprivileged strips host-escape options", func(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
cr := &containerReference{
|
||||||
|
input: &NewContainerInput{
|
||||||
|
Options: dangerousOptions,
|
||||||
|
NetworkMode: "bridge",
|
||||||
|
UsernsMode: "private",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, hostConfig, err := cr.mergeContainerConfigs(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Privileged: false,
|
||||||
|
UsernsMode: container.UsernsMode("private"),
|
||||||
|
NetworkMode: container.NetworkMode("bridge"),
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.False(t, hostConfig.Privileged)
|
||||||
|
assert.Empty(t, string(hostConfig.PidMode))
|
||||||
|
assert.Empty(t, string(hostConfig.IpcMode))
|
||||||
|
assert.Empty(t, string(hostConfig.UTSMode))
|
||||||
|
assert.Empty(t, string(hostConfig.CgroupnsMode))
|
||||||
|
// UsernsMode must keep the runner-controlled value, not the one from options.
|
||||||
|
assert.Equal(t, "private", string(hostConfig.UsernsMode))
|
||||||
|
assert.Empty(t, hostConfig.CapAdd)
|
||||||
|
assert.Empty(t, hostConfig.SecurityOpt)
|
||||||
|
assert.Empty(t, hostConfig.VolumesFrom)
|
||||||
|
assert.Empty(t, hostConfig.Runtime)
|
||||||
|
assert.Empty(t, hostConfig.CgroupParent)
|
||||||
|
assert.Empty(t, hostConfig.Sysctls)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("privileged preserves options", func(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
cr := &containerReference{
|
||||||
|
input: &NewContainerInput{
|
||||||
|
Options: "--pid=host --cap-add=ALL --security-opt seccomp=unconfined",
|
||||||
|
NetworkMode: "bridge",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, hostConfig, err := cr.mergeContainerConfigs(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Privileged: true,
|
||||||
|
NetworkMode: container.NetworkMode("bridge"),
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "host", string(hostConfig.PidMode))
|
||||||
|
assert.Equal(t, []string{"ALL"}, hostConfig.CapAdd)
|
||||||
|
assert.Equal(t, []string{"seccomp=unconfined"}, hostConfig.SecurityOpt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckVolumesRejectsEscapingHostPaths(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
|
||||||
|
base := t.TempDir()
|
||||||
|
allowed := filepath.Join(base, "allowed")
|
||||||
|
denied := filepath.Join(base, "denied")
|
||||||
|
require.NoError(t, os.MkdirAll(allowed, 0o700))
|
||||||
|
require.NoError(t, os.MkdirAll(denied, 0o700))
|
||||||
|
|
||||||
|
cr := &containerReference{
|
||||||
|
input: &NewContainerInput{
|
||||||
|
ValidVolumes: []string{filepath.Join(allowed, "**")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
escapingPath := allowed + string(filepath.Separator) + ".." + string(filepath.Separator) + "denied"
|
||||||
|
_, hostConf := cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Binds: []string{escapingPath + ":/mnt"},
|
||||||
|
})
|
||||||
|
assert.Empty(t, hostConf.Binds)
|
||||||
|
|
||||||
|
linkPath := filepath.Join(allowed, "link")
|
||||||
|
if err := os.Symlink(denied, linkPath); err != nil {
|
||||||
|
t.Skipf("cannot create symlink: %v", err)
|
||||||
|
}
|
||||||
|
_, hostConf = cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Binds: []string{linkPath + ":/mnt"},
|
||||||
|
})
|
||||||
|
assert.Empty(t, hostConf.Binds)
|
||||||
|
|
||||||
|
_, hostConf = cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Binds: []string{filepath.Join(linkPath, "missing") + ":/mnt"},
|
||||||
|
})
|
||||||
|
assert.Empty(t, hostConf.Binds)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergeContainerConfigsVolumesReplaceRunnerMounts(t *testing.T) {
|
||||||
|
logger, _ := test.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
cr := &containerReference{
|
||||||
|
input: &NewContainerInput{
|
||||||
|
NetworkMode: "bridge",
|
||||||
|
Options: "--volume /host/tools:/opt/hostedtoolcache",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, hostConf, err := cr.mergeContainerConfigs(ctx, &container.Config{}, &container.HostConfig{
|
||||||
|
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
|
||||||
|
Mounts: []mount.Mount{{Type: mount.TypeVolume, Source: "act-toolcache", Target: "/opt/hostedtoolcache"}},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []string{"/var/run/docker.sock:/var/run/docker.sock", "/host/tools:/opt/hostedtoolcache"}, hostConf.Binds)
|
||||||
|
assert.Empty(t, hostConf.Mounts)
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,9 +18,19 @@ func init() {
|
|||||||
|
|
||||||
var originalCommonSocketLocations = CommonSocketLocations
|
var originalCommonSocketLocations = CommonSocketLocations
|
||||||
|
|
||||||
|
func isolateSocketEnv(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
t.Cleanup(func() { CommonSocketLocations = originalCommonSocketLocations })
|
||||||
|
if host, ok := os.LookupEnv("DOCKER_HOST"); ok {
|
||||||
|
t.Setenv("DOCKER_HOST", host)
|
||||||
|
} else {
|
||||||
|
t.Cleanup(func() { os.Unsetenv("DOCKER_HOST") })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetSocketAndHostWithSocket(t *testing.T) {
|
func TestGetSocketAndHostWithSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
dockerHost := "unix:///my/docker/host.sock"
|
dockerHost := "unix:///my/docker/host.sock"
|
||||||
socketURI := "/path/to/my.socket"
|
socketURI := "/path/to/my.socket"
|
||||||
t.Setenv("DOCKER_HOST", dockerHost)
|
t.Setenv("DOCKER_HOST", dockerHost)
|
||||||
@@ -29,7 +39,7 @@ func TestGetSocketAndHostWithSocket(t *testing.T) {
|
|||||||
ret, err := GetSocketAndHost(socketURI)
|
ret, err := GetSocketAndHost(socketURI)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{socketURI, dockerHost}, ret)
|
assert.Equal(t, SocketAndHost{socketURI, dockerHost}, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,30 +52,30 @@ func TestGetSocketAndHostNoSocket(t *testing.T) {
|
|||||||
ret, err := GetSocketAndHost("")
|
ret, err := GetSocketAndHost("")
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{dockerHost, dockerHost}, ret)
|
assert.Equal(t, SocketAndHost{dockerHost, dockerHost}, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSocketAndHostOnlySocket(t *testing.T) {
|
func TestGetSocketAndHostOnlySocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
socketURI := "/path/to/my.socket"
|
socketURI := "/path/to/my.socket"
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
|
||||||
defaultSocket, defaultSocketFound := socketLocation()
|
defaultSocket, defaultSocketFound := socketLocation()
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
ret, err := GetSocketAndHost(socketURI)
|
ret, err := GetSocketAndHost(socketURI)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.NoError(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, true, defaultSocketFound, "Expected to find default socket") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.True(t, defaultSocketFound, "Expected to find default socket")
|
||||||
assert.Equal(t, socketURI, ret.Socket, "Expected socket to match common location")
|
assert.Equal(t, socketURI, ret.Socket, "Expected socket to match common location")
|
||||||
assert.Equal(t, defaultSocket, ret.Host, "Expected ret.Host to match default socket location")
|
assert.Equal(t, defaultSocket, ret.Host, "Expected ret.Host to match default socket location")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSocketAndHostDontMount(t *testing.T) {
|
func TestGetSocketAndHostDontMount(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
dockerHost := "unix:///my/docker/host.sock"
|
dockerHost := "unix:///my/docker/host.sock"
|
||||||
t.Setenv("DOCKER_HOST", dockerHost)
|
t.Setenv("DOCKER_HOST", dockerHost)
|
||||||
|
|
||||||
@@ -73,13 +83,13 @@ func TestGetSocketAndHostDontMount(t *testing.T) {
|
|||||||
ret, err := GetSocketAndHost("-")
|
ret, err := GetSocketAndHost("-")
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{"-", dockerHost}, ret)
|
assert.Equal(t, SocketAndHost{"-", dockerHost}, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
defaultSocket, found := socketLocation()
|
defaultSocket, found := socketLocation()
|
||||||
|
|
||||||
@@ -87,8 +97,8 @@ func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
|||||||
ret, err := GetSocketAndHost("")
|
ret, err := GetSocketAndHost("")
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Equal(t, true, found, "Expected a default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.True(t, found, "Expected a default socket to be found")
|
||||||
assert.Nil(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{defaultSocket, defaultSocket}, ret, "Expected to match default socket location")
|
assert.Equal(t, SocketAndHost{defaultSocket, defaultSocket}, ret, "Expected to match default socket location")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +107,7 @@ func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
|||||||
// > This happens if neither DOCKER_HOST nor --container-daemon-socket has a value, but socketLocation() returns a URI
|
// > This happens if neither DOCKER_HOST nor --container-daemon-socket has a value, but socketLocation() returns a URI
|
||||||
func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
mySocketFile, tmpErr := os.CreateTemp(t.TempDir(), "act-*.sock")
|
mySocketFile, tmpErr := os.CreateTemp(t.TempDir(), "act-*.sock")
|
||||||
mySocket := mySocketFile.Name()
|
mySocket := mySocketFile.Name()
|
||||||
unixSocket := "unix://" + mySocket
|
unixSocket := "unix://" + mySocket
|
||||||
@@ -112,13 +123,14 @@ func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Equal(t, unixSocket, defaultSocket, "Expected default socket to match common socket location")
|
assert.Equal(t, unixSocket, defaultSocket, "Expected default socket to match common socket location")
|
||||||
assert.Equal(t, true, found, "Expected default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.True(t, found, "Expected default socket to be found")
|
||||||
assert.Nil(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Expected no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{unixSocket, unixSocket}, ret, "Expected to match default socket location")
|
assert.Equal(t, SocketAndHost{unixSocket, unixSocket}, ret, "Expected to match default socket location")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
mySocket := "/my/socket/path.sock"
|
mySocket := "/my/socket/path.sock"
|
||||||
CommonSocketLocations = []string{"/unusual", "/socket", "/location"}
|
CommonSocketLocations = []string{"/unusual", "/socket", "/location"}
|
||||||
@@ -128,7 +140,7 @@ func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
|||||||
ret, err := GetSocketAndHost(mySocket)
|
ret, err := GetSocketAndHost(mySocket)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert.Equal(t, false, found, "Expected no default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.False(t, found, "Expected no default socket to be found")
|
||||||
assert.Equal(t, "", defaultSocket, "Expected no default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, "", defaultSocket, "Expected no default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, SocketAndHost{}, ret, "Expected to match default socket location")
|
assert.Equal(t, SocketAndHost{}, ret, "Expected to match default socket location")
|
||||||
assert.Error(t, err, "Expected an error in invalid state")
|
assert.Error(t, err, "Expected an error in invalid state")
|
||||||
@@ -136,6 +148,7 @@ func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) {
|
func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
socketURI := "unix:///path/to/my.socket"
|
socketURI := "unix:///path/to/my.socket"
|
||||||
CommonSocketLocations = []string{"/unusual", "/location"}
|
CommonSocketLocations = []string{"/unusual", "/location"}
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
@@ -147,8 +160,8 @@ func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) {
|
|||||||
// Assert
|
// Assert
|
||||||
// Default socket locations
|
// Default socket locations
|
||||||
assert.Equal(t, "", defaultSocket, "Expect default socket location to be empty") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, "", defaultSocket, "Expect default socket location to be empty") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, false, found, "Expected no default socket to be found") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.False(t, found, "Expected no default socket to be found")
|
||||||
// Sane default
|
// Sane default
|
||||||
assert.Nil(t, err, "Expect no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Expect no error from GetSocketAndHost") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, socketURI, ret.Host, "Expect host to default to unusual socket")
|
assert.Equal(t, socketURI, ret.Host, "Expect host to default to unusual socket")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ package container
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/moby/moby/api/types/system"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,8 +52,8 @@ func RunnerArch(ctx context.Context) string {
|
|||||||
return runtime.GOOS
|
return runtime.GOOS
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHostInfo(ctx context.Context) (info types.Info, err error) {
|
func GetHostInfo(ctx context.Context) (info system.Info, err error) {
|
||||||
return types.Info{}, nil
|
return system.Info{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
|
func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
|
||||||
@@ -61,7 +62,7 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerNetworkCreateExecutor(name string) common.Executor {
|
func NewDockerNetworkCreateExecutor(name string, opts NewDockerNetworkCreateExecutorInput) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -72,3 +73,7 @@ func NewDockerNetworkRemoveExecutor(name string) common.Executor {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveOrphanNetworks(ctx context.Context, runnerUUID string, createdBefore time.Time) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@ package container
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/moby/moby/client"
|
||||||
"github.com/docker/docker/api/types/volume"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executor {
|
func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executor {
|
||||||
@@ -23,12 +22,12 @@ func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executo
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
list, err := cli.VolumeList(ctx, volume.ListOptions{Filters: filters.NewArgs()})
|
list, err := cli.VolumeList(ctx, client.VolumeListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, vol := range list.Volumes {
|
for _, vol := range list.Items {
|
||||||
if vol.Name == volumeName {
|
if vol.Name == volumeName {
|
||||||
return removeExecutor(volumeName, force)(ctx)
|
return removeExecutor(volumeName, force)(ctx)
|
||||||
}
|
}
|
||||||
@@ -42,7 +41,7 @@ func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executo
|
|||||||
func removeExecutor(volume string, force bool) common.Executor {
|
func removeExecutor(volume string, force bool) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
logger.Debugf("%sdocker volume rm %s", logPrefix, volume)
|
logger.Debugf("docker volume rm %s", volume)
|
||||||
|
|
||||||
if common.Dryrun(ctx) {
|
if common.Dryrun(ctx) {
|
||||||
return nil
|
return nil
|
||||||
@@ -54,6 +53,7 @@ func removeExecutor(volume string, force bool) common.Executor {
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
return cli.VolumeRemove(ctx, volume, force)
|
_, err = cli.VolumeRemove(ctx, volume, client.VolumeRemoveOptions{Force: force})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
act/container/helpers_test.go
Normal file
27
act/container/helpers_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
mobyclient "github.com/moby/moby/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// requireDocker skips the test unless a reachable docker daemon is available.
|
||||||
|
// GetDockerClient succeeds even without a running daemon (its ping is best-effort),
|
||||||
|
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
||||||
|
func requireDocker(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
ctx := context.Background()
|
||||||
|
cli, err := GetDockerClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("skipping: docker client unavailable: %v", err)
|
||||||
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
if _, err := cli.Ping(ctx, mobyclient.PingOptions{}); err != nil {
|
||||||
|
t.Skipf("skipping: docker daemon unreachable: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,11 +17,14 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/filecollector"
|
"gitea.com/gitea/runner/act/filecollector"
|
||||||
"gitea.com/gitea/act_runner/act/lookpath"
|
"gitea.com/gitea/runner/act/lookpath"
|
||||||
|
"gitea.com/gitea/runner/internal/pkg/process"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5/helper/polyfill"
|
"github.com/go-git/go-billy/v5/helper/polyfill"
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
"github.com/go-git/go-billy/v5/osfs"
|
||||||
@@ -34,9 +37,32 @@ type HostEnvironment struct {
|
|||||||
TmpDir string
|
TmpDir string
|
||||||
ToolCache string
|
ToolCache string
|
||||||
Workdir string
|
Workdir string
|
||||||
ActPath string
|
// CleanWorkdir means teardown owns Workdir and may delete it. Leave false
|
||||||
CleanUp func()
|
// when Workdir points at a caller-owned checkout (e.g. `act` local mode).
|
||||||
StdOut io.Writer
|
CleanWorkdir bool
|
||||||
|
ActPath string
|
||||||
|
CleanUp func()
|
||||||
|
StdOut io.Writer
|
||||||
|
AllocatePTY bool // allocate a pseudo-TTY for each step's process
|
||||||
|
|
||||||
|
// procGroup owns every process the job's steps start. Atomic: Remove may read
|
||||||
|
// it while a step is still starting.
|
||||||
|
procGroupOnce sync.Once
|
||||||
|
procGroup atomic.Pointer[process.Group]
|
||||||
|
}
|
||||||
|
|
||||||
|
// processGroup returns the job-scoped process group, creating it on first use.
|
||||||
|
// Returns nil if the job object could not be created; Group is nil-safe.
|
||||||
|
func (e *HostEnvironment) processGroup(ctx context.Context) *process.Group {
|
||||||
|
e.procGroupOnce.Do(func() {
|
||||||
|
group, err := process.NewGroup()
|
||||||
|
if err != nil {
|
||||||
|
common.Logger(ctx).Warnf("could not create the job's process group; processes a step leaves behind can only be reclaimed by the workspace scan: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.procGroup.Store(group)
|
||||||
|
})
|
||||||
|
return e.procGroup.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *HostEnvironment) Create(_, _ []string) common.Executor {
|
func (e *HostEnvironment) Create(_, _ []string) common.Executor {
|
||||||
@@ -192,12 +218,12 @@ func (e *HostEnvironment) Start(_ bool) common.Executor {
|
|||||||
|
|
||||||
type ptyWriter struct {
|
type ptyWriter struct {
|
||||||
Out io.Writer
|
Out io.Writer
|
||||||
AutoStop bool
|
AutoStop atomic.Bool
|
||||||
dirtyLine bool
|
dirtyLine bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *ptyWriter) Write(buf []byte) (int, error) {
|
func (w *ptyWriter) Write(buf []byte) (int, error) {
|
||||||
if w.AutoStop && len(buf) > 0 && buf[len(buf)-1] == 4 {
|
if w.AutoStop.Load() && len(buf) > 0 && buf[len(buf)-1] == 4 {
|
||||||
n, err := w.Out.Write(buf[:len(buf)-1])
|
n, err := w.Out.Write(buf[:len(buf)-1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
@@ -256,7 +282,7 @@ func setupPty(cmd *exec.Cmd, cmdline string) (*os.File, *os.File, error) {
|
|||||||
cmd.Stdin = tty
|
cmd.Stdin = tty
|
||||||
cmd.Stdout = tty
|
cmd.Stdout = tty
|
||||||
cmd.Stderr = tty
|
cmd.Stderr = tty
|
||||||
cmd.SysProcAttr = getSysProcAttr(cmdline, true)
|
cmd.SysProcAttr = process.SysProcAttr(cmdline, true)
|
||||||
return ppty, tty, nil
|
return ppty, tty, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,6 +330,10 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
|
|||||||
} else {
|
} else {
|
||||||
wd = e.Path
|
wd = e.Path
|
||||||
}
|
}
|
||||||
|
// Flush any buffered, not-yet-newline-terminated trailing line, as the docker backend
|
||||||
|
// does in waitForCommand, so the final line of a command's output is not lost.
|
||||||
|
defer common.FlushWriter(e.StdOut)
|
||||||
|
|
||||||
f, err := lookupPathHost(command[0], env, e.StdOut)
|
f, err := lookupPathHost(command[0], env, e.StdOut)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -316,7 +346,12 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
|
|||||||
cmd.Env = envList
|
cmd.Env = envList
|
||||||
cmd.Stderr = e.StdOut
|
cmd.Stderr = e.StdOut
|
||||||
cmd.Dir = wd
|
cmd.Dir = wd
|
||||||
cmd.SysProcAttr = getSysProcAttr(cmdline, false)
|
cmd.SysProcAttr = process.SysProcAttr(cmdline, false)
|
||||||
|
|
||||||
|
// Kills the step's whole tree on cancellation and bounds the post-exit I/O
|
||||||
|
// wait, so an orphan holding cmd's stdout pipe cannot hang cmd.Wait().
|
||||||
|
treeKill := process.NewTreeKill(cmd)
|
||||||
|
|
||||||
var ppty *os.File
|
var ppty *os.File
|
||||||
var tty *os.File
|
var tty *os.File
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -327,36 +362,49 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
|
|||||||
tty.Close()
|
tty.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if true /* allocate Terminal */ {
|
if e.AllocatePTY {
|
||||||
var err error
|
var err error
|
||||||
ppty, tty, err = setupPty(cmd, cmdline)
|
ppty, tty, err = setupPty(cmd, cmdline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.Logger(ctx).Debugf("Failed to setup Pty %v\n", err.Error())
|
common.Logger(ctx).Debugf("Failed to setup Pty %v\n", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer := &ptyWriter{Out: e.StdOut}
|
var writer *ptyWriter
|
||||||
logctx, finishLog := context.WithCancel(context.Background())
|
var logctx context.Context
|
||||||
if ppty != nil {
|
if ppty != nil {
|
||||||
|
writer = &ptyWriter{Out: e.StdOut}
|
||||||
|
var finishLog context.CancelFunc
|
||||||
|
logctx, finishLog = context.WithCancel(context.Background())
|
||||||
go copyPtyOutput(writer, ppty, finishLog)
|
go copyPtyOutput(writer, ppty, finishLog)
|
||||||
} else {
|
|
||||||
finishLog()
|
|
||||||
}
|
|
||||||
if ppty != nil {
|
|
||||||
go writeKeepAlive(ppty)
|
go writeKeepAlive(ppty)
|
||||||
}
|
}
|
||||||
err = cmd.Run()
|
if err := cmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Assign before the step's Killer so the step's job nests inside the group's;
|
||||||
|
// cancellation still scopes to this step's tree.
|
||||||
|
if err := e.processGroup(ctx).Assign(cmd.Process); err != nil {
|
||||||
|
common.Logger(ctx).Warnf("could not assign the step's process to the job's process group; a process it leaves behind may outlive the job: %v", err)
|
||||||
|
}
|
||||||
|
if k, kerr := treeKill.Capture(cmd.Process); kerr != nil {
|
||||||
|
common.Logger(ctx).Warnf("process tree kill setup failed, falling back to single-process kill: %v", kerr)
|
||||||
|
} else {
|
||||||
|
defer k.Close()
|
||||||
|
}
|
||||||
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var exitErr *exec.ExitError
|
||||||
|
if errors.As(err, &exitErr) {
|
||||||
|
return ExitCodeError(exitErr.ExitCode())
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if tty != nil {
|
if tty != nil {
|
||||||
writer.AutoStop = true
|
writer.AutoStop.Store(true)
|
||||||
if _, err := tty.WriteString("\x04"); err != nil {
|
if _, err := tty.WriteString("\x04"); err != nil {
|
||||||
common.Logger(ctx).Debug("Failed to write EOT")
|
common.Logger(ctx).Debug("Failed to write EOT")
|
||||||
}
|
}
|
||||||
}
|
<-logctx.Done()
|
||||||
<-logctx.Done()
|
|
||||||
|
|
||||||
if ppty != nil {
|
|
||||||
ppty.Close()
|
ppty.Close()
|
||||||
ppty = nil
|
ppty = nil
|
||||||
}
|
}
|
||||||
@@ -385,12 +433,201 @@ func (e *HostEnvironment) UpdateFromEnv(srcPath string, env *map[string]string)
|
|||||||
return parseEnvFile(e, srcPath, env)
|
return parseEnvFile(e, srcPath, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeAll is a var so tests can substitute a blocking stub.
|
||||||
|
var removeAll = os.RemoveAll
|
||||||
|
|
||||||
|
// removeAllWithContext returns once the delete finishes or ctx is cancelled. On
|
||||||
|
// cancellation the goroutine leaks: a delete inside a syscall cannot be
|
||||||
|
// interrupted (see runWithTimeout).
|
||||||
|
func removeAllWithContext(ctx context.Context, path string) error {
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- removeAll(path) }()
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
return err
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removePathWithRetry(ctx context.Context, path string) error {
|
||||||
|
if path == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
attempts := 1
|
||||||
|
delay := time.Duration(0)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
attempts = 5
|
||||||
|
delay = 200 * time.Millisecond
|
||||||
|
}
|
||||||
|
var lastErr error
|
||||||
|
for i := 0; i < attempts; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-time.After(delay):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastErr = removeAllWithContext(ctx, path)
|
||||||
|
if lastErr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if errors.Is(lastErr, context.DeadlineExceeded) {
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildWindowsWorkspaceKillScript builds a PowerShell command that taskkills
|
||||||
|
// every process tree whose ExecutablePath or CommandLine references one of the
|
||||||
|
// given workspace dirs, releasing file handles for cleanup. Win32_Process
|
||||||
|
// exposes both fields (Get-Process doesn't, wmic is deprecated); matching is on
|
||||||
|
// the dir+separator prefix via ordinal String methods, so a name-prefix sibling
|
||||||
|
// (job1 vs job10) is spared and path metacharacters stay literal.
|
||||||
|
func buildWindowsWorkspaceKillScript(dirs []string) string {
|
||||||
|
quoted := make([]string, len(dirs))
|
||||||
|
for i, d := range dirs {
|
||||||
|
// Single-quoted PowerShell literal; escape ' by doubling it.
|
||||||
|
quoted[i] = "'" + strings.ReplaceAll(d, "'", "''") + "'"
|
||||||
|
}
|
||||||
|
|
||||||
|
return `$paths = @(` + strings.Join(quoted, ",") + `)
|
||||||
|
$selfPid = $PID
|
||||||
|
Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {
|
||||||
|
if ($_.ProcessId -eq $selfPid) { return $false }
|
||||||
|
foreach ($p in $paths) {
|
||||||
|
$prefix = $p + '\'
|
||||||
|
if ($_.ExecutablePath -and $_.ExecutablePath.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) { return $true }
|
||||||
|
if ($_.CommandLine -and $_.CommandLine.IndexOf($prefix, [System.StringComparison]::OrdinalIgnoreCase) -ge 0) { return $true }
|
||||||
|
}
|
||||||
|
return $false
|
||||||
|
} | ForEach-Object {
|
||||||
|
& taskkill.exe /PID $_.ProcessId /T /F 2>$null | Out-Null
|
||||||
|
}
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *HostEnvironment) terminateRunningProcesses(ctx context.Context) {
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detached: exec.CommandContext won't start on a cancelled ctx, and a
|
||||||
|
// server cancel has already cancelled the parent ctx.
|
||||||
|
killCtx, killCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer killCancel()
|
||||||
|
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
|
// Dirs we own; a process referencing one is a leftover. ToolCache is shared
|
||||||
|
// across jobs, and Workdir may be a caller-owned checkout.
|
||||||
|
owned := []string{e.Path, e.TmpDir}
|
||||||
|
if e.CleanWorkdir {
|
||||||
|
owned = append(owned, e.Workdir)
|
||||||
|
}
|
||||||
|
dirs := make([]string, 0, len(owned))
|
||||||
|
for _, d := range owned {
|
||||||
|
if d == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
abs, err := filepath.Abs(d)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dirs = append(dirs, abs)
|
||||||
|
}
|
||||||
|
if len(dirs) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
script := buildWindowsWorkspaceKillScript(dirs)
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(killCtx, "powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
logger.Debugf("workspace process-tree kill via PowerShell failed: %v output=%s", err, strings.TrimSpace(string(out)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Win32_Process exposes no working directory, so the scan above misses a
|
||||||
|
// process that merely runs in a workspace dir while pinning a handle on it.
|
||||||
|
if killed, err := process.KillProcessesWithCWDUnder(killCtx, dirs); err != nil {
|
||||||
|
logger.Debugf("workspace process kill by working directory reported errors: %v", err)
|
||||||
|
} else if killed > 0 {
|
||||||
|
logger.Debugf("terminated %d leftover process(es) by workspace working directory", killed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hostCleanupTimeout bounds each teardown phase so one stalled delete cannot
|
||||||
|
// wedge the runner slot. A var so tests can shrink it.
|
||||||
|
var hostCleanupTimeout = 30 * time.Second
|
||||||
|
|
||||||
|
// runWithTimeout returns context.DeadlineExceeded once timeout elapses, leaking
|
||||||
|
// the goroutine: a delete blocked in a syscall (AV filter driver, dead network
|
||||||
|
// mount) cannot be interrupted, and leaking scratch state beats losing the
|
||||||
|
// runner's capacity slot forever. The idle stale-dir sweep reclaims it later.
|
||||||
|
func runWithTimeout(fn func(), timeout time.Duration) error {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
fn()
|
||||||
|
}()
|
||||||
|
timer := time.NewTimer(timeout)
|
||||||
|
defer timer.Stop()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return nil
|
||||||
|
case <-timer.C:
|
||||||
|
return context.DeadlineExceeded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (e *HostEnvironment) Remove() common.Executor {
|
func (e *HostEnvironment) Remove() common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if e.CleanUp != nil {
|
logger := common.Logger(ctx)
|
||||||
e.CleanUp()
|
|
||||||
|
// End lingering processes before removing the workspace; on Windows their
|
||||||
|
// file locks block cleanup. Closing the group is deterministic, the scan a net.
|
||||||
|
if err := e.procGroup.Load().Close(); err != nil {
|
||||||
|
logger.Debugf("closing the job's process group failed: %v", err)
|
||||||
}
|
}
|
||||||
return os.RemoveAll(e.Path)
|
e.terminateRunningProcesses(ctx)
|
||||||
|
|
||||||
|
// Removes per-job misc state only, never the toolcache root. Bounded because
|
||||||
|
// CleanUp is a caller-supplied, typically unbounded os.RemoveAll.
|
||||||
|
if e.CleanUp != nil {
|
||||||
|
logger.Debugf("running host environment cleanup callback")
|
||||||
|
if err := runWithTimeout(e.CleanUp, hostCleanupTimeout); err != nil {
|
||||||
|
logger.Warnf("host environment cleanup did not finish within %s; continuing job completion, scratch state may be leaked and is reclaimed by the idle stale-dir sweep", hostCleanupTimeout)
|
||||||
|
} else {
|
||||||
|
logger.Debugf("host environment cleanup callback finished")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detach: a cancelled ctx would skip removePathWithRetry's retries,
|
||||||
|
// which absorb Windows file-handle release lag after the kill above.
|
||||||
|
rmCtx, rmCancel := context.WithTimeout(context.Background(), hostCleanupTimeout)
|
||||||
|
defer rmCancel()
|
||||||
|
|
||||||
|
var errs []error
|
||||||
|
if err := removePathWithRetry(rmCtx, e.Path); err != nil {
|
||||||
|
logger.Warnf("failed to remove host misc state %s: %v", e.Path, err)
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
if e.CleanWorkdir {
|
||||||
|
if err := removePathWithRetry(rmCtx, e.Workdir); err != nil {
|
||||||
|
logger.Warnf("failed to remove host workspace %s: %v", e.Workdir, err)
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, err := range errs {
|
||||||
|
if !errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Teardown timed out; warned above. Do not fail job completion over it.
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,22 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Type assert HostEnvironment implements ExecutionsEnvironment
|
// Type assert HostEnvironment implements ExecutionsEnvironment
|
||||||
@@ -69,3 +77,287 @@ func TestGetContainerArchive(t *testing.T) {
|
|||||||
_, err = reader.Next()
|
_, err = reader.Next()
|
||||||
assert.ErrorIs(t, err, io.EOF)
|
assert.ErrorIs(t, err, io.EOF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHostEnvironmentExecExitCode(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("uses POSIX shell")
|
||||||
|
}
|
||||||
|
dir := t.TempDir()
|
||||||
|
ctx := context.Background()
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: filepath.Join(dir, "path"),
|
||||||
|
TmpDir: filepath.Join(dir, "tmp"),
|
||||||
|
ToolCache: filepath.Join(dir, "tool_cache"),
|
||||||
|
ActPath: filepath.Join(dir, "act_path"),
|
||||||
|
StdOut: io.Discard,
|
||||||
|
Workdir: filepath.Join(dir, "path"),
|
||||||
|
}
|
||||||
|
for _, p := range []string{e.Path, e.TmpDir, e.ToolCache, e.ActPath} {
|
||||||
|
assert.NoError(t, os.MkdirAll(p, 0o700)) //nolint:testifylint // test setup
|
||||||
|
}
|
||||||
|
|
||||||
|
err := e.Exec([]string{"sh", "-c", "exit 3"}, map[string]string{"PATH": os.Getenv("PATH")}, "", "")(ctx)
|
||||||
|
var exitErr ExitCodeError
|
||||||
|
require.ErrorAs(t, err, &exitErr)
|
||||||
|
assert.Equal(t, ExitCodeError(3), exitErr)
|
||||||
|
assert.Equal(t, "Process completed with exit code 3.", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostEnvironmentAllocatePTY(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("uses POSIX shell")
|
||||||
|
}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
allocPTY bool
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{name: "off", allocPTY: false, expect: "NOTTY"},
|
||||||
|
{name: "on", allocPTY: true, expect: "TTY"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: filepath.Join(dir, "path"),
|
||||||
|
TmpDir: filepath.Join(dir, "tmp"),
|
||||||
|
ToolCache: filepath.Join(dir, "tool_cache"),
|
||||||
|
ActPath: filepath.Join(dir, "act_path"),
|
||||||
|
StdOut: buf,
|
||||||
|
Workdir: filepath.Join(dir, "path"),
|
||||||
|
AllocatePTY: tc.allocPTY,
|
||||||
|
}
|
||||||
|
for _, p := range []string{e.Path, e.TmpDir, e.ToolCache, e.ActPath} {
|
||||||
|
require.NoError(t, os.MkdirAll(p, 0o700))
|
||||||
|
}
|
||||||
|
|
||||||
|
err := e.Exec(
|
||||||
|
[]string{"sh", "-c", "[ -t 1 ] && printf TTY || printf NOTTY"},
|
||||||
|
map[string]string{"PATH": os.Getenv("PATH")}, "", "",
|
||||||
|
)(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
got := strings.TrimSpace(strings.ReplaceAll(buf.String(), "\r", ""))
|
||||||
|
assert.Equal(t, tc.expect, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostEnvironmentRemovePreservesWorkdirByDefault(t *testing.T) {
|
||||||
|
logger := logrus.New()
|
||||||
|
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
||||||
|
base := t.TempDir()
|
||||||
|
miscRoot := filepath.Join(base, "misc")
|
||||||
|
path := filepath.Join(miscRoot, "hostexecutor")
|
||||||
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
|
workdir := filepath.Join(base, "workspace", "owner", "repo")
|
||||||
|
require.NoError(t, os.MkdirAll(workdir, 0o700))
|
||||||
|
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: path,
|
||||||
|
Workdir: workdir,
|
||||||
|
CleanUp: func() {
|
||||||
|
_ = os.RemoveAll(miscRoot)
|
||||||
|
},
|
||||||
|
StdOut: os.Stdout,
|
||||||
|
}
|
||||||
|
require.NoError(t, e.Remove()(ctx))
|
||||||
|
_, err := os.Stat(workdir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostEnvironmentRemoveCleansWorkdirWhenOwned(t *testing.T) {
|
||||||
|
logger := logrus.New()
|
||||||
|
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
||||||
|
base := t.TempDir()
|
||||||
|
miscRoot := filepath.Join(base, "misc")
|
||||||
|
path := filepath.Join(miscRoot, "hostexecutor")
|
||||||
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
|
workdir := filepath.Join(base, "workspace", "123", "owner", "repo")
|
||||||
|
require.NoError(t, os.MkdirAll(workdir, 0o700))
|
||||||
|
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: path,
|
||||||
|
Workdir: workdir,
|
||||||
|
CleanWorkdir: true,
|
||||||
|
CleanUp: func() {
|
||||||
|
_ = os.RemoveAll(miscRoot)
|
||||||
|
},
|
||||||
|
StdOut: os.Stdout,
|
||||||
|
}
|
||||||
|
require.NoError(t, e.Remove()(ctx))
|
||||||
|
_, err := os.Stat(workdir)
|
||||||
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
|
||||||
|
release := make(chan struct{})
|
||||||
|
stubDone := make(chan struct{})
|
||||||
|
|
||||||
|
orig := removeAll
|
||||||
|
removeAll = func(string) error {
|
||||||
|
defer close(stubDone)
|
||||||
|
<-release
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// removeAllWithContext intentionally leaks the delete goroutine on timeout,
|
||||||
|
// and that goroutine still references removeAll. Unblock it and wait for it
|
||||||
|
// to return before restoring the var, so the restore can't race the read.
|
||||||
|
t.Cleanup(func() {
|
||||||
|
close(release)
|
||||||
|
<-stubDone
|
||||||
|
removeAll = orig
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
err := removeAllWithContext(ctx, t.TempDir())
|
||||||
|
require.ErrorIs(t, err, context.DeadlineExceeded)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHostEnvironmentRemoveDoesNotHangOnStuckCleanUp guards against a stalled
|
||||||
|
// CleanUp callback (e.g. an os.RemoveAll blocked by an AV/EDR filter driver or
|
||||||
|
// an unresponsive mount) wedging the runner slot forever at "Cleaning up
|
||||||
|
// container". Remove must time out the callback and complete job teardown.
|
||||||
|
func TestHostEnvironmentRemoveDoesNotHangOnStuckCleanUp(t *testing.T) {
|
||||||
|
// Keep the suite fast: shrink the per-phase teardown timeout for this test.
|
||||||
|
orig := hostCleanupTimeout
|
||||||
|
hostCleanupTimeout = 100 * time.Millisecond
|
||||||
|
t.Cleanup(func() { hostCleanupTimeout = orig })
|
||||||
|
|
||||||
|
logger := logrus.New()
|
||||||
|
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
||||||
|
base := t.TempDir()
|
||||||
|
path := filepath.Join(base, "misc", "hostexecutor")
|
||||||
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
|
|
||||||
|
release := make(chan struct{})
|
||||||
|
t.Cleanup(func() { close(release) }) // unblock the leaked goroutine at test end
|
||||||
|
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: path,
|
||||||
|
CleanUp: func() {
|
||||||
|
<-release // simulate a delete syscall stuck indefinitely
|
||||||
|
},
|
||||||
|
StdOut: os.Stdout,
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- e.Remove()(ctx) }()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
require.NoError(t, err)
|
||||||
|
case <-time.After(10 * time.Second):
|
||||||
|
t.Fatal("Remove() hung on a stuck CleanUp callback")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHostEnvironmentRemoveDoesNotHangOnStuckPathRemoval guards against a
|
||||||
|
// stalled os.RemoveAll on the misc/workspace paths (same AV/EDR wedge as
|
||||||
|
// #1023) wedging job completion after the CleanUp callback has already timed
|
||||||
|
// out or finished.
|
||||||
|
func TestHostEnvironmentRemoveDoesNotHangOnStuckPathRemoval(t *testing.T) {
|
||||||
|
origTimeout := hostCleanupTimeout
|
||||||
|
hostCleanupTimeout = 100 * time.Millisecond
|
||||||
|
t.Cleanup(func() { hostCleanupTimeout = origTimeout })
|
||||||
|
|
||||||
|
release := make(chan struct{})
|
||||||
|
stubDone := make(chan struct{})
|
||||||
|
|
||||||
|
origRemoveAll := removeAll
|
||||||
|
removeAll = func(string) error {
|
||||||
|
defer close(stubDone)
|
||||||
|
<-release
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// The stuck delete goroutine outlives the timed-out Remove and still reads
|
||||||
|
// removeAll; unblock it and wait before restoring to avoid a restore/read race.
|
||||||
|
t.Cleanup(func() {
|
||||||
|
close(release)
|
||||||
|
<-stubDone
|
||||||
|
removeAll = origRemoveAll
|
||||||
|
})
|
||||||
|
|
||||||
|
logger := logrus.New()
|
||||||
|
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
||||||
|
base := t.TempDir()
|
||||||
|
path := filepath.Join(base, "misc", "hostexecutor")
|
||||||
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
|
|
||||||
|
e := &HostEnvironment{
|
||||||
|
Path: path,
|
||||||
|
StdOut: os.Stdout,
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- e.Remove()(ctx) }()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
require.NoError(t, err)
|
||||||
|
case <-time.After(10 * time.Second):
|
||||||
|
t.Fatal("Remove() hung on a stuck path removal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildWindowsWorkspaceKillScript(t *testing.T) {
|
||||||
|
t.Run("single dir", func(t *testing.T) {
|
||||||
|
s := buildWindowsWorkspaceKillScript([]string{`C:\workspace\job1`})
|
||||||
|
assert.Contains(t, s, `$paths = @('C:\workspace\job1')`)
|
||||||
|
// Self-PID guard is essential — without it the script could taskkill
|
||||||
|
// the PowerShell process running it.
|
||||||
|
assert.Contains(t, s, "$selfPid = $PID")
|
||||||
|
assert.Contains(t, s, "$_.ProcessId -eq $selfPid")
|
||||||
|
// Must match both ExecutablePath (binaries from the workspace) and
|
||||||
|
// CommandLine (system binaries invoked with workspace paths in args),
|
||||||
|
// both bounded by dir+separator so a name-prefix sibling is spared.
|
||||||
|
assert.Contains(t, s, `$prefix = $p + '\'`)
|
||||||
|
assert.Contains(t, s, "$_.ExecutablePath.StartsWith($prefix")
|
||||||
|
assert.Contains(t, s, "$_.CommandLine.IndexOf($prefix")
|
||||||
|
// Each matched PID must be tree-killed, not just stopped.
|
||||||
|
assert.Contains(t, s, "taskkill.exe /PID $_.ProcessId /T /F")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("multiple dirs comma-separated", func(t *testing.T) {
|
||||||
|
s := buildWindowsWorkspaceKillScript([]string{
|
||||||
|
`C:\work\path`,
|
||||||
|
`C:\work\workdir`,
|
||||||
|
`C:\Users\runner\AppData\Local\Temp\job-42`,
|
||||||
|
})
|
||||||
|
assert.Contains(t, s, `'C:\work\path'`)
|
||||||
|
assert.Contains(t, s, `'C:\work\workdir'`)
|
||||||
|
assert.Contains(t, s, `'C:\Users\runner\AppData\Local\Temp\job-42'`)
|
||||||
|
// Commas between entries — no trailing comma, no leading comma.
|
||||||
|
assert.Contains(t, s, `'C:\work\path','C:\work\workdir',`)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("path with single quote is escaped", func(t *testing.T) {
|
||||||
|
// In PowerShell single-quoted strings the only special char is the
|
||||||
|
// quote itself, escaped by doubling. A workspace path that ever
|
||||||
|
// contained `'` would inject a command into the script otherwise.
|
||||||
|
s := buildWindowsWorkspaceKillScript([]string{`C:\work\it's\path`})
|
||||||
|
assert.Contains(t, s, `'C:\work\it''s\path'`)
|
||||||
|
// And it must NOT appear unescaped — otherwise the quote would
|
||||||
|
// terminate the literal early.
|
||||||
|
assert.NotContains(t, s, `'C:\work\it's\path'`)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("path with wildcard metacharacters is matched literally", func(t *testing.T) {
|
||||||
|
// A path containing [ ] ? * must be embedded verbatim and matched with
|
||||||
|
// ordinal String methods, not -like, otherwise the metacharacters would
|
||||||
|
// be interpreted as wildcards and the leftover process could escape.
|
||||||
|
s := buildWindowsWorkspaceKillScript([]string{`C:\work\[job]?1`})
|
||||||
|
assert.Contains(t, s, `'C:\work\[job]?1'`)
|
||||||
|
assert.NotContains(t, s, "-like")
|
||||||
|
assert.Contains(t, s, "StartsWith")
|
||||||
|
assert.Contains(t, s, "IndexOf")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty dir list still produces a valid script", func(t *testing.T) {
|
||||||
|
s := buildWindowsWorkspaceKillScript(nil)
|
||||||
|
// Empty array literal — script runs, matches nothing, is a no-op.
|
||||||
|
assert.Contains(t, s, "$paths = @()")
|
||||||
|
assert.Contains(t, s, "Get-CimInstance Win32_Process")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,12 +66,15 @@ func (*LinuxContainerEnvironmentExtensions) JoinPathVariable(paths ...string) st
|
|||||||
return strings.Join(paths, ":")
|
return strings.Join(paths, ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultToolCache is where the runner mounts the tool cache inside job containers.
|
||||||
|
const DefaultToolCache = "/opt/hostedtoolcache"
|
||||||
|
|
||||||
func (*LinuxContainerEnvironmentExtensions) GetRunnerContext(ctx context.Context) map[string]any {
|
func (*LinuxContainerEnvironmentExtensions) GetRunnerContext(ctx context.Context) map[string]any {
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"os": "Linux",
|
"os": "Linux",
|
||||||
"arch": RunnerArch(ctx),
|
"arch": RunnerArch(ctx),
|
||||||
"temp": "/tmp",
|
"temp": "/tmp",
|
||||||
"tool_cache": "/opt/hostedtoolcache",
|
"tool_cache": DefaultToolCache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding/unicode"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Executor {
|
func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Executor {
|
||||||
@@ -28,9 +31,19 @@ func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Ex
|
|||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s := bufio.NewScanner(reader)
|
// Decode by BOM: Windows PowerShell 5.1 redirection writes UTF-16, and some
|
||||||
|
// tools emit a UTF-8 BOM. Without a BOM the file is read as UTF-8, as before.
|
||||||
|
decoded := transform.NewReader(reader, unicode.BOMOverride(unicode.UTF8.NewDecoder()))
|
||||||
|
|
||||||
|
s := bufio.NewScanner(decoded)
|
||||||
|
// Default 64 KiB max token size is too small for realistic env-file lines; allow up to 16 MiB.
|
||||||
|
s.Buffer(make([]byte, 0, 64*1024), 16*1024*1024)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
line := s.Text()
|
line := s.Text()
|
||||||
|
// GitHub's runner ignores blank lines
|
||||||
|
if strings.TrimSpace(line) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
singleLineEnv := strings.Index(line, "=")
|
singleLineEnv := strings.Index(line, "=")
|
||||||
multiLineEnv := strings.Index(line, "<<")
|
multiLineEnv := strings.Index(line, "<<")
|
||||||
if singleLineEnv != -1 && (multiLineEnv == -1 || singleLineEnv < multiLineEnv) {
|
if singleLineEnv != -1 && (multiLineEnv == -1 || singleLineEnv < multiLineEnv) {
|
||||||
@@ -50,6 +63,9 @@ func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Ex
|
|||||||
}
|
}
|
||||||
multiLineEnvContent += content
|
multiLineEnvContent += content
|
||||||
}
|
}
|
||||||
|
if err := s.Err(); err != nil {
|
||||||
|
return fmt.Errorf("reading env file: %w", err)
|
||||||
|
}
|
||||||
if !delimiterFound {
|
if !delimiterFound {
|
||||||
return fmt.Errorf("invalid format delimiter '%v' not found before end of file", multiLineEnvDelimiter)
|
return fmt.Errorf("invalid format delimiter '%v' not found before end of file", multiLineEnvDelimiter)
|
||||||
}
|
}
|
||||||
@@ -58,6 +74,9 @@ func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Ex
|
|||||||
return fmt.Errorf("invalid format '%v', expected a line with '=' or '<<'", line)
|
return fmt.Errorf("invalid format '%v', expected a line with '=' or '<<'", line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err := s.Err(); err != nil {
|
||||||
|
return fmt.Errorf("reading env file: %w", err)
|
||||||
|
}
|
||||||
env = &localEnv
|
env = &localEnv
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
134
act/container/parse_env_file_test.go
Normal file
134
act/container/parse_env_file_test.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestHostEnv(t *testing.T) (*HostEnvironment, string) {
|
||||||
|
t.Helper()
|
||||||
|
e := &HostEnvironment{Path: t.TempDir()}
|
||||||
|
return e, filepath.Join(e.Path, "envfile")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileSingleLine(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO=bar\nBAZ=qux\n"), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
|
assert.Equal(t, "qux", env["BAZ"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileMultiLine(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
content := "FOO<<EOF\nline1\nline2\nEOF\n"
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "line1\nline2", env["FOO"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileLargeValueWithinLimit(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
big := strings.Repeat("x", 2*1024*1024)
|
||||||
|
content := "FOO<<EOF\n" + big + "\nEOF\n"
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, big, env["FOO"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileLineExceedsBufferReportsScannerError(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
tooBig := strings.Repeat("x", 17*1024*1024) // over the 16 MiB cap
|
||||||
|
content := "FOO<<EOF\n" + tooBig + "\nEOF\n"
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
err := parseEnvFile(e, envPath, &env)(context.Background())
|
||||||
|
require.ErrorIs(t, err, bufio.ErrTooLong)
|
||||||
|
assert.Contains(t, err.Error(), "reading env file")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regression test: a blank line used to fail the job at "Complete Job", after
|
||||||
|
// every step had already been recorded as successful.
|
||||||
|
func TestParseEnvFileBlankLines(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte("\nFOO=bar\n\n \nBAZ=qux\n\n"), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
|
assert.Equal(t, "qux", env["BAZ"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// blank lines inside a heredoc value are content, not separators
|
||||||
|
func TestParseEnvFileMultiLineKeepsBlankLines(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\n\nline2\nEOF\n"), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "line1\n\nline2", env["FOO"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileUTF8BOM(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
content := append([]byte{0xEF, 0xBB, 0xBF}, []byte("FOO=bar\n")...)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Windows host mode: PowerShell 5.1 redirection writes UTF-16, which used to be
|
||||||
|
// unrecognisable as KEY=VALUE, so the writes were silently ignored.
|
||||||
|
func TestParseEnvFileUTF16(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
encoder *encoding.Encoder
|
||||||
|
}{
|
||||||
|
{"little endian", unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder()},
|
||||||
|
{"big endian", unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewEncoder()},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
content, err := tt.encoder.Bytes([]byte("FOO=bar\r\nMULTI<<EOF\r\nline1\r\nEOF\r\n"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
||||||
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
|
assert.Equal(t, "line1", env["MULTI"])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEnvFileMissingDelimiter(t *testing.T) {
|
||||||
|
e, envPath := newTestHostEnv(t)
|
||||||
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\nline2\n"), 0o600))
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
err := parseEnvFile(e, envPath, &env)(context.Background())
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "delimiter")
|
||||||
|
}
|
||||||
@@ -8,23 +8,10 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSysProcAttr(_ string, tty bool) *syscall.SysProcAttr {
|
|
||||||
if tty {
|
|
||||||
return &syscall.SysProcAttr{
|
|
||||||
Setsid: true,
|
|
||||||
Setctty: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &syscall.SysProcAttr{
|
|
||||||
Setpgid: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func openPty() (*os.File, *os.File, error) {
|
func openPty() (*os.File, *os.File, error) {
|
||||||
return pty.Open()
|
return pty.Open()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,8 @@ package container
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSysProcAttr(cmdLine string, tty bool) *syscall.SysProcAttr {
|
|
||||||
return &syscall.SysProcAttr{
|
|
||||||
Setpgid: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func openPty() (*os.File, *os.File, error) {
|
func openPty() (*os.File, *os.File, error) {
|
||||||
return nil, nil, errors.New("Unsupported")
|
return nil, nil, errors.New("Unsupported")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,8 @@ package container
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSysProcAttr(cmdLine string, tty bool) *syscall.SysProcAttr {
|
|
||||||
return &syscall.SysProcAttr{
|
|
||||||
Rfork: syscall.RFNOTEG,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func openPty() (*os.File, *os.File, error) {
|
func openPty() (*os.File, *os.File, error) {
|
||||||
return nil, nil, errors.New("Unsupported")
|
return nil, nil, errors.New("Unsupported")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,8 @@ package container
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSysProcAttr(cmdLine string, tty bool) *syscall.SysProcAttr {
|
|
||||||
return &syscall.SysProcAttr{CmdLine: cmdLine, CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP}
|
|
||||||
}
|
|
||||||
|
|
||||||
func openPty() (*os.File, *os.File, error) {
|
func openPty() (*os.File, *os.File, error) {
|
||||||
return nil, nil, errors.New("Unsupported")
|
return nil, nil, errors.New("Unsupported")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
||||||
"github.com/rhysd/actionlint"
|
"github.com/rhysd/actionlint"
|
||||||
@@ -266,7 +266,7 @@ func (impl *interperterImpl) jobSuccess() (bool, error) { //nolint:unparam // pr
|
|||||||
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
||||||
|
|
||||||
for _, needs := range jobNeeds {
|
for _, needs := range jobNeeds {
|
||||||
if jobs[needs].Result != "success" {
|
if jobs[needs].NeedsResult() != "success" {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,8 +274,17 @@ func (impl *interperterImpl) jobSuccess() (bool, error) { //nolint:unparam // pr
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// jobStatus returns the current job status, treating a nil Job context as an
|
||||||
|
// empty status so status-check functions never panic on a nil dereference.
|
||||||
|
func (impl *interperterImpl) jobStatus() string {
|
||||||
|
if impl.env.Job == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return impl.env.Job.Status
|
||||||
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) stepSuccess() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
func (impl *interperterImpl) stepSuccess() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
return impl.env.Job.Status == "success", nil
|
return impl.jobStatus() == "success", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) jobFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
func (impl *interperterImpl) jobFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
@@ -283,7 +292,7 @@ func (impl *interperterImpl) jobFailure() (bool, error) { //nolint:unparam // pr
|
|||||||
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
||||||
|
|
||||||
for _, needs := range jobNeeds {
|
for _, needs := range jobNeeds {
|
||||||
if jobs[needs].Result == "failure" {
|
if jobs[needs].NeedsResult() == "failure" {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -292,9 +301,9 @@ func (impl *interperterImpl) jobFailure() (bool, error) { //nolint:unparam // pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) stepFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
func (impl *interperterImpl) stepFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
return impl.env.Job.Status == "failure", nil
|
return impl.jobStatus() == "failure", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) cancelled() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
func (impl *interperterImpl) cancelled() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
return impl.env.Job.Status == "cancelled", nil
|
return impl.jobStatus() == "cancelled", nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -43,7 +43,7 @@ func TestFunctionContains(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -72,7 +72,7 @@ func TestFunctionStartsWith(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -101,7 +101,7 @@ func TestFunctionEndsWith(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -128,7 +128,7 @@ func TestFunctionJoin(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -154,7 +154,7 @@ func TestFunctionToJSON(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -177,7 +177,7 @@ func TestFunctionFromJSON(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -205,9 +205,9 @@ func TestFunctionHashFiles(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
workdir, err := filepath.Abs("testdata")
|
workdir, err := filepath.Abs("testdata")
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
output, err := NewInterpeter(env, Config{WorkingDir: workdir}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{WorkingDir: workdir}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -248,9 +248,33 @@ func TestFunctionFormat(t *testing.T) {
|
|||||||
if tt.error != nil {
|
if tt.error != nil {
|
||||||
assert.Equal(t, tt.error, err.Error())
|
assert.Equal(t, tt.error, err.Error())
|
||||||
} else {
|
} else {
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStatusFunctionsNilJob(t *testing.T) {
|
||||||
|
// A nil Job context must not panic: the status-check functions should treat
|
||||||
|
// it as an empty status and return false rather than dereferencing nil.
|
||||||
|
env := &EvaluationEnvironment{}
|
||||||
|
|
||||||
|
table := []struct {
|
||||||
|
input string
|
||||||
|
context string
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{"cancelled()", "job", "cancelled-nil-job"},
|
||||||
|
{"success()", "step", "step-success-nil-job"},
|
||||||
|
{"failure()", "step", "step-failure-nil-job"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range table {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
output, err := NewInterpeter(env, Config{Context: tt.context}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
assert.Equal(t, false, output)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/rhysd/actionlint"
|
"github.com/rhysd/actionlint"
|
||||||
)
|
)
|
||||||
@@ -156,7 +156,6 @@ func (impl *interperterImpl) evaluateNode(exprNode actionlint.ExprNode) (any, er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func (impl *interperterImpl) evaluateVariable(variableNode *actionlint.VariableNode) (any, error) {
|
func (impl *interperterImpl) evaluateVariable(variableNode *actionlint.VariableNode) (any, error) {
|
||||||
switch strings.ToLower(variableNode.Name) {
|
switch strings.ToLower(variableNode.Name) {
|
||||||
case "github":
|
case "github":
|
||||||
@@ -252,7 +251,7 @@ func (impl *interperterImpl) evaluateArrayDeref(arrayDerefNode *actionlint.Array
|
|||||||
|
|
||||||
func (impl *interperterImpl) getPropertyValue(left reflect.Value, property string) (value any, err error) {
|
func (impl *interperterImpl) getPropertyValue(left reflect.Value, property string) (value any, err error) {
|
||||||
switch left.Kind() {
|
switch left.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Pointer:
|
||||||
return impl.getPropertyValue(left.Elem(), property)
|
return impl.getPropertyValue(left.Elem(), property)
|
||||||
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@@ -322,7 +321,7 @@ func (impl *interperterImpl) getPropertyValue(left reflect.Value, property strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) getMapValue(value reflect.Value) (any, error) {
|
func (impl *interperterImpl) getMapValue(value reflect.Value) (any, error) {
|
||||||
if value.Kind() == reflect.Ptr {
|
if value.Kind() == reflect.Pointer {
|
||||||
return impl.getMapValue(value.Elem())
|
return impl.getMapValue(value.Elem())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -584,7 +583,6 @@ func (impl *interperterImpl) evaluateLogicalCompare(compareNode *actionlint.Logi
|
|||||||
return nil, fmt.Errorf("Unable to compare incompatibles types '%s' and '%s'", leftValue.Kind(), rightValue.Kind())
|
return nil, fmt.Errorf("Unable to compare incompatibles types '%s' and '%s'", leftValue.Kind(), rightValue.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func (impl *interperterImpl) evaluateFuncCall(funcCallNode *actionlint.FuncCallNode) (any, error) {
|
func (impl *interperterImpl) evaluateFuncCall(funcCallNode *actionlint.FuncCallNode) (any, error) {
|
||||||
args := make([]reflect.Value, 0)
|
args := make([]reflect.Value, 0)
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLiterals(t *testing.T) {
|
func TestLiterals(t *testing.T) {
|
||||||
@@ -35,7 +36,7 @@ func TestLiterals(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -105,10 +106,10 @@ func TestOperators(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
if tt.error != "" {
|
if tt.error != "" {
|
||||||
assert.NotNil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.error, err.Error())
|
assert.Equal(t, tt.error, err.Error())
|
||||||
} else {
|
} else {
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
@@ -157,7 +158,7 @@ func TestOperatorsCompare(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
@@ -520,10 +521,12 @@ func TestOperatorsBooleanEvaluation(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
if expected, ok := tt.expected.(float64); ok && math.IsNaN(expected) {
|
if expected, ok := tt.expected.(float64); ok && math.IsNaN(expected) {
|
||||||
assert.True(t, math.IsNaN(output.(float64)))
|
number, ok := output.(float64)
|
||||||
|
require.True(t, ok, "want a number, got %T", output)
|
||||||
|
assert.True(t, math.IsNaN(number))
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
}
|
}
|
||||||
@@ -624,7 +627,7 @@ func TestContexts(t *testing.T) {
|
|||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
assert.Equal(t, tt.expected, output)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -73,10 +73,16 @@ func (cc *CopyCollector) WriteFile(fpath string, fi fs.FileInfo, linkName string
|
|||||||
if err := os.MkdirAll(filepath.Dir(fdestpath), 0o777); err != nil {
|
if err := os.MkdirAll(filepath.Dir(fdestpath), 0o777); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Remove any existing destination so we can overwrite read-only files
|
||||||
|
// (e.g. git pack files at mode 0444 trip EACCES on macOS and "Access is
|
||||||
|
// denied" on Windows when reopened with O_WRONLY) and so os.Symlink does
|
||||||
|
// not fail with EEXIST. os.Remove clears the Windows read-only attribute
|
||||||
|
// internally; on Unix unlink only needs write permission on the parent.
|
||||||
|
_ = os.Remove(fdestpath)
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return os.Symlink(linkName, fdestpath)
|
return os.Symlink(linkName, fdestpath)
|
||||||
}
|
}
|
||||||
df, err := os.OpenFile(fdestpath, os.O_CREATE|os.O_WRONLY, fi.Mode())
|
df, err := os.OpenFile(fdestpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fi.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -128,7 +134,6 @@ func (*DefaultFs) Readlink(path string) (string, error) {
|
|||||||
return os.Readlink(path)
|
return os.Readlink(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func (fc *FileCollector) CollectFiles(ctx context.Context, submodulePath []string) filepath.WalkFunc {
|
func (fc *FileCollector) CollectFiles(ctx context.Context, submodulePath []string) filepath.WalkFunc {
|
||||||
i, _ := fc.Fs.OpenGitIndex(path.Join(fc.SrcPath, path.Join(submodulePath...)))
|
i, _ := fc.Fs.OpenGitIndex(path.Join(fc.SrcPath, path.Join(submodulePath...)))
|
||||||
return func(file string, fi os.FileInfo, err error) error {
|
return func(file string, fi os.FileInfo, err error) error {
|
||||||
|
|||||||
@@ -8,9 +8,12 @@ import (
|
|||||||
"archive/tar"
|
"archive/tar"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5"
|
"github.com/go-git/go-billy/v5"
|
||||||
"github.com/go-git/go-billy/v5/memfs"
|
"github.com/go-git/go-billy/v5/memfs"
|
||||||
@@ -20,6 +23,7 @@ import (
|
|||||||
"github.com/go-git/go-git/v5/plumbing/format/index"
|
"github.com/go-git/go-git/v5/plumbing/format/index"
|
||||||
"github.com/go-git/go-git/v5/storage/filesystem"
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type memoryFs struct {
|
type memoryFs struct {
|
||||||
@@ -174,3 +178,107 @@ func TestSymlinks(t *testing.T) {
|
|||||||
assert.Equal(t, ".env", files["test.env"].Linkname)
|
assert.Equal(t, ".env", files["test.env"].Linkname)
|
||||||
assert.ErrorIs(t, err, io.EOF, "tar must be read cleanly to EOF")
|
assert.ErrorIs(t, err, io.EOF, "tar must be read cleanly to EOF")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Regression for https://gitea.com/gitea/runner/issues/876 and /941:
|
||||||
|
// re-copying an action directory must overwrite a pre-existing read-only
|
||||||
|
// file (e.g. a git pack .idx at mode 0444) instead of failing with EACCES
|
||||||
|
// on macOS or "Access is denied" on Windows.
|
||||||
|
func TestCopyCollectorWriteFileOverwritesReadOnlyFile(t *testing.T) {
|
||||||
|
dst := t.TempDir()
|
||||||
|
target := filepath.Join(dst, "sub", "pack.idx")
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
|
||||||
|
require.NoError(t, os.WriteFile(target, []byte("old"), 0o444))
|
||||||
|
|
||||||
|
src := filepath.Join(t.TempDir(), "pack.idx")
|
||||||
|
require.NoError(t, os.WriteFile(src, []byte("new"), 0o444))
|
||||||
|
fi, err := os.Stat(src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cc := &CopyCollector{DstDir: dst}
|
||||||
|
require.NoError(t, cc.WriteFile("sub/pack.idx", fi, "", strings.NewReader("new")))
|
||||||
|
|
||||||
|
got, err := os.ReadFile(target)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "new", string(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Without the destination removal, os.Symlink fails with EEXIST when the
|
||||||
|
// path already holds a regular file from an earlier copy of the action.
|
||||||
|
func TestCopyCollectorWriteFileOverwritesFileWithSymlink(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("creating symlinks requires elevated privileges on Windows")
|
||||||
|
}
|
||||||
|
dst := t.TempDir()
|
||||||
|
target := filepath.Join(dst, "link")
|
||||||
|
require.NoError(t, os.WriteFile(target, []byte("stale"), 0o644))
|
||||||
|
|
||||||
|
fi, err := os.Lstat(target)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cc := &CopyCollector{DstDir: dst}
|
||||||
|
require.NoError(t, cc.WriteFile("link", fi, "target", nil))
|
||||||
|
|
||||||
|
resolved, err := os.Readlink(target)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "target", resolved)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultFsOpenReadlinkAndWalk(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("creating symlinks requires elevated privileges on Windows")
|
||||||
|
}
|
||||||
|
|
||||||
|
root := t.TempDir()
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(root, "file.txt"), []byte("content"), 0o644))
|
||||||
|
require.NoError(t, os.Symlink("file.txt", filepath.Join(root, "link.txt")))
|
||||||
|
|
||||||
|
fsys := &DefaultFs{}
|
||||||
|
var walked []string
|
||||||
|
require.NoError(t, fsys.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
|
require.NoError(t, err)
|
||||||
|
walked = append(walked, info.Name())
|
||||||
|
return nil
|
||||||
|
}))
|
||||||
|
require.Contains(t, walked, "file.txt")
|
||||||
|
require.Contains(t, walked, "link.txt")
|
||||||
|
|
||||||
|
file, err := fsys.Open(filepath.Join(root, "file.txt"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
data, err := io.ReadAll(file)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, file.Close())
|
||||||
|
require.Equal(t, "content", string(data))
|
||||||
|
|
||||||
|
link, err := fsys.Readlink(filepath.Join(root, "link.txt"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "file.txt", link)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileCollectorCancellationAndWalkError(t *testing.T) {
|
||||||
|
fc := &FileCollector{Fs: &memoryFs{Filesystem: memfs.New()}}
|
||||||
|
walk := fc.CollectFiles(cancelledContext(t), nil)
|
||||||
|
|
||||||
|
err := walk("file", fakeFileInfo{name: "file"}, nil)
|
||||||
|
require.EqualError(t, err, "copy cancelled")
|
||||||
|
|
||||||
|
err = walk("file", fakeFileInfo{name: "file"}, os.ErrPermission)
|
||||||
|
require.ErrorIs(t, err, os.ErrPermission)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cancelledContext(t *testing.T) context.Context {
|
||||||
|
t.Helper()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeFileInfo struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fakeFileInfo) Name() string { return f.name }
|
||||||
|
func (f fakeFileInfo) Size() int64 { return 0 }
|
||||||
|
func (f fakeFileInfo) Mode() os.FileMode { return 0o644 }
|
||||||
|
func (f fakeFileInfo) ModTime() time.Time { return time.Time{} }
|
||||||
|
func (f fakeFileInfo) IsDir() bool { return false }
|
||||||
|
func (f fakeFileInfo) Sys() any { return nil }
|
||||||
|
|||||||
@@ -4,18 +4,6 @@
|
|||||||
|
|
||||||
package lookpath
|
package lookpath
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
type Env interface {
|
type Env interface {
|
||||||
Getenv(name string) string
|
Getenv(name string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type defaultEnv struct{}
|
|
||||||
|
|
||||||
func (*defaultEnv) Getenv(name string) string {
|
|
||||||
return os.Getenv(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LookPath(file string) (string, error) {
|
|
||||||
return LookPath2(file, &defaultEnv{})
|
|
||||||
}
|
|
||||||
|
|||||||
74
act/lookpath/lp_unix_test.go
Normal file
74
act/lookpath/lp_unix_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
||||||
|
|
||||||
|
package lookpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testEnv map[string]string
|
||||||
|
|
||||||
|
func (e testEnv) Getenv(name string) string {
|
||||||
|
return e[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookPath2SearchesPathAndEmptyElement(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
exe := filepath.Join(dir, "tool")
|
||||||
|
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := LookPath2("tool", testEnv{"PATH": string(filepath.ListSeparator) + dir})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got != exe {
|
||||||
|
t.Fatalf("LookPath2() = %q, want %q", got, exe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookPath2DirectPathDoesNotSearchPath(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
exe := filepath.Join(dir, "tool")
|
||||||
|
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := LookPath2(exe, testEnv{"PATH": ""})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got != exe {
|
||||||
|
t.Fatalf("LookPath2() = %q, want %q", got, exe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookPath2ReportsPermissionAndNotFound(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
file := filepath.Join(dir, "not-executable")
|
||||||
|
if err := os.WriteFile(file, []byte("plain text"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := LookPath2(file, testEnv{"PATH": dir})
|
||||||
|
var pathErr *Error
|
||||||
|
if !errors.As(err, &pathErr) || !errors.Is(pathErr.Err, fs.ErrPermission) {
|
||||||
|
t.Fatalf("LookPath2(non-executable) error = %v, want fs.ErrPermission wrapped in *Error", err)
|
||||||
|
}
|
||||||
|
if pathErr.Error() != fs.ErrPermission.Error() {
|
||||||
|
t.Fatalf("Error() = %q, want %q", pathErr.Error(), fs.ErrPermission.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = LookPath2("missing", testEnv{"PATH": dir})
|
||||||
|
if !errors.As(err, &pathErr) || !errors.Is(pathErr.Err, ErrNotFound) {
|
||||||
|
t.Fatalf("LookPath2(missing) error = %v, want ErrNotFound wrapped in *Error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,7 @@ func LookPath2(file string, lenv Env) (string, error) {
|
|||||||
var exts []string
|
var exts []string
|
||||||
x := lenv.Getenv(`PATHEXT`)
|
x := lenv.Getenv(`PATHEXT`)
|
||||||
if x != "" {
|
if x != "" {
|
||||||
for _, e := range strings.Split(strings.ToLower(x), `;`) {
|
for e := range strings.SplitSeq(strings.ToLower(x), `;`) {
|
||||||
if e == "" {
|
if e == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,17 +76,19 @@ func (a ActionRunsUsing) IsComposite() bool {
|
|||||||
|
|
||||||
// ActionRuns are a field in Action
|
// ActionRuns are a field in Action
|
||||||
type ActionRuns struct {
|
type ActionRuns struct {
|
||||||
Using ActionRunsUsing `yaml:"using"`
|
Using ActionRunsUsing `yaml:"using"`
|
||||||
Env map[string]string `yaml:"env"`
|
Env map[string]string `yaml:"env"`
|
||||||
Main string `yaml:"main"`
|
Main string `yaml:"main"`
|
||||||
Pre string `yaml:"pre"`
|
Pre string `yaml:"pre"`
|
||||||
PreIf string `yaml:"pre-if"`
|
PreIf string `yaml:"pre-if"`
|
||||||
Post string `yaml:"post"`
|
Post string `yaml:"post"`
|
||||||
PostIf string `yaml:"post-if"`
|
PostIf string `yaml:"post-if"`
|
||||||
Image string `yaml:"image"`
|
Image string `yaml:"image"`
|
||||||
Entrypoint string `yaml:"entrypoint"`
|
PreEntrypoint string `yaml:"pre-entrypoint"`
|
||||||
Args []string `yaml:"args"`
|
Entrypoint string `yaml:"entrypoint"`
|
||||||
Steps []Step `yaml:"steps"`
|
PostEntrypoint string `yaml:"post-entrypoint"`
|
||||||
|
Args []string `yaml:"args"`
|
||||||
|
Steps []Step `yaml:"steps"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Action describes a metadata file for GitHub actions. The metadata filename must be either action.yml or action.yaml. The data in the metadata file defines the inputs, outputs and main entrypoint for your action.
|
// Action describes a metadata file for GitHub actions. The metadata filename must be either action.yml or action.yaml. The data in the metadata file defines the inputs, outputs and main entrypoint for your action.
|
||||||
|
|||||||
82
act/model/action_test.go
Normal file
82
act/model/action_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReadActionDefaultsAndCaseInsensitiveUsing(t *testing.T) {
|
||||||
|
action, err := ReadAction(strings.NewReader(`
|
||||||
|
name: example
|
||||||
|
runs:
|
||||||
|
using: NoDe24
|
||||||
|
main: dist/index.js
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if action.Runs.Using != ActionRunsUsingNode24 {
|
||||||
|
t.Fatalf("using = %q, want %q", action.Runs.Using, ActionRunsUsingNode24)
|
||||||
|
}
|
||||||
|
if action.Runs.PreIf != "always()" {
|
||||||
|
t.Fatalf("pre-if = %q, want always()", action.Runs.PreIf)
|
||||||
|
}
|
||||||
|
if action.Runs.PostIf != "always()" {
|
||||||
|
t.Fatalf("post-if = %q, want always()", action.Runs.PostIf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadActionPreservesExplicitConditions(t *testing.T) {
|
||||||
|
action, err := ReadAction(strings.NewReader(`
|
||||||
|
runs:
|
||||||
|
using: composite
|
||||||
|
pre-if: success()
|
||||||
|
post-if: failure()
|
||||||
|
steps:
|
||||||
|
- run: echo hello
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if action.Runs.PreIf != "success()" || action.Runs.PostIf != "failure()" {
|
||||||
|
t.Fatalf("conditions = %q/%q, want explicit values", action.Runs.PreIf, action.Runs.PostIf)
|
||||||
|
}
|
||||||
|
if !action.Runs.Using.IsComposite() || action.Runs.Using.IsDocker() || action.Runs.Using.IsNode() {
|
||||||
|
t.Fatalf("unexpected using predicates for %q", action.Runs.Using)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadActionRejectsUnknownUsing(t *testing.T) {
|
||||||
|
_, err := ReadAction(strings.NewReader(`
|
||||||
|
runs:
|
||||||
|
using: node99
|
||||||
|
`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected unknown runs.using to fail")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "node99") {
|
||||||
|
t.Fatalf("error = %q, want invalid value", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadActionDockerEntrypoints(t *testing.T) {
|
||||||
|
action, err := ReadAction(strings.NewReader(`
|
||||||
|
runs:
|
||||||
|
using: docker
|
||||||
|
image: Dockerfile
|
||||||
|
pre-entrypoint: pre.sh
|
||||||
|
post-entrypoint: post.sh
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if action.Runs.PreEntrypoint != "pre.sh" {
|
||||||
|
t.Fatalf("pre-entrypoint = %q, want pre.sh", action.Runs.PreEntrypoint)
|
||||||
|
}
|
||||||
|
if action.Runs.PostEntrypoint != "post.sh" {
|
||||||
|
t.Fatalf("post-entrypoint = %q, want post.sh", action.Runs.PostEntrypoint)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GithubContext struct {
|
type GithubContext struct {
|
||||||
|
|||||||
@@ -61,8 +61,6 @@ type WorkflowFiles struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewWorkflowPlanner will load a specific workflow, all workflows from a directory or all workflows from a directory and its subdirectories
|
// NewWorkflowPlanner will load a specific workflow, all workflows from a directory or all workflows from a directory and its subdirectories
|
||||||
//
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, error) {
|
func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, error) {
|
||||||
path, err := filepath.Abs(path)
|
path, err := filepath.Abs(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WorkflowPlanTest struct {
|
type WorkflowPlanTest struct {
|
||||||
@@ -57,11 +59,141 @@ func TestWorkflow(t *testing.T) {
|
|||||||
|
|
||||||
// Check that an invalid job id returns error
|
// Check that an invalid job id returns error
|
||||||
result, err := createStages(&workflow, "invalid_job_id")
|
result, err := createStages(&workflow, "invalid_job_id")
|
||||||
assert.NotNil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Nil(t, result)
|
assert.Nil(t, result)
|
||||||
|
|
||||||
// Check that an valid job id returns non-error
|
// Check that an valid job id returns non-error
|
||||||
result, err = createStages(&workflow, "valid_job")
|
result, err = createStages(&workflow, "valid_job")
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.NotNil(t, result)
|
assert.NotNil(t, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewSingleWorkflowPlannerAndPlanMethods(t *testing.T) {
|
||||||
|
planner, err := NewSingleWorkflowPlanner("ci.yml", strings.NewReader(`
|
||||||
|
name: CI
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build project
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: make build
|
||||||
|
test:
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: make test
|
||||||
|
`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"pull_request", "push"}, planner.GetEvents())
|
||||||
|
|
||||||
|
eventPlan, err := planner.PlanEvent("push")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, eventPlan.Stages, 2)
|
||||||
|
assert.Equal(t, []string{"build"}, eventPlan.Stages[0].GetJobIDs())
|
||||||
|
assert.Equal(t, []string{"test"}, eventPlan.Stages[1].GetJobIDs())
|
||||||
|
assert.Equal(t, len("Build project"), eventPlan.MaxRunNameLen())
|
||||||
|
assert.Equal(t, "Build project", eventPlan.Stages[0].Runs[0].String())
|
||||||
|
assert.Equal(t, "build", eventPlan.Stages[0].Runs[0].JobID)
|
||||||
|
assert.NotNil(t, eventPlan.Stages[0].Runs[0].Job())
|
||||||
|
|
||||||
|
jobPlan, err := planner.PlanJob("test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, jobPlan.Stages, 2)
|
||||||
|
assert.Equal(t, []string{"build"}, jobPlan.Stages[0].GetJobIDs())
|
||||||
|
assert.Equal(t, []string{"test"}, jobPlan.Stages[1].GetJobIDs())
|
||||||
|
|
||||||
|
allPlan, err := planner.PlanAll()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, allPlan.Stages, 2)
|
||||||
|
assert.Equal(t, []string{"build"}, allPlan.Stages[0].GetJobIDs())
|
||||||
|
assert.Equal(t, []string{"test"}, allPlan.Stages[1].GetJobIDs())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCombineWorkflowPlannerMergesWorkflowStages(t *testing.T) {
|
||||||
|
first := mustReadWorkflow(t, `
|
||||||
|
name: First
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: make build
|
||||||
|
`)
|
||||||
|
second := mustReadWorkflow(t, `
|
||||||
|
name: Second
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: make lint
|
||||||
|
test:
|
||||||
|
needs: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: make test
|
||||||
|
`)
|
||||||
|
|
||||||
|
planner := CombineWorkflowPlanner(first, second)
|
||||||
|
plan, err := planner.PlanEvent("push")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, plan.Stages, 2)
|
||||||
|
assert.ElementsMatch(t, []string{"build", "lint"}, plan.Stages[0].GetJobIDs())
|
||||||
|
assert.Equal(t, []string{"test"}, plan.Stages[1].GetJobIDs())
|
||||||
|
|
||||||
|
empty, err := planner.PlanEvent("schedule")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, empty.Stages)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPlannerErrorsForMissingAndCyclicJobs(t *testing.T) {
|
||||||
|
workflow := mustReadWorkflow(t, `
|
||||||
|
name: Cyclic
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
a:
|
||||||
|
needs: b
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: echo a
|
||||||
|
b:
|
||||||
|
needs: a
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: echo b
|
||||||
|
`)
|
||||||
|
planner := CombineWorkflowPlanner(workflow)
|
||||||
|
|
||||||
|
plan, err := planner.PlanJob("missing")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Empty(t, plan.Stages)
|
||||||
|
assert.Contains(t, err.Error(), "Could not find any stages")
|
||||||
|
|
||||||
|
plan, err = planner.PlanEvent("push")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Empty(t, plan.Stages)
|
||||||
|
assert.Contains(t, err.Error(), "unable to build dependency graph")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewSingleWorkflowPlannerErrors(t *testing.T) {
|
||||||
|
_, err := NewSingleWorkflowPlanner("empty.yml", strings.NewReader(""))
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "file is empty")
|
||||||
|
|
||||||
|
_, err = NewSingleWorkflowPlanner("invalid.yml", strings.NewReader("jobs: ["))
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "workflow is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustReadWorkflow(t *testing.T, content string) *Workflow {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
workflow, err := ReadWorkflow(strings.NewReader(content))
|
||||||
|
require.NoError(t, err)
|
||||||
|
if workflow.Name == "" {
|
||||||
|
workflow.Name = "workflow"
|
||||||
|
}
|
||||||
|
return workflow
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ jobs:
|
|||||||
with-volumes:
|
with-volumes:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container:
|
||||||
image: node:16-buster-slim
|
image: node:24-bookworm-slim
|
||||||
volumes:
|
volumes:
|
||||||
- my_docker_volume:/path/to/volume
|
- my_docker_volume:/path/to/volume
|
||||||
- /path/to/nonexist/directory
|
- /path/to/nonexist/directory
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
@@ -86,11 +86,12 @@ func (w *Workflow) OnSchedule() []string {
|
|||||||
case []any:
|
case []any:
|
||||||
allSchedules := []string{}
|
allSchedules := []string{}
|
||||||
for _, v := range val {
|
for _, v := range val {
|
||||||
for k, cron := range v.(map[string]any) {
|
entry, ok := v.(map[string]any)
|
||||||
if k != "cron" {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allSchedules = append(allSchedules, cron.(string))
|
if cron, ok := entry["cron"].(string); ok {
|
||||||
|
allSchedules = append(allSchedules, cron)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allSchedules
|
return allSchedules
|
||||||
@@ -190,23 +191,52 @@ func (w *Workflow) WorkflowCallConfig() *WorkflowCall {
|
|||||||
|
|
||||||
// Job is the structure of one job in a workflow
|
// Job is the structure of one job in a workflow
|
||||||
type Job struct {
|
type Job struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
RawNeeds yaml.Node `yaml:"needs"`
|
RawNeeds yaml.Node `yaml:"needs"`
|
||||||
RawRunsOn yaml.Node `yaml:"runs-on"`
|
RawRunsOn yaml.Node `yaml:"runs-on"`
|
||||||
Env yaml.Node `yaml:"env"`
|
Env yaml.Node `yaml:"env"`
|
||||||
If yaml.Node `yaml:"if"`
|
If yaml.Node `yaml:"if"`
|
||||||
Steps []*Step `yaml:"steps"`
|
Steps []*Step `yaml:"steps"`
|
||||||
TimeoutMinutes string `yaml:"timeout-minutes"`
|
TimeoutMinutes string `yaml:"timeout-minutes"`
|
||||||
Services map[string]*ContainerSpec `yaml:"services"`
|
RawContinueOnError string `yaml:"continue-on-error"`
|
||||||
Strategy *Strategy `yaml:"strategy"`
|
Services map[string]*ContainerSpec `yaml:"services"`
|
||||||
RawContainer yaml.Node `yaml:"container"`
|
Strategy *Strategy `yaml:"strategy"`
|
||||||
Defaults Defaults `yaml:"defaults"`
|
RawContainer yaml.Node `yaml:"container"`
|
||||||
Outputs map[string]string `yaml:"outputs"`
|
Defaults Defaults `yaml:"defaults"`
|
||||||
Uses string `yaml:"uses"`
|
Outputs map[string]string `yaml:"outputs"`
|
||||||
With map[string]any `yaml:"with"`
|
Uses string `yaml:"uses"`
|
||||||
RawSecrets yaml.Node `yaml:"secrets"`
|
With map[string]any `yaml:"with"`
|
||||||
RawPermissions yaml.Node `yaml:"permissions"`
|
RawSecrets yaml.Node `yaml:"secrets"`
|
||||||
Result string
|
RawPermissions yaml.Node `yaml:"permissions"`
|
||||||
|
Result string
|
||||||
|
// Runtime fields set during execution (not from YAML):
|
||||||
|
ContinueOnError bool // true when all failing matrix combinations had continue-on-error=true
|
||||||
|
hasFirmFailure bool // true once any combination failed without continue-on-error
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContinueOnError records whether this combination's failure should not fail the workflow.
|
||||||
|
// Must be called under the job lock. Safe across parallel matrix combinations.
|
||||||
|
func (j *Job) SetContinueOnError(continueOnErr bool) {
|
||||||
|
if continueOnErr {
|
||||||
|
if !j.hasFirmFailure {
|
||||||
|
j.ContinueOnError = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
j.hasFirmFailure = true
|
||||||
|
j.ContinueOnError = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NeedsResult returns the job result as seen by dependent jobs through the
|
||||||
|
// `needs` context. A job that failed but was tolerated via continue-on-error
|
||||||
|
// reports "success" to its dependents, matching GitHub: such a failure must not
|
||||||
|
// block jobs gated on the default `if: success()`, even though the overall
|
||||||
|
// workflow run is still marked as failed.
|
||||||
|
func (j *Job) NeedsResult() string {
|
||||||
|
if j.Result == "failure" && j.ContinueOnError {
|
||||||
|
return "success"
|
||||||
|
}
|
||||||
|
return j.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strategy for the job
|
// Strategy for the job
|
||||||
@@ -325,14 +355,20 @@ func (j *Job) Needs() []string {
|
|||||||
|
|
||||||
// RunsOn list for Job
|
// RunsOn list for Job
|
||||||
func (j *Job) RunsOn() []string {
|
func (j *Job) RunsOn() []string {
|
||||||
switch j.RawRunsOn.Kind {
|
return RunsOnFromNode(j.RawRunsOn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunsOnFromNode parses the runs-on labels from a raw runs-on node, so callers can evaluate a
|
||||||
|
// copy of the node (avoiding mutation of the shared Job) before reading the labels.
|
||||||
|
func RunsOnFromNode(rawRunsOn yaml.Node) []string {
|
||||||
|
switch rawRunsOn.Kind {
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
var val struct {
|
var val struct {
|
||||||
Group string
|
Group string
|
||||||
Labels yaml.Node
|
Labels yaml.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
if !decodeNode(j.RawRunsOn, &val) {
|
if !decodeNode(rawRunsOn, &val) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,7 +380,7 @@ func (j *Job) RunsOn() []string {
|
|||||||
|
|
||||||
return labels
|
return labels
|
||||||
default:
|
default:
|
||||||
return nodeAsStringSlice(j.RawRunsOn)
|
return nodeAsStringSlice(rawRunsOn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,9 +444,9 @@ func normalizeMatrixValue(key string, val any) ([]any, error) {
|
|||||||
// Scalar values are wrapped into single-element arrays automatically.
|
// Scalar values are wrapped into single-element arrays automatically.
|
||||||
// Template expressions are resolved by EvaluateYamlNode before this method is
|
// Template expressions are resolved by EvaluateYamlNode before this method is
|
||||||
// called; if unresolved, the literal string is wrapped as a one-element fallback.
|
// called; if unresolved, the literal string is wrapped as a one-element fallback.
|
||||||
func (j *Job) Matrix() map[string][]any {
|
func (j *Job) Matrix() (map[string][]any, error) {
|
||||||
if j.Strategy == nil || j.Strategy.RawMatrix.Kind != yaml.MappingNode {
|
if j.Strategy == nil || j.Strategy.RawMatrix.Kind != yaml.MappingNode {
|
||||||
return nil
|
return map[string][]any{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode to flexible map first so that scalar values don't cause a type error.
|
// Decode to flexible map first so that scalar values don't cause a type error.
|
||||||
@@ -420,9 +456,9 @@ func (j *Job) Matrix() map[string][]any {
|
|||||||
// Fall back to the strict array-only format for backward compatibility.
|
// Fall back to the strict array-only format for backward compatibility.
|
||||||
var val map[string][]any
|
var val map[string][]any
|
||||||
if !decodeNode(j.Strategy.RawMatrix, &val) {
|
if !decodeNode(j.Strategy.RawMatrix, &val) {
|
||||||
return nil
|
return map[string][]any{}, nil
|
||||||
}
|
}
|
||||||
return val
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert flexible format to expected format with validation
|
// Convert flexible format to expected format with validation
|
||||||
@@ -430,18 +466,15 @@ func (j *Job) Matrix() map[string][]any {
|
|||||||
for k, v := range flexVal {
|
for k, v := range flexVal {
|
||||||
normalized, err := normalizeMatrixValue(k, v)
|
normalized, err := normalizeMatrixValue(k, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("matrix validation error: %v", err)
|
return nil, err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
val[k] = normalized
|
val[k] = normalized
|
||||||
}
|
}
|
||||||
return val
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMatrixes returns the matrix cross product
|
// GetMatrixes returns the matrix cross product
|
||||||
// It skips includes and hard fails excludes for non-existing keys
|
// It skips includes and hard fails excludes for non-existing keys
|
||||||
//
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func (j *Job) GetMatrixes() ([]map[string]any, error) {
|
func (j *Job) GetMatrixes() ([]map[string]any, error) {
|
||||||
matrixes := make([]map[string]any, 0)
|
matrixes := make([]map[string]any, 0)
|
||||||
if j.Strategy != nil {
|
if j.Strategy != nil {
|
||||||
@@ -449,38 +482,38 @@ func (j *Job) GetMatrixes() ([]map[string]any, error) {
|
|||||||
j.Strategy.FailFast = j.Strategy.GetFailFast()
|
j.Strategy.FailFast = j.Strategy.GetFailFast()
|
||||||
j.Strategy.MaxParallel = j.Strategy.GetMaxParallel()
|
j.Strategy.MaxParallel = j.Strategy.GetMaxParallel()
|
||||||
|
|
||||||
if m := j.Matrix(); m != nil {
|
m, err := j.Matrix()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(m) > 0 {
|
||||||
includes := make([]map[string]any, 0)
|
includes := make([]map[string]any, 0)
|
||||||
extraIncludes := make([]map[string]any, 0)
|
extraIncludes := make([]map[string]any, 0)
|
||||||
|
addInclude := func(raw any) error {
|
||||||
|
include, ok := raw.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("the workflow is not valid. Matrix include %v is not a map of matrix keys to values", raw)
|
||||||
|
}
|
||||||
|
for k := range include {
|
||||||
|
if _, ok := m[k]; ok {
|
||||||
|
includes = append(includes, include)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extraIncludes = append(extraIncludes, include)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, v := range m["include"] {
|
for _, v := range m["include"] {
|
||||||
switch t := v.(type) {
|
switch t := v.(type) {
|
||||||
case []any:
|
case []any:
|
||||||
for _, i := range t {
|
for _, i := range t {
|
||||||
i := i.(map[string]any)
|
if err := addInclude(i); err != nil {
|
||||||
extraInclude := true
|
return nil, err
|
||||||
for k := range i {
|
|
||||||
if _, ok := m[k]; ok {
|
|
||||||
includes = append(includes, i)
|
|
||||||
extraInclude = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if extraInclude {
|
|
||||||
extraIncludes = append(extraIncludes, i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case any:
|
case any:
|
||||||
v := v.(map[string]any)
|
if err := addInclude(t); err != nil {
|
||||||
extraInclude := true
|
return nil, err
|
||||||
for k := range v {
|
|
||||||
if _, ok := m[k]; ok {
|
|
||||||
includes = append(includes, v)
|
|
||||||
extraInclude = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if extraInclude {
|
|
||||||
extraIncludes = append(extraIncludes, v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -488,10 +521,13 @@ func (j *Job) GetMatrixes() ([]map[string]any, error) {
|
|||||||
|
|
||||||
excludes := make([]map[string]any, 0)
|
excludes := make([]map[string]any, 0)
|
||||||
for _, e := range m["exclude"] {
|
for _, e := range m["exclude"] {
|
||||||
e := e.(map[string]any)
|
exclude, ok := e.(map[string]any)
|
||||||
for k := range e {
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("the workflow is not valid. Matrix exclude %v is not a map of matrix keys to values", e)
|
||||||
|
}
|
||||||
|
for k := range exclude {
|
||||||
if _, ok := m[k]; ok {
|
if _, ok := m[k]; ok {
|
||||||
excludes = append(excludes, e)
|
excludes = append(excludes, exclude)
|
||||||
} else {
|
} else {
|
||||||
// We fail completely here because that's what GitHub does for non-existing matrix keys, fail on exclude, silent skip on include
|
// We fail completely here because that's what GitHub does for non-existing matrix keys, fail on exclude, silent skip on include
|
||||||
return nil, fmt.Errorf("the workflow is not valid. Matrix exclude key %q does not match any key within the matrix", k)
|
return nil, fmt.Errorf("the workflow is not valid. Matrix exclude key %q does not match any key within the matrix", k)
|
||||||
@@ -647,6 +683,33 @@ type Step struct {
|
|||||||
TimeoutMinutes string `yaml:"timeout-minutes"`
|
TimeoutMinutes string `yaml:"timeout-minutes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a deep copy safe to mutate independently of s. Job steps are shared across
|
||||||
|
// parallel matrix runs, which mutate per-job fields (ID, Number, Shell) and evaluate the If/Env
|
||||||
|
// yaml.Nodes in place, so each job must own its copy.
|
||||||
|
func (s *Step) Clone() *Step {
|
||||||
|
clone := *s
|
||||||
|
clone.If = CloneYamlNode(s.If)
|
||||||
|
clone.Env = CloneYamlNode(s.Env)
|
||||||
|
clone.With = maps.Clone(s.With)
|
||||||
|
return &clone
|
||||||
|
}
|
||||||
|
|
||||||
|
// CloneYamlNode returns a deep copy of a yaml.Node so callers can evaluate it in place without
|
||||||
|
// mutating a node shared across parallel jobs.
|
||||||
|
func CloneYamlNode(n yaml.Node) yaml.Node {
|
||||||
|
clone := n
|
||||||
|
if n.Content != nil {
|
||||||
|
clone.Content = make([]*yaml.Node, len(n.Content))
|
||||||
|
for i, child := range n.Content {
|
||||||
|
if child != nil {
|
||||||
|
childClone := CloneYamlNode(*child)
|
||||||
|
clone.Content[i] = &childClone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
||||||
// String gets the name of step
|
// String gets the name of step
|
||||||
func (s *Step) String() string {
|
func (s *Step) String() string {
|
||||||
if s.Name != "" {
|
if s.Name != "" {
|
||||||
|
|||||||
@@ -5,13 +5,244 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestStepCloneIsolatesMutableFields guards the parallel-matrix race fix: combinations share the
|
||||||
|
// job's *Step, and Clone() must hand each a copy whose If/Env nodes and With map can be mutated
|
||||||
|
// independently. A shallow copy would share Env.Content's backing array (and the With map) and
|
||||||
|
// leak writes across combinations.
|
||||||
|
func TestStepCloneIsolatesMutableFields(t *testing.T) {
|
||||||
|
var orig Step
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte("if: ${{ env.X == 'a' }}\nenv:\n KEY: original\nwith:\n arg: original\n"), &orig))
|
||||||
|
require.Len(t, orig.Env.Content, 2) // [key, value]
|
||||||
|
|
||||||
|
clone := orig.Clone()
|
||||||
|
clone.If.Value = "changed"
|
||||||
|
clone.Env.Content[1].Value = "changed"
|
||||||
|
clone.With["arg"] = "changed"
|
||||||
|
|
||||||
|
assert.Equal(t, "${{ env.X == 'a' }}", orig.If.Value, "If must not be shared with the clone")
|
||||||
|
assert.Equal(t, "original", orig.Env.Content[1].Value, "Env nodes must not be shared with the clone")
|
||||||
|
assert.Equal(t, "original", orig.With["arg"], "With map must not be shared with the clone")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestJobNeedsResult guards the continue-on-error semantics exposed to dependent
|
||||||
|
// jobs through the `needs` context: a failed-but-tolerated job reports "success"
|
||||||
|
// so it does not block dependents gated on the default `if: success()`, matching
|
||||||
|
// GitHub. A firm failure and any non-failure result are reported verbatim.
|
||||||
|
func TestJobNeedsResult(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
result string
|
||||||
|
continueOnError bool
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"tolerated failure reports success", "failure", true, "success"},
|
||||||
|
{"firm failure reports failure", "failure", false, "failure"},
|
||||||
|
{"success is unchanged", "success", false, "success"},
|
||||||
|
{"success with continue-on-error is unchanged", "success", true, "success"},
|
||||||
|
{"empty result is unchanged", "", true, ""},
|
||||||
|
{"skipped is unchanged", "skipped", true, "skipped"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
j := &Job{Result: tc.result, ContinueOnError: tc.continueOnError}
|
||||||
|
assert.Equal(t, tc.want, j.NeedsResult())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobSetContinueOnErrorFirmFailureWins(t *testing.T) {
|
||||||
|
job := &Job{}
|
||||||
|
job.SetContinueOnError(true)
|
||||||
|
assert.True(t, job.ContinueOnError)
|
||||||
|
|
||||||
|
job.SetContinueOnError(false)
|
||||||
|
assert.False(t, job.ContinueOnError)
|
||||||
|
|
||||||
|
job.SetContinueOnError(true)
|
||||||
|
assert.False(t, job.ContinueOnError, "a later tolerated failure must not hide an earlier firm failure")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStepStatusText(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
status stepStatus
|
||||||
|
text string
|
||||||
|
}{
|
||||||
|
{StepStatusSuccess, "success"},
|
||||||
|
{StepStatusFailure, "failure"},
|
||||||
|
{StepStatusSkipped, "skipped"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.text, func(t *testing.T) {
|
||||||
|
got, err := tc.status.MarshalText()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, tc.text, string(got))
|
||||||
|
|
||||||
|
var parsed stepStatus
|
||||||
|
require.NoError(t, parsed.UnmarshalText(got))
|
||||||
|
assert.Equal(t, tc.status, parsed)
|
||||||
|
assert.Equal(t, tc.text, parsed.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed stepStatus
|
||||||
|
require.Error(t, parsed.UnmarshalText([]byte("cancelled")))
|
||||||
|
assert.Empty(t, stepStatus(99).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowCallConfig(t *testing.T) {
|
||||||
|
workflow, err := ReadWorkflow(strings.NewReader(`
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
name:
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
outputs:
|
||||||
|
digest:
|
||||||
|
value: ${{ jobs.build.outputs.digest }}
|
||||||
|
jobs: {}
|
||||||
|
`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
config := workflow.WorkflowCallConfig()
|
||||||
|
require.NotNil(t, config)
|
||||||
|
require.Contains(t, config.Inputs, "name")
|
||||||
|
assert.True(t, config.Inputs["name"].Required)
|
||||||
|
assert.Equal(t, "string", config.Inputs["name"].Type)
|
||||||
|
assert.Equal(t, "${{ jobs.build.outputs.digest }}", config.Outputs["digest"].Value)
|
||||||
|
|
||||||
|
listWorkflow, err := ReadWorkflow(strings.NewReader("on: [workflow_call]\njobs: {}\n"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotNil(t, listWorkflow.WorkflowCallConfig())
|
||||||
|
assert.Empty(t, listWorkflow.WorkflowCallConfig().Inputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobSecretsAndEnvironment(t *testing.T) {
|
||||||
|
inheritJob := readJob(t, `
|
||||||
|
secrets: inherit
|
||||||
|
env:
|
||||||
|
A: one
|
||||||
|
B: two
|
||||||
|
`)
|
||||||
|
assert.True(t, inheritJob.InheritSecrets())
|
||||||
|
assert.Nil(t, inheritJob.Secrets())
|
||||||
|
assert.Equal(t, map[string]string{"A": "one", "B": "two"}, inheritJob.Environment())
|
||||||
|
|
||||||
|
mappingJob := readJob(t, `
|
||||||
|
secrets:
|
||||||
|
TOKEN: ${{ secrets.TOKEN }}
|
||||||
|
`)
|
||||||
|
assert.False(t, mappingJob.InheritSecrets())
|
||||||
|
assert.Equal(t, map[string]string{"TOKEN": "${{ secrets.TOKEN }}"}, mappingJob.Secrets())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobTypeAndString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
job Job
|
||||||
|
want JobType
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{job: Job{}, want: JobTypeDefault},
|
||||||
|
{job: Job{Uses: "./.github/workflows/reuse.yml"}, want: JobTypeReusableWorkflowLocal},
|
||||||
|
{job: Job{Uses: "owner/repo/.github/workflows/reuse.yaml@v1"}, want: JobTypeReusableWorkflowRemote},
|
||||||
|
{job: Job{Uses: "owner/repo/.github/workflows/reuse.yaml"}, want: JobTypeInvalid, wantErr: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(fmt.Sprintf("%s/%s", tc.job.Uses, tc.want), func(t *testing.T) {
|
||||||
|
got, err := tc.job.Type()
|
||||||
|
if tc.wantErr {
|
||||||
|
require.Error(t, err)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "default", JobTypeDefault.String())
|
||||||
|
assert.Equal(t, "local-reusable-workflow", JobTypeReusableWorkflowLocal.String())
|
||||||
|
assert.Equal(t, "remote-reusable-workflow", JobTypeReusableWorkflowRemote.String())
|
||||||
|
assert.Equal(t, "unknown", JobType(99).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStepStringEnvironmentEnvAndType(t *testing.T) {
|
||||||
|
step := readStep(t, `
|
||||||
|
id: example
|
||||||
|
env:
|
||||||
|
DIRECT: value
|
||||||
|
with:
|
||||||
|
mixed-key: input
|
||||||
|
`)
|
||||||
|
assert.Equal(t, "example", step.String())
|
||||||
|
assert.Equal(t, map[string]string{"DIRECT": "value"}, step.Environment())
|
||||||
|
assert.Equal(t, map[string]string{"DIRECT": "value", "INPUT_MIXED-KEY": "input"}, step.GetEnv())
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
step Step
|
||||||
|
want StepType
|
||||||
|
}{
|
||||||
|
{step: Step{}, want: StepTypeInvalid},
|
||||||
|
{step: Step{Run: "echo hi"}, want: StepTypeRun},
|
||||||
|
{step: Step{Run: "echo hi", Uses: "actions/checkout@v4"}, want: StepTypeInvalid},
|
||||||
|
{step: Step{Uses: "docker://alpine:latest"}, want: StepTypeUsesDockerURL},
|
||||||
|
{step: Step{Uses: "./.github/workflows/reuse.yml"}, want: StepTypeReusableWorkflowLocal},
|
||||||
|
{step: Step{Uses: "owner/repo/.github/workflows/reuse.yml@v1"}, want: StepTypeReusableWorkflowRemote},
|
||||||
|
{step: Step{Uses: "./actions/local"}, want: StepTypeUsesActionLocal},
|
||||||
|
{step: Step{Uses: "actions/checkout@v4"}, want: StepTypeUsesActionRemote},
|
||||||
|
} {
|
||||||
|
t.Run(tc.want.String(), func(t *testing.T) {
|
||||||
|
assert.Equal(t, tc.want, tc.step.Type())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "invalid", StepTypeInvalid.String())
|
||||||
|
assert.Equal(t, "run", StepTypeRun.String())
|
||||||
|
assert.Equal(t, "local-action", StepTypeUsesActionLocal.String())
|
||||||
|
assert.Equal(t, "remote-action", StepTypeUsesActionRemote.String())
|
||||||
|
assert.Equal(t, "docker", StepTypeUsesDockerURL.String())
|
||||||
|
assert.Equal(t, "local-reusable-workflow", StepTypeReusableWorkflowLocal.String())
|
||||||
|
assert.Equal(t, "remote-reusable-workflow", StepTypeReusableWorkflowRemote.String())
|
||||||
|
assert.Equal(t, "unknown", StepType(99).String())
|
||||||
|
assert.NotEmpty(t, (&Step{Uses: "actions/checkout@v4"}).UsesHash())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowGetJobAndIDs(t *testing.T) {
|
||||||
|
workflow := &Workflow{Jobs: map[string]*Job{"build": {}}}
|
||||||
|
assert.Equal(t, []string{"build"}, workflow.GetJobIDs())
|
||||||
|
|
||||||
|
job := workflow.GetJob("build")
|
||||||
|
require.NotNil(t, job)
|
||||||
|
assert.Equal(t, "build", job.Name)
|
||||||
|
assert.Equal(t, "success()", job.If.Value)
|
||||||
|
assert.Nil(t, workflow.GetJob("missing"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRawConcurrencyYaml(t *testing.T) {
|
||||||
|
var expr RawConcurrency
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte("group-${{ github.ref }}"), &expr))
|
||||||
|
assert.Equal(t, "group-${{ github.ref }}", expr.RawExpression)
|
||||||
|
marshaled, err := expr.MarshalYAML()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "group-${{ github.ref }}", marshaled)
|
||||||
|
|
||||||
|
var object RawConcurrency
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte("group: ci\ncancel-in-progress: true\n"), &object))
|
||||||
|
assert.Equal(t, "ci", object.Group)
|
||||||
|
assert.Equal(t, "true", object.CancelInProgress)
|
||||||
|
marshaled, err = object.MarshalYAML()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, (*objectConcurrency)(&object), marshaled)
|
||||||
|
}
|
||||||
|
|
||||||
func TestReadWorkflow_ScheduleEvent(t *testing.T) {
|
func TestReadWorkflow_ScheduleEvent(t *testing.T) {
|
||||||
yaml := `
|
yaml := `
|
||||||
name: local-action-docker-url
|
name: local-action-docker-url
|
||||||
@@ -56,7 +287,7 @@ jobs:
|
|||||||
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
newSchedules = workflow.OnSchedule()
|
newSchedules = workflow.OnSchedule()
|
||||||
assert.Len(t, newSchedules, 0) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Empty(t, newSchedules)
|
||||||
|
|
||||||
yaml = `
|
yaml = `
|
||||||
name: local-action-docker-url
|
name: local-action-docker-url
|
||||||
@@ -74,7 +305,7 @@ jobs:
|
|||||||
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
newSchedules = workflow.OnSchedule()
|
newSchedules = workflow.OnSchedule()
|
||||||
assert.Len(t, newSchedules, 0) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Empty(t, newSchedules)
|
||||||
|
|
||||||
yaml = `
|
yaml = `
|
||||||
name: local-action-docker-url
|
name: local-action-docker-url
|
||||||
@@ -91,7 +322,7 @@ jobs:
|
|||||||
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "read workflow should succeed") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
newSchedules = workflow.OnSchedule()
|
newSchedules = workflow.OnSchedule()
|
||||||
assert.Len(t, newSchedules, 0) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Empty(t, newSchedules)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadWorkflow_StringEvent(t *testing.T) {
|
func TestReadWorkflow_StringEvent(t *testing.T) {
|
||||||
@@ -436,7 +667,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
|
|||||||
matrixes, err := job.GetMatrixes()
|
matrixes, err := job.GetMatrixes()
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Matrix(), map[string][]any(nil))
|
matrix, err := job.Matrix()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, matrix)
|
||||||
assert.Equal(t, job.Strategy.MaxParallel, 2) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.MaxParallel, 2) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Strategy.FailFast, true) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.FailFast, true) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
@@ -444,7 +677,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
|
|||||||
matrixes, err = job.GetMatrixes()
|
matrixes, err = job.GetMatrixes()
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Matrix(), map[string][]any(nil))
|
matrix, err = job.Matrix()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, matrix)
|
||||||
assert.Equal(t, job.Strategy.MaxParallel, 4) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.MaxParallel, 4) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Strategy.FailFast, false) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.FailFast, false) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
@@ -452,7 +687,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
|
|||||||
matrixes, err = job.GetMatrixes()
|
matrixes, err = job.GetMatrixes()
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, matrixes, []map[string]any{{}}) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Matrix(), map[string][]any(nil))
|
matrix, err = job.Matrix()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, matrix)
|
||||||
assert.Equal(t, job.Strategy.MaxParallel, 2) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.MaxParallel, 2) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, job.Strategy.FailFast, false) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, job.Strategy.FailFast, false) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
@@ -469,7 +706,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
|
|||||||
{"datacenter": "site-b", "node-version": "12.x", "site": "dev"},
|
{"datacenter": "site-b", "node-version": "12.x", "site": "dev"},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert.Equal(t, job.Matrix(), //nolint:testifylint // pre-existing issue from nektos/act
|
matrix, err = job.Matrix()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, matrix, //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
map[string][]any{
|
map[string][]any{
|
||||||
"datacenter": {"site-c", "site-d"},
|
"datacenter": {"site-c", "site-d"},
|
||||||
"exclude": {
|
"exclude": {
|
||||||
@@ -861,16 +1100,18 @@ jobs:
|
|||||||
t.Fatal("job not found")
|
t.Fatal("job not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix := job.Matrix()
|
matrix, err := job.Matrix()
|
||||||
|
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
|
require.Error(t, err)
|
||||||
assert.Nil(t, matrix, "matrix should be nil on error")
|
assert.Nil(t, matrix, "matrix should be nil on error")
|
||||||
} else {
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
if tt.wantLen == 0 {
|
if tt.wantLen == 0 {
|
||||||
assert.Nil(t, matrix, "matrix should be nil for jobs without strategy")
|
assert.Empty(t, matrix, "no matrix for jobs without strategy")
|
||||||
} else {
|
} else {
|
||||||
assert.NotNil(t, matrix, "matrix should not be nil")
|
assert.NotNil(t, matrix, "matrix should not be nil")
|
||||||
assert.Equal(t, tt.wantLen, len(matrix), "matrix should have expected number of keys") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Len(t, matrix, tt.wantLen, "matrix should have expected number of keys")
|
||||||
if tt.checkFn != nil {
|
if tt.checkFn != nil {
|
||||||
tt.checkFn(t, matrix)
|
tt.checkFn(t, matrix)
|
||||||
}
|
}
|
||||||
@@ -899,10 +1140,24 @@ func TestJobMatrixValidation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to get matrix
|
matrix, err := job.Matrix()
|
||||||
matrix := job.Matrix()
|
require.ErrorContains(t, err, `matrix key "config" has invalid nested object value`)
|
||||||
|
assert.Nil(t, matrix)
|
||||||
// Should return nil due to validation error
|
|
||||||
assert.Nil(t, matrix, "matrix with nested map should return nil")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readJob(t *testing.T, content string) *Job {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var job Job
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte(content), &job))
|
||||||
|
return &job
|
||||||
|
}
|
||||||
|
|
||||||
|
func readStep(t *testing.T, content string) *Step {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var step Step
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte(content), &step))
|
||||||
|
return &step
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
"embed"
|
"embed"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -18,9 +20,10 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/container"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/kballard/go-shellquote"
|
"github.com/kballard/go-shellquote"
|
||||||
)
|
)
|
||||||
@@ -44,6 +47,11 @@ type runAction func(step actionStep, actionDir string, remoteAction *remoteActio
|
|||||||
//go:embed res/trampoline.js
|
//go:embed res/trampoline.js
|
||||||
var trampoline embed.FS
|
var trampoline embed.FS
|
||||||
|
|
||||||
|
var (
|
||||||
|
ContainerImageExistsLocally = container.ImageExistsLocally
|
||||||
|
ContainerNewDockerBuildExecutor = container.NewDockerBuildExecutor
|
||||||
|
)
|
||||||
|
|
||||||
func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) {
|
func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
allErrors := []error{}
|
allErrors := []error{}
|
||||||
@@ -121,6 +129,16 @@ func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath
|
|||||||
return action, err
|
return action, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cachedActionTar returns the action's tree from the action cache, which only a remote action
|
||||||
|
// has an entry in.
|
||||||
|
func cachedActionTar(ctx context.Context, step actionStep, name, includePrefix string) (io.ReadCloser, error) {
|
||||||
|
remote, ok := step.(*stepActionRemote)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("action %q is a remote action but runs as %T", name, step)
|
||||||
|
}
|
||||||
|
return step.getRunContext().Config.ActionCache.GetTarArchive(ctx, remote.cacheDir, remote.resolvedSha, includePrefix)
|
||||||
|
}
|
||||||
|
|
||||||
func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actionPath, containerActionDir string) error {
|
func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actionPath, containerActionDir string) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
@@ -139,8 +157,7 @@ func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actio
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rc.Config != nil && rc.Config.ActionCache != nil {
|
if rc.Config != nil && rc.Config.ActionCache != nil {
|
||||||
raction := step.(*stepActionRemote)
|
ta, err := cachedActionTar(ctx, step, stepModel.Uses, "")
|
||||||
ta, err := rc.Config.ActionCache.GetTarArchive(ctx, raction.cacheDir, raction.resolvedSha, "")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -148,6 +165,8 @@ func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actio
|
|||||||
return rc.JobContainer.CopyTarStream(ctx, containerActionDirCopy, ta)
|
return rc.JobContainer.CopyTarStream(ctx, containerActionDirCopy, ta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer git.AcquireCloneLock(actionDir)()
|
||||||
|
|
||||||
if err := removeGitIgnore(ctx, actionDir); err != nil {
|
if err := removeGitIgnore(ctx, actionDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -197,7 +216,7 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
|
|||||||
if remoteAction == nil {
|
if remoteAction == nil {
|
||||||
location = containerActionDir
|
location = containerActionDir
|
||||||
}
|
}
|
||||||
return execAsDocker(ctx, step, actionName, location, remoteAction == nil)
|
return execAsDocker(ctx, step, actionName, actionDir, location, remoteAction == nil, stepStageMain)
|
||||||
case x.IsComposite():
|
case x.IsComposite():
|
||||||
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -264,10 +283,38 @@ func removeGitIgnore(ctx context.Context, directory string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: break out parts of function to reduce complexicity
|
// dockerActionImageTag derives the local docker image tag used when an action
|
||||||
|
// is built from a Dockerfile.
|
||||||
//
|
//
|
||||||
//nolint:gocyclo // function handles many cases
|
// For Gitea: a local action (`uses: ./` or `uses: ./path`) has an actionName
|
||||||
func execAsDocker(ctx context.Context, step actionStep, actionName, basedir string, localAction bool) error {
|
// that is the workspace-relative path of the action. That path is identical
|
||||||
|
// across repositories (e.g. "./" for a self-referencing action), so without
|
||||||
|
// namespacing, every repository's local docker action would build and reuse the
|
||||||
|
// same `act-dockeraction:latest` image on a shared docker daemon. A subsequent
|
||||||
|
// repository would then silently run the image built for an earlier one.
|
||||||
|
// Including the repository keeps the tag stable for caching within a repository
|
||||||
|
// while preventing cross-repository collisions.
|
||||||
|
// See https://gitea.com/gitea/runner/issues/1039.
|
||||||
|
func dockerActionImageTag(repository, actionName string, localAction bool) string {
|
||||||
|
name := actionName
|
||||||
|
if localAction {
|
||||||
|
name = path.Join(repository, actionName)
|
||||||
|
}
|
||||||
|
// The human-readable name is sanitized by collapsing every non-alphanumeric character to "-".
|
||||||
|
sanitized := regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(name, "-")
|
||||||
|
if localAction {
|
||||||
|
// For local actions a short hash of the raw repository and action path is appended so the tag stays unique per repository.
|
||||||
|
sum := sha256.Sum256([]byte(repository + "\x00" + actionName))
|
||||||
|
sanitized += "-" + hex.EncodeToString(sum[:])[:12]
|
||||||
|
}
|
||||||
|
// "-dockeraction" ensures that "./", "./test " won't get converted to "act-:latest", "act-test-:latest" which are invalid docker image names
|
||||||
|
image := fmt.Sprintf("%s-dockeraction:%s", sanitized, "latest")
|
||||||
|
image = "act-" + strings.TrimLeft(image, "-")
|
||||||
|
return strings.ToLower(image)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: break out parts of function to reduce complexicity
|
||||||
|
func execAsDocker(ctx context.Context, step actionStep, actionName, actionDir, basedir string, localAction bool, stage stepStage) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
action := step.getActionModel()
|
action := step.getActionModel()
|
||||||
@@ -280,18 +327,15 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, basedir stri
|
|||||||
// Apply forcePull only for prebuild docker images
|
// Apply forcePull only for prebuild docker images
|
||||||
forcePull = rc.Config.ForcePull
|
forcePull = rc.Config.ForcePull
|
||||||
} else {
|
} else {
|
||||||
// "-dockeraction" enshures that "./", "./test " won't get converted to "act-:latest", "act-test-:latest" which are invalid docker image names
|
image = dockerActionImageTag(step.getGithubContext(ctx).Repository, actionName, localAction)
|
||||||
image = fmt.Sprintf("%s-dockeraction:%s", regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(actionName, "-"), "latest")
|
|
||||||
image = "act-" + strings.TrimLeft(image, "-")
|
|
||||||
image = strings.ToLower(image)
|
|
||||||
contextDir, fileName := filepath.Split(filepath.Join(basedir, action.Runs.Image))
|
contextDir, fileName := filepath.Split(filepath.Join(basedir, action.Runs.Image))
|
||||||
|
|
||||||
anyArchExists, err := container.ImageExistsLocally(ctx, image, "any")
|
anyArchExists, err := ContainerImageExistsLocally(ctx, image, "any")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
correctArchExists, err := container.ImageExistsLocally(ctx, image, rc.Config.ContainerArchitecture)
|
correctArchExists, err := ContainerImageExistsLocally(ctx, image, rc.Config.ContainerArchitecture)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -316,20 +360,28 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, basedir stri
|
|||||||
}
|
}
|
||||||
defer buildContext.Close()
|
defer buildContext.Close()
|
||||||
} else if rc.Config.ActionCache != nil {
|
} else if rc.Config.ActionCache != nil {
|
||||||
rstep := step.(*stepActionRemote)
|
buildContext, err = cachedActionTar(ctx, step, actionName, contextDir)
|
||||||
buildContext, err = rc.Config.ActionCache.GetTarArchive(ctx, rstep.cacheDir, rstep.resolvedSha, contextDir)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer buildContext.Close()
|
defer buildContext.Close()
|
||||||
}
|
}
|
||||||
prepImage = container.NewDockerBuildExecutor(container.NewDockerBuildExecutorInput{
|
prepImage = ContainerNewDockerBuildExecutor(container.NewDockerBuildExecutorInput{
|
||||||
ContextDir: contextDir,
|
ContextDir: contextDir,
|
||||||
Dockerfile: fileName,
|
Dockerfile: fileName,
|
||||||
ImageTag: image,
|
ImageTag: image,
|
||||||
BuildContext: buildContext,
|
BuildContext: buildContext,
|
||||||
Platform: rc.Config.ContainerArchitecture,
|
Platform: rc.Config.ContainerArchitecture,
|
||||||
|
BuildArgs: rc.proxyBuildArgs(),
|
||||||
})
|
})
|
||||||
|
if buildContext == nil {
|
||||||
|
// Held across the whole build: the daemon drains contextDir lazily.
|
||||||
|
inner := prepImage
|
||||||
|
prepImage = func(ctx context.Context) error {
|
||||||
|
defer git.AcquireCloneLock(actionDir)()
|
||||||
|
return inner(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Debugf("image '%s' for architecture '%s' already exists", image, rc.Config.ContainerArchitecture)
|
logger.Debugf("image '%s' for architecture '%s' already exists", image, rc.Config.ContainerArchitecture)
|
||||||
}
|
}
|
||||||
@@ -343,16 +395,9 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, basedir stri
|
|||||||
cmd = action.Runs.Args
|
cmd = action.Runs.Args
|
||||||
evalDockerArgs(ctx, step, action, &cmd)
|
evalDockerArgs(ctx, step, action, &cmd)
|
||||||
}
|
}
|
||||||
entrypoint := strings.Fields(eval.Interpolate(ctx, step.getStepModel().With["entrypoint"]))
|
entrypoint, err := dockerEntrypoint(ctx, step, eval, stage)
|
||||||
if len(entrypoint) == 0 {
|
if err != nil {
|
||||||
if action.Runs.Entrypoint != "" {
|
return err
|
||||||
entrypoint, err = shellquote.Split(action.Runs.Entrypoint)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
entrypoint = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
stepContainer := newStepContainer(ctx, step, image, cmd, entrypoint)
|
stepContainer := newStepContainer(ctx, step, image, cmd, entrypoint)
|
||||||
return common.NewPipelineExecutor(
|
return common.NewPipelineExecutor(
|
||||||
@@ -362,10 +407,34 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, basedir stri
|
|||||||
stepContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
|
stepContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
|
||||||
stepContainer.Start(true),
|
stepContainer.Start(true),
|
||||||
).Finally(
|
).Finally(
|
||||||
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers && !rc.Config.AutoRemove),
|
||||||
).Finally(stepContainer.Close())(ctx)
|
).Finally(stepContainer.Close())(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dockerEntrypoint returns the entrypoint the action's image runs with for the given
|
||||||
|
// stage. Only the main stage honours the `entrypoint` input.
|
||||||
|
func dockerEntrypoint(ctx context.Context, step actionStep, eval ExpressionEvaluator, stage stepStage) ([]string, error) {
|
||||||
|
runs := step.getActionModel().Runs
|
||||||
|
|
||||||
|
var entrypoint string
|
||||||
|
switch stage {
|
||||||
|
case stepStagePre:
|
||||||
|
entrypoint = runs.PreEntrypoint
|
||||||
|
case stepStagePost:
|
||||||
|
entrypoint = runs.PostEntrypoint
|
||||||
|
default:
|
||||||
|
if fields := strings.Fields(eval.Interpolate(ctx, step.getStepModel().With["entrypoint"])); len(fields) > 0 {
|
||||||
|
return fields, nil
|
||||||
|
}
|
||||||
|
entrypoint = runs.Entrypoint
|
||||||
|
}
|
||||||
|
|
||||||
|
if entrypoint == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return shellquote.Split(entrypoint)
|
||||||
|
}
|
||||||
|
|
||||||
func evalDockerArgs(ctx context.Context, step step, action *model.Action, cmd *[]string) {
|
func evalDockerArgs(ctx context.Context, step step, action *model.Action, cmd *[]string) {
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
stepModel := step.getStepModel()
|
stepModel := step.getStepModel()
|
||||||
@@ -412,24 +481,19 @@ func newStepContainer(ctx context.Context, step step, image string, cmd, entrypo
|
|||||||
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache"))
|
envList = append(envList, rc.runnerEnv(ctx)...)
|
||||||
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
|
|
||||||
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx)))
|
|
||||||
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
|
|
||||||
|
|
||||||
binds, mounts := rc.GetBindsAndMounts()
|
binds, mounts := rc.GetBindsAndMounts()
|
||||||
networkMode := "container:" + rc.jobContainerName()
|
networkMode := "container:" + rc.jobContainerName()
|
||||||
if rc.IsHostEnv(ctx) {
|
if rc.IsHostEnv(ctx) {
|
||||||
networkMode = "default"
|
networkMode = "default"
|
||||||
}
|
}
|
||||||
stepContainer := container.NewContainer(&container.NewContainerInput{
|
stepContainer := ContainerNewContainer(&container.NewContainerInput{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
Entrypoint: entrypoint,
|
Entrypoint: entrypoint,
|
||||||
WorkingDir: rc.JobContainer.ToContainerPath(rc.Config.Workdir),
|
WorkingDir: rc.JobContainer.ToContainerPath(rc.Config.Workdir),
|
||||||
Image: image,
|
Image: image,
|
||||||
Username: rc.Config.Secrets["DOCKER_USERNAME"],
|
Name: createContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID),
|
||||||
Password: rc.Config.Secrets["DOCKER_PASSWORD"],
|
|
||||||
Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID),
|
|
||||||
Env: envList,
|
Env: envList,
|
||||||
Mounts: mounts,
|
Mounts: mounts,
|
||||||
NetworkMode: networkMode,
|
NetworkMode: networkMode,
|
||||||
@@ -441,7 +505,8 @@ func newStepContainer(ctx context.Context, step step, image string, cmd, entrypo
|
|||||||
Platform: rc.Config.ContainerArchitecture,
|
Platform: rc.Config.ContainerArchitecture,
|
||||||
Options: rc.Config.ContainerOptions,
|
Options: rc.Config.ContainerOptions,
|
||||||
AutoRemove: rc.Config.AutoRemove,
|
AutoRemove: rc.Config.AutoRemove,
|
||||||
ValidVolumes: rc.Config.ValidVolumes,
|
ValidVolumes: rc.validVolumes(),
|
||||||
|
AllocatePTY: rc.Config.AllocatePTY,
|
||||||
})
|
})
|
||||||
return stepContainer
|
return stepContainer
|
||||||
}
|
}
|
||||||
@@ -517,44 +582,57 @@ func hasPreStep(step actionStep) common.Conditional {
|
|||||||
return action.Runs.Using.IsComposite() ||
|
return action.Runs.Using.IsComposite() ||
|
||||||
(action.Runs.Using.IsNode() &&
|
(action.Runs.Using.IsNode() &&
|
||||||
action.Runs.Pre != "") ||
|
action.Runs.Pre != "") ||
|
||||||
|
(action.Runs.Using.IsDocker() &&
|
||||||
|
action.Runs.PreEntrypoint != "") ||
|
||||||
(action.Runs.Using == model.ActionRunsUsingGo &&
|
(action.Runs.Using == model.ActionRunsUsingGo &&
|
||||||
action.Runs.Pre != "")
|
action.Runs.Pre != "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// actionStagePaths resolves where a step's action lives and where the job container sees
|
||||||
|
// it, for the pre and post stage.
|
||||||
|
func actionStagePaths(step actionStep) (actionDir, actionPath, actionName, containerActionDir string) {
|
||||||
|
rc := step.getRunContext()
|
||||||
|
stepModel := step.getStepModel()
|
||||||
|
|
||||||
|
if sar, ok := step.(*stepActionRemote); ok {
|
||||||
|
actionDir = sar.actionDir()
|
||||||
|
actionPath = newRemoteAction(stepModel.Uses).Path
|
||||||
|
} else {
|
||||||
|
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
||||||
|
}
|
||||||
|
|
||||||
|
actionName, containerActionDir = getContainerActionPaths(stepModel, path.Join(actionDir, actionPath), rc)
|
||||||
|
return actionDir, actionPath, actionName, containerActionDir
|
||||||
|
}
|
||||||
|
|
||||||
|
// execDockerActionStage runs a docker action's image for its pre or post stage.
|
||||||
|
func execDockerActionStage(ctx context.Context, step actionStep, stage stepStage) error {
|
||||||
|
actionDir, actionPath, actionName, containerActionDir := actionStagePaths(step)
|
||||||
|
|
||||||
|
_, remote := step.(*stepActionRemote)
|
||||||
|
location := containerActionDir
|
||||||
|
if remote {
|
||||||
|
location = path.Join(actionDir, actionPath)
|
||||||
|
}
|
||||||
|
return execAsDocker(ctx, step, actionName, actionDir, location, !remote, stage)
|
||||||
|
}
|
||||||
|
|
||||||
func runPreStep(step actionStep) common.Executor {
|
func runPreStep(step actionStep) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
logger.Debugf("run pre step for '%s'", step.getStepModel())
|
logger.Debugf("run pre step for '%s'", step.getStepModel())
|
||||||
|
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
stepModel := step.getStepModel()
|
|
||||||
action := step.getActionModel()
|
action := step.getActionModel()
|
||||||
|
|
||||||
|
actionDir, actionPath, _, containerActionDir := actionStagePaths(step)
|
||||||
|
|
||||||
x := action.Runs.Using
|
x := action.Runs.Using
|
||||||
switch {
|
switch {
|
||||||
case x.IsNode():
|
case x.IsNode():
|
||||||
// defaults in pre steps were missing, however provided inputs are available
|
// defaults in pre steps were missing, however provided inputs are available
|
||||||
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
||||||
// todo: refactor into step
|
|
||||||
var actionDir string
|
|
||||||
var actionPath string
|
|
||||||
if _, ok := step.(*stepActionRemote); ok {
|
|
||||||
actionPath = newRemoteAction(stepModel.Uses).Path
|
|
||||||
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), stepModel.UsesHash())
|
|
||||||
} else {
|
|
||||||
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
||||||
actionPath = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var actionLocation string
|
|
||||||
if actionPath != "" {
|
|
||||||
actionLocation = path.Join(actionDir, actionPath)
|
|
||||||
} else {
|
|
||||||
actionLocation = actionDir
|
|
||||||
}
|
|
||||||
|
|
||||||
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
||||||
|
|
||||||
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -567,6 +645,12 @@ func runPreStep(step actionStep) common.Executor {
|
|||||||
|
|
||||||
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
||||||
|
|
||||||
|
case x.IsDocker():
|
||||||
|
// defaults in pre steps were missing, however provided inputs are available
|
||||||
|
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
||||||
|
|
||||||
|
return execDockerActionStage(ctx, step, stepStagePre)
|
||||||
|
|
||||||
case x.IsComposite():
|
case x.IsComposite():
|
||||||
if step.getCompositeSteps() == nil {
|
if step.getCompositeSteps() == nil {
|
||||||
step.getCompositeRunContext(ctx)
|
step.getCompositeRunContext(ctx)
|
||||||
@@ -580,25 +664,6 @@ func runPreStep(step actionStep) common.Executor {
|
|||||||
case x == model.ActionRunsUsingGo:
|
case x == model.ActionRunsUsingGo:
|
||||||
// defaults in pre steps were missing, however provided inputs are available
|
// defaults in pre steps were missing, however provided inputs are available
|
||||||
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
||||||
// todo: refactor into step
|
|
||||||
var actionDir string
|
|
||||||
var actionPath string
|
|
||||||
if _, ok := step.(*stepActionRemote); ok {
|
|
||||||
actionPath = newRemoteAction(stepModel.Uses).Path
|
|
||||||
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), stepModel.UsesHash())
|
|
||||||
} else {
|
|
||||||
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
||||||
actionPath = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var actionLocation string
|
|
||||||
if actionPath != "" {
|
|
||||||
actionLocation = path.Join(actionDir, actionPath)
|
|
||||||
} else {
|
|
||||||
actionLocation = actionDir
|
|
||||||
}
|
|
||||||
|
|
||||||
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
||||||
|
|
||||||
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -651,6 +716,8 @@ func hasPostStep(step actionStep) common.Conditional {
|
|||||||
return action.Runs.Using.IsComposite() ||
|
return action.Runs.Using.IsComposite() ||
|
||||||
(action.Runs.Using.IsNode() &&
|
(action.Runs.Using.IsNode() &&
|
||||||
action.Runs.Post != "") ||
|
action.Runs.Post != "") ||
|
||||||
|
(action.Runs.Using.IsDocker() &&
|
||||||
|
action.Runs.PostEntrypoint != "") ||
|
||||||
(action.Runs.Using == model.ActionRunsUsingGo &&
|
(action.Runs.Using == model.ActionRunsUsingGo &&
|
||||||
action.Runs.Post != "")
|
action.Runs.Post != "")
|
||||||
}
|
}
|
||||||
@@ -662,28 +729,9 @@ func runPostStep(step actionStep) common.Executor {
|
|||||||
logger.Debugf("run post step for '%s'", step.getStepModel())
|
logger.Debugf("run post step for '%s'", step.getStepModel())
|
||||||
|
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
stepModel := step.getStepModel()
|
|
||||||
action := step.getActionModel()
|
action := step.getActionModel()
|
||||||
|
|
||||||
// todo: refactor into step
|
actionDir, actionPath, _, containerActionDir := actionStagePaths(step)
|
||||||
var actionDir string
|
|
||||||
var actionPath string
|
|
||||||
if _, ok := step.(*stepActionRemote); ok {
|
|
||||||
actionPath = newRemoteAction(stepModel.Uses).Path
|
|
||||||
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), stepModel.UsesHash())
|
|
||||||
} else {
|
|
||||||
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
||||||
actionPath = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var actionLocation string
|
|
||||||
if actionPath != "" {
|
|
||||||
actionLocation = path.Join(actionDir, actionPath)
|
|
||||||
} else {
|
|
||||||
actionLocation = actionDir
|
|
||||||
}
|
|
||||||
|
|
||||||
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
||||||
|
|
||||||
x := action.Runs.Using
|
x := action.Runs.Using
|
||||||
switch {
|
switch {
|
||||||
@@ -698,6 +746,11 @@ func runPostStep(step actionStep) common.Executor {
|
|||||||
|
|
||||||
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
||||||
|
|
||||||
|
case x.IsDocker():
|
||||||
|
populateEnvsFromSavedState(step.getEnv(), step, rc)
|
||||||
|
|
||||||
|
return execDockerActionStage(ctx, step, stepStagePost)
|
||||||
|
|
||||||
case x.IsComposite():
|
case x.IsComposite():
|
||||||
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2024 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package runner
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
git "github.com/go-git/go-git/v5"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GoGitActionCacheOfflineMode struct {
|
|
||||||
Parent GoGitActionCache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c GoGitActionCacheOfflineMode) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) {
|
|
||||||
sha, fetchErr := c.Parent.Fetch(ctx, cacheDir, url, ref, token)
|
|
||||||
gitPath := path.Join(c.Parent.Path, safeFilename(cacheDir)+".git")
|
|
||||||
gogitrepo, err := git.PlainOpen(gitPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", fetchErr
|
|
||||||
}
|
|
||||||
refName := plumbing.ReferenceName("refs/action-cache-offline/" + ref)
|
|
||||||
r, err := gogitrepo.Reference(refName, true)
|
|
||||||
if fetchErr == nil {
|
|
||||||
if err != nil || sha != r.Hash().String() {
|
|
||||||
if err == nil {
|
|
||||||
refName = r.Name()
|
|
||||||
}
|
|
||||||
ref := plumbing.NewHashReference(refName, plumbing.NewHash(sha))
|
|
||||||
_ = gogitrepo.Storer.SetReference(ref)
|
|
||||||
}
|
|
||||||
} else if err == nil {
|
|
||||||
return r.Hash().String(), nil
|
|
||||||
}
|
|
||||||
return sha, fetchErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c GoGitActionCacheOfflineMode) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) {
|
|
||||||
return c.Parent.GetTarArchive(ctx, cacheDir, sha, includePrefix)
|
|
||||||
}
|
|
||||||
@@ -8,64 +8,139 @@ import (
|
|||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestActionCache(t *testing.T) {
|
func runGit(t *testing.T, dir string, args ...string) {
|
||||||
if testing.Short() {
|
t.Helper()
|
||||||
t.Skip("skipping integration test")
|
if dir != "" {
|
||||||
|
args = append([]string{"-C", dir}, args...)
|
||||||
}
|
}
|
||||||
|
cmd := exec.Command("git", args...)
|
||||||
|
// Fixed identity and host-config isolation so commits succeed offline regardless of the
|
||||||
|
// host's git config (mirrors gitCmd in act/common/git).
|
||||||
|
cmd.Env = append(os.Environ(),
|
||||||
|
"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com",
|
||||||
|
"GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com",
|
||||||
|
"GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null",
|
||||||
|
)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestShortShaActionRejected verifies a `uses` ref that is a shortened commit SHA is rejected
|
||||||
|
// with a clear error. The action is resolved from a local repo (via DefaultActionInstance) so
|
||||||
|
// this runs offline.
|
||||||
|
func TestShortShaActionRejected(t *testing.T) {
|
||||||
|
// a local "remote" action repo at <root>/actions/hello-world-docker-action
|
||||||
|
actionRoot := t.TempDir()
|
||||||
|
repo := filepath.Join(actionRoot, "actions", "hello-world-docker-action")
|
||||||
|
require.NoError(t, os.MkdirAll(repo, 0o755))
|
||||||
|
runGit(t, "", "init", "--initial-branch=main", repo)
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(repo, "action.yml"),
|
||||||
|
[]byte("name: hello\nruns:\n using: node24\n main: index.js\n"), 0o644))
|
||||||
|
runGit(t, repo, "add", ".")
|
||||||
|
runGit(t, repo, "commit", "-m", "initial")
|
||||||
|
out, err := exec.Command("git", "-C", repo, "rev-parse", "HEAD").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
shortSha := strings.TrimSpace(string(out))[:7]
|
||||||
|
|
||||||
|
// a workflow that uses the action at the short SHA
|
||||||
|
wfDir := filepath.Join(t.TempDir(), "wf")
|
||||||
|
require.NoError(t, os.MkdirAll(wfDir, 0o755))
|
||||||
|
wf := fmt.Sprintf("on: push\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/hello-world-docker-action@%s\n", shortSha)
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(wfDir, "push.yml"), []byte(wf), 0o644))
|
||||||
|
|
||||||
|
runner, err := New(&Config{
|
||||||
|
Workdir: wfDir,
|
||||||
|
EventName: "push",
|
||||||
|
Platforms: map[string]string{"ubuntu-latest": baseImage},
|
||||||
|
GitHubInstance: "github.com",
|
||||||
|
DefaultActionInstance: actionRoot,
|
||||||
|
ContainerMaxLifetime: time.Hour,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
planner, err := model.NewWorkflowPlanner(wfDir, true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
plan, err := planner.PlanEvent("push")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = runner.NewPlanExecutor(plan)(common.WithDryrun(context.Background(), true))
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "shortened version of a commit SHA")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestActionCache(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Build a local bare repo with a `js` action dir so this runs offline (formerly cloned
|
||||||
|
// github.com/nektos/act-test-actions over the network). allowAnySHA1InWant lets the
|
||||||
|
// "Fetch Sha" case fetch a commit hash directly.
|
||||||
|
remoteDir := t.TempDir()
|
||||||
|
runGit(t, "", "init", "--bare", "--initial-branch=main", remoteDir)
|
||||||
|
runGit(t, remoteDir, "config", "uploadpack.allowAnySHA1InWant", "true")
|
||||||
|
|
||||||
|
workDir := t.TempDir()
|
||||||
|
runGit(t, "", "clone", remoteDir, workDir)
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Join(workDir, "js"), 0o755))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(workDir, "js", "action.yml"),
|
||||||
|
[]byte("name: js\nruns:\n using: node24\n main: index.js\n"), 0o644))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(workDir, "js", "index.js"),
|
||||||
|
[]byte("console.log('hello');\n"), 0o644))
|
||||||
|
runGit(t, workDir, "add", ".")
|
||||||
|
runGit(t, workDir, "commit", "-m", "initial")
|
||||||
|
runGit(t, workDir, "push", "-u", "origin", "main")
|
||||||
|
|
||||||
|
out, err := exec.Command("git", "-C", workDir, "rev-parse", "main").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
fullSha := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
cache := &GoGitActionCache{
|
cache := &GoGitActionCache{
|
||||||
Path: t.TempDir(),
|
Path: t.TempDir(),
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
cacheDir := "local/act-test-actions"
|
||||||
cacheDir := "nektos/act-test-actions"
|
|
||||||
repo := "https://github.com/nektos/act-test-actions"
|
|
||||||
refs := []struct {
|
refs := []struct {
|
||||||
Name string
|
Name string
|
||||||
CacheDir string
|
Ref string
|
||||||
Repo string
|
|
||||||
Ref string
|
|
||||||
}{
|
}{
|
||||||
{
|
{Name: "Fetch Branch Name", Ref: "main"},
|
||||||
Name: "Fetch Branch Name",
|
{Name: "Fetch Branch Name Absolutely", Ref: "refs/heads/main"},
|
||||||
CacheDir: cacheDir,
|
{Name: "Fetch HEAD", Ref: "HEAD"},
|
||||||
Repo: repo,
|
{Name: "Fetch Sha", Ref: fullSha},
|
||||||
Ref: "main",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Fetch Branch Name Absolutely",
|
|
||||||
CacheDir: cacheDir,
|
|
||||||
Repo: repo,
|
|
||||||
Ref: "refs/heads/main",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Fetch HEAD",
|
|
||||||
CacheDir: cacheDir,
|
|
||||||
Repo: repo,
|
|
||||||
Ref: "HEAD",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Fetch Sha",
|
|
||||||
CacheDir: cacheDir,
|
|
||||||
Repo: repo,
|
|
||||||
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
for _, c := range refs {
|
for _, c := range refs {
|
||||||
t.Run(c.Name, func(t *testing.T) {
|
t.Run(c.Name, func(t *testing.T) {
|
||||||
sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
|
sha, err := cache.Fetch(ctx, cacheDir, remoteDir, c.Ref, "")
|
||||||
if !a.NoError(err) || !a.NotEmpty(sha) { //nolint:testifylint // pre-existing issue from nektos/act
|
if !a.NoError(err) || !a.NotEmpty(sha) { //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
atar, err := cache.GetTarArchive(ctx, c.CacheDir, sha, "js")
|
atar, err := cache.GetTarArchive(ctx, cacheDir, sha, "js")
|
||||||
if !a.NoError(err) || !a.NotEmpty(atar) { //nolint:testifylint // pre-existing issue from nektos/act
|
// NotNil, not NotEmpty: atar is a live io.PipeReader whose producer goroutine is
|
||||||
|
// writing concurrently; NotEmpty deep-reflects over its internals and races.
|
||||||
|
if !a.NoError(err) || !a.NotNil(atar) { //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// GetTarArchive streams from a background goroutine walking the shared repo.
|
||||||
|
// Drain and close so it finishes before the next subtest fetches into the same
|
||||||
|
// repo; otherwise the lingering walk races with that fetch.
|
||||||
|
defer func() {
|
||||||
|
_, _ = io.Copy(io.Discard, atar)
|
||||||
|
_ = atar.Close()
|
||||||
|
}()
|
||||||
mytar := tar.NewReader(atar)
|
mytar := tar.NewReader(atar)
|
||||||
th, err := mytar.Next()
|
th, err := mytar.Next()
|
||||||
if !a.NoError(err) || !a.NotEqual(0, th.Size) { //nolint:testifylint // pre-existing issue from nektos/act
|
if !a.NoError(err) || !a.NotEqual(0, th.Size) { //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step actionStep) map[string]string {
|
func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step actionStep) map[string]string {
|
||||||
@@ -85,6 +86,19 @@ func newCompositeRunContext(ctx context.Context, parent *RunContext, step action
|
|||||||
return compositerc
|
return compositerc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appendUniqueMasks appends the masks from src to dst, skipping any mask that
|
||||||
|
// is already present in dst. This prevents the parent RunContext's Masks slice
|
||||||
|
// from growing exponentially when composite actions are nested or repeated,
|
||||||
|
// since each composite RunContext is seeded with its parent's masks.
|
||||||
|
func appendUniqueMasks(dst, src []string) []string {
|
||||||
|
for _, m := range src {
|
||||||
|
if !slices.Contains(dst, m) {
|
||||||
|
dst = append(dst, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
|
|
||||||
func execAsComposite(step actionStep) common.Executor {
|
func execAsComposite(step actionStep) common.Executor {
|
||||||
rc := step.getRunContext()
|
rc := step.getRunContext()
|
||||||
action := step.getActionModel()
|
action := step.getActionModel()
|
||||||
@@ -110,7 +124,11 @@ func execAsComposite(step actionStep) common.Executor {
|
|||||||
}, eval.Interpolate(ctx, output.Value))
|
}, eval.Interpolate(ctx, output.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.Masks = append(rc.Masks, compositeRC.Masks...)
|
// compositeRC.Masks is seeded with rc.Masks (see newCompositeRunContext)
|
||||||
|
// and may have additional masks appended while the composite action runs.
|
||||||
|
// Only append masks that are not already present, otherwise nested or
|
||||||
|
// repeated composite actions grow rc.Masks exponentially.
|
||||||
|
rc.Masks = appendUniqueMasks(rc.Masks, compositeRC.Masks)
|
||||||
rc.ExtraPath = compositeRC.ExtraPath
|
rc.ExtraPath = compositeRC.ExtraPath
|
||||||
// compositeRC.Env is dirty, contains INPUT_ and merged step env, only rely on compositeRC.GlobalEnv
|
// compositeRC.Env is dirty, contains INPUT_ and merged step env, only rely on compositeRC.GlobalEnv
|
||||||
mergeIntoMap := mergeIntoMapCaseSensitive
|
mergeIntoMap := mergeIntoMapCaseSensitive
|
||||||
|
|||||||
70
act/runner/action_composite_test.go
Normal file
70
act/runner/action_composite_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAppendUniqueMasks(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
dst []string
|
||||||
|
src []string
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "appends new masks",
|
||||||
|
dst: []string{"a"},
|
||||||
|
src: []string{"b", "c"},
|
||||||
|
want: []string{"a", "b", "c"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "skips masks already present",
|
||||||
|
dst: []string{"a", "b"},
|
||||||
|
src: []string{"a", "b"},
|
||||||
|
want: []string{"a", "b"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "deduplicates within src",
|
||||||
|
dst: []string{"a"},
|
||||||
|
src: []string{"b", "b", "a"},
|
||||||
|
want: []string{"a", "b"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty src leaves dst unchanged",
|
||||||
|
dst: []string{"a"},
|
||||||
|
src: nil,
|
||||||
|
want: []string{"a"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
assert.Equal(t, tt.want, appendUniqueMasks(tt.dst, tt.src))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAppendUniqueMasksNoExponentialGrowth reproduces the exponential growth of
|
||||||
|
// the parent's Masks slice observed with nested/repeated composite actions. A
|
||||||
|
// composite RunContext is seeded with its parent's masks and the whole seeded
|
||||||
|
// slice was previously appended back into the parent, doubling its length on
|
||||||
|
// every composite action.
|
||||||
|
func TestAppendUniqueMasksNoExponentialGrowth(t *testing.T) {
|
||||||
|
parentMasks := []string{"secret"}
|
||||||
|
|
||||||
|
for range 20 {
|
||||||
|
// compositeRC.Masks starts as a copy of the parent's masks (it is
|
||||||
|
// seeded with parent.Masks in newCompositeRunContext).
|
||||||
|
compositeMasks := make([]string, len(parentMasks))
|
||||||
|
copy(compositeMasks, parentMasks)
|
||||||
|
|
||||||
|
parentMasks = appendUniqueMasks(parentMasks, compositeMasks)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"secret"}, parentMasks)
|
||||||
|
}
|
||||||
@@ -9,12 +9,18 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type closerMock struct {
|
type closerMock struct {
|
||||||
@@ -137,7 +143,7 @@ runs:
|
|||||||
|
|
||||||
action, err := readActionImpl(context.Background(), tt.step, "actionDir", "actionPath", readFile, writeFile)
|
action, err := readActionImpl(context.Background(), tt.step, "actionDir", "actionPath", readFile, writeFile)
|
||||||
|
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.expected, action)
|
assert.Equal(t, tt.expected, action)
|
||||||
|
|
||||||
closerMock.AssertExpectations(t)
|
closerMock.AssertExpectations(t)
|
||||||
@@ -145,6 +151,44 @@ runs:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// With AutoRemove the daemon reaps the container on exit, so act must not remove it afterwards.
|
||||||
|
func TestExecAsDockerAutoRemove(t *testing.T) {
|
||||||
|
orig := ContainerNewContainer
|
||||||
|
defer func() { ContainerNewContainer = orig }()
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
autoRemove bool
|
||||||
|
removes int
|
||||||
|
}{
|
||||||
|
{false, 2}, // stale + post-run
|
||||||
|
{true, 1}, // post-run skipped
|
||||||
|
} {
|
||||||
|
cm := &containerMock{}
|
||||||
|
ContainerNewContainer = func(*container.NewContainerInput) container.ExecutionsEnvironment { return cm }
|
||||||
|
|
||||||
|
step := &stepActionRemote{
|
||||||
|
Step: &model.Step{ID: "1", Uses: "org/action@v1"},
|
||||||
|
RunContext: &RunContext{
|
||||||
|
Config: &Config{AutoRemove: tc.autoRemove},
|
||||||
|
Run: &model.Run{JobID: "1", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"1": {}}}},
|
||||||
|
JobContainer: cm,
|
||||||
|
},
|
||||||
|
action: &model.Action{Runs: model.ActionRuns{Using: "docker", Image: "docker://node:14"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
removes := 0
|
||||||
|
cm.On("Pull", false).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Remove").Return(func(context.Context) error { removes++; return nil })
|
||||||
|
cm.On("Create", []string(nil), []string(nil)).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Start", true).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Close").Return(func(context.Context) error { return nil })
|
||||||
|
|
||||||
|
require.NoError(t, execAsDocker(context.Background(), step, "action", t.TempDir(), t.TempDir(), false, stepStageMain))
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
assert.Equal(t, tc.removes, removes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestActionRunner(t *testing.T) {
|
func TestActionRunner(t *testing.T) {
|
||||||
table := []struct {
|
table := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -247,8 +291,336 @@ func TestActionRunner(t *testing.T) {
|
|||||||
|
|
||||||
err := runActionImpl(tt.step, "dir", newRemoteAction("org/repo/path@ref"))(ctx)
|
err := runActionImpl(tt.step, "dir", newRemoteAction("org/repo/path@ref"))(ctx)
|
||||||
|
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
cm.AssertExpectations(t)
|
cm.AssertExpectations(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewStepContainerDoesNotUseDockerSecrets(t *testing.T) {
|
||||||
|
cm := &containerMock{}
|
||||||
|
|
||||||
|
var captured *container.NewContainerInput
|
||||||
|
origContainerNewContainer := ContainerNewContainer
|
||||||
|
ContainerNewContainer = func(input *container.NewContainerInput) container.ExecutionsEnvironment {
|
||||||
|
captured = input
|
||||||
|
return cm
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
ContainerNewContainer = origContainerNewContainer
|
||||||
|
}()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
rc := &RunContext{
|
||||||
|
Name: "job",
|
||||||
|
Config: &Config{
|
||||||
|
Secrets: map[string]string{
|
||||||
|
"DOCKER_USERNAME": "docker-user",
|
||||||
|
"DOCKER_PASSWORD": "docker-password",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Run: &model.Run{
|
||||||
|
JobID: "job",
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Name: "test",
|
||||||
|
Jobs: map[string]*model.Job{
|
||||||
|
"job": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JobContainer: cm,
|
||||||
|
StepResults: map[string]*model.StepResult{},
|
||||||
|
}
|
||||||
|
env := map[string]string{}
|
||||||
|
step := &stepMock{}
|
||||||
|
step.On("getRunContext").Return(rc)
|
||||||
|
step.On("getStepModel").Return(&model.Step{ID: "action"})
|
||||||
|
step.On("getEnv").Return(&env)
|
||||||
|
|
||||||
|
_ = newStepContainer(ctx, step, "registry.example.com/action:tag", nil, nil)
|
||||||
|
|
||||||
|
// DOCKER_USERNAME/DOCKER_PASSWORD should not be injected as pull credentials for docker action containers.
|
||||||
|
assert.Empty(t, captured.Username)
|
||||||
|
assert.Empty(t, captured.Password)
|
||||||
|
step.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaybeCopyToActionDirHoldsCloneLock(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
actionDir := t.TempDir()
|
||||||
|
|
||||||
|
releaseCopy := make(chan struct{})
|
||||||
|
release := sync.OnceFunc(func() { close(releaseCopy) })
|
||||||
|
defer release()
|
||||||
|
|
||||||
|
copyEntered := make(chan struct{})
|
||||||
|
|
||||||
|
cm := &containerMock{}
|
||||||
|
cm.On("CopyDir", "/var/run/act/actions/", actionDir+"/", false).Return(func(ctx context.Context) error {
|
||||||
|
close(copyEntered)
|
||||||
|
<-releaseCopy
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
step := &stepActionRemote{
|
||||||
|
Step: &model.Step{Uses: "remote/action@v1"},
|
||||||
|
RunContext: &RunContext{
|
||||||
|
Config: &Config{},
|
||||||
|
JobContainer: cm,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
copyDone := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
copyDone <- maybeCopyToActionDir(ctx, step, actionDir, "", "/var/run/act/actions/")
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-copyEntered:
|
||||||
|
case err := <-copyDone:
|
||||||
|
t.Fatalf("maybeCopyToActionDir returned before CopyDir was entered: %v", err)
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("CopyDir was not entered within 1 second")
|
||||||
|
}
|
||||||
|
|
||||||
|
peerAcquired := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
unlock := git.AcquireCloneLock(actionDir)
|
||||||
|
close(peerAcquired)
|
||||||
|
unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-peerAcquired:
|
||||||
|
t.Fatal("peer AcquireCloneLock returned while CopyDir was running")
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
|
||||||
|
release()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-copyDone:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("maybeCopyToActionDir returned error: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("maybeCopyToActionDir did not return after CopyDir was unblocked")
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-peerAcquired:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("peer AcquireCloneLock did not proceed after lock released")
|
||||||
|
}
|
||||||
|
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecAsDockerHoldsCloneLockForRemoteUncached(t *testing.T) {
|
||||||
|
actionDir := t.TempDir()
|
||||||
|
|
||||||
|
unlockOnce := sync.OnceFunc(git.AcquireCloneLock(actionDir))
|
||||||
|
defer unlockOnce()
|
||||||
|
|
||||||
|
innerEntered := make(chan struct{})
|
||||||
|
releaseInner := make(chan struct{})
|
||||||
|
releaseOnce := sync.OnceFunc(func() { close(releaseInner) })
|
||||||
|
defer releaseOnce()
|
||||||
|
|
||||||
|
origImageExists := ContainerImageExistsLocally
|
||||||
|
ContainerImageExistsLocally = func(_ context.Context, _, _ string) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
defer func() { ContainerImageExistsLocally = origImageExists }()
|
||||||
|
|
||||||
|
origBuildExec := ContainerNewDockerBuildExecutor
|
||||||
|
ContainerNewDockerBuildExecutor = func(_ container.NewDockerBuildExecutorInput) common.Executor {
|
||||||
|
return func(_ context.Context) error {
|
||||||
|
close(innerEntered)
|
||||||
|
<-releaseInner
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer func() { ContainerNewDockerBuildExecutor = origBuildExec }()
|
||||||
|
|
||||||
|
step := &stepActionRemote{
|
||||||
|
Step: &model.Step{ID: "1", Uses: "remote/action@v1", With: map[string]string{}},
|
||||||
|
RunContext: &RunContext{
|
||||||
|
Config: &Config{},
|
||||||
|
Run: &model.Run{
|
||||||
|
JobID: "1",
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Name: "wf",
|
||||||
|
Jobs: map[string]*model.Job{"1": {}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JobContainer: &containerMock{},
|
||||||
|
},
|
||||||
|
action: &model.Action{Runs: model.ActionRuns{Using: "docker", Image: "Dockerfile"}},
|
||||||
|
env: map[string]string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- execAsDocker(ctx, step, "test-action", actionDir, actionDir, false, stepStageMain) }()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-innerEntered:
|
||||||
|
t.Fatal("inner build executor ran before clone lock was released")
|
||||||
|
case err := <-done:
|
||||||
|
t.Fatalf("execAsDocker returned before inner was entered: %v", err)
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
|
||||||
|
unlockOnce()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-innerEntered:
|
||||||
|
case err := <-done:
|
||||||
|
t.Fatalf("execAsDocker returned without entering inner: %v", err)
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("inner build executor not entered after lock released")
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
releaseOnce()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("execAsDocker did not return after inner was released and ctx was canceled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDockerActionImageTag(t *testing.T) {
|
||||||
|
// Remote actions already carry a unique, ref-scoped actionName (the uses
|
||||||
|
// hash), so the tag must be left untouched for backwards compatibility.
|
||||||
|
assert.Equal(t,
|
||||||
|
"act-abc123-dockeraction:latest",
|
||||||
|
dockerActionImageTag("owner/repo", "abc123", false),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Local actions keep a human-readable, repository-namespaced prefix and gain a short hash suffix that makes the tag unique per (repository, actionName).
|
||||||
|
// See https://gitea.com/gitea/runner/issues/1039.
|
||||||
|
assert.Equal(t,
|
||||||
|
"act-owner-repo-baca2daaa2fe-dockeraction:latest",
|
||||||
|
dockerActionImageTag("owner/repo", "./", true),
|
||||||
|
)
|
||||||
|
assert.Equal(t,
|
||||||
|
"act-owner-repo-sub-e847b61255a8-dockeraction:latest",
|
||||||
|
dockerActionImageTag("owner/repo", "./sub", true),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sanitizing every non-alphanumeric character to "-" is lossy, so distinct inputs can collapse to the same readable prefix.
|
||||||
|
// The hash suffix must keep such cases apart, otherwise an image built for one repository is reused for another.
|
||||||
|
collisions := [][2]struct {
|
||||||
|
repoName string
|
||||||
|
actionName string
|
||||||
|
}{
|
||||||
|
// Two different repositories, both `uses: ./`: "a/b-c" and "a-b/c" both sanitize to "a-b-c".
|
||||||
|
{{"a/b-c", "./"}, {"a-b/c", "./"}},
|
||||||
|
// A repository's root action vs another repository's sub-path action:
|
||||||
|
// "owner/repo-a" + "./" and "owner/repo" + "./a" both sanitize to "owner-repo-a".
|
||||||
|
{{"owner/repo-a", "./"}, {"owner/repo", "./a"}},
|
||||||
|
}
|
||||||
|
for _, c := range collisions {
|
||||||
|
assert.NotEqual(t,
|
||||||
|
dockerActionImageTag(c[0].repoName, c[0].actionName, true),
|
||||||
|
dockerActionImageTag(c[1].repoName, c[1].actionName, true),
|
||||||
|
"local docker action tags must differ for %q/%q vs %q/%q",
|
||||||
|
c[0].repoName, c[0].actionName, c[1].repoName, c[1].actionName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Distinct local actions within the same repository keep distinct tags.
|
||||||
|
assert.NotEqual(t,
|
||||||
|
dockerActionImageTag("owner/repo", "./", true),
|
||||||
|
dockerActionImageTag("owner/repo", "./sub", true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the entrypoint is stage specific: every stage of a docker action receives runs.args
|
||||||
|
// and runs.env, and the `entrypoint` input applies to the main stage alone.
|
||||||
|
func TestExecAsDockerStageEntrypoint(t *testing.T) {
|
||||||
|
orig := ContainerNewContainer
|
||||||
|
defer func() { ContainerNewContainer = orig }()
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
stage stepStage
|
||||||
|
wantEntrypoint []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "main stage prefers the entrypoint input",
|
||||||
|
stage: stepStageMain,
|
||||||
|
wantEntrypoint: []string{"input.sh"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "pre stage uses runs.pre-entrypoint",
|
||||||
|
stage: stepStagePre,
|
||||||
|
wantEntrypoint: []string{"pre.sh", "--verbose"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "post stage uses runs.post-entrypoint",
|
||||||
|
stage: stepStagePost,
|
||||||
|
wantEntrypoint: []string{"post.sh"},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
cm := &containerMock{}
|
||||||
|
var input *container.NewContainerInput
|
||||||
|
ContainerNewContainer = func(in *container.NewContainerInput) container.ExecutionsEnvironment {
|
||||||
|
input = in
|
||||||
|
return cm
|
||||||
|
}
|
||||||
|
|
||||||
|
step := &stepActionRemote{
|
||||||
|
Step: &model.Step{ID: "1", Uses: "org/action@v1", With: map[string]string{"entrypoint": "input.sh"}},
|
||||||
|
RunContext: &RunContext{
|
||||||
|
Config: &Config{},
|
||||||
|
Run: &model.Run{JobID: "1", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"1": {}}}},
|
||||||
|
JobContainer: cm,
|
||||||
|
},
|
||||||
|
action: &model.Action{Runs: model.ActionRuns{
|
||||||
|
Using: "docker",
|
||||||
|
Image: "docker://node:14",
|
||||||
|
PreEntrypoint: "pre.sh --verbose",
|
||||||
|
Entrypoint: "main.sh",
|
||||||
|
PostEntrypoint: "post.sh",
|
||||||
|
Args: []string{"hello"},
|
||||||
|
Env: map[string]string{"MY_VAR": "world"},
|
||||||
|
}},
|
||||||
|
env: map[string]string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
cm.On("Pull", false).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Remove").Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Create", []string(nil), []string(nil)).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Start", true).Return(func(context.Context) error { return nil })
|
||||||
|
cm.On("Close").Return(func(context.Context) error { return nil })
|
||||||
|
|
||||||
|
require.NoError(t, execAsDocker(context.Background(), step, "action", t.TempDir(), t.TempDir(), false, tc.stage))
|
||||||
|
require.NotNil(t, input)
|
||||||
|
assert.Equal(t, tc.wantEntrypoint, input.Entrypoint)
|
||||||
|
assert.Equal(t, []string{"hello"}, input.Cmd)
|
||||||
|
assert.Contains(t, input.Env, "MY_VAR=world")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDockerActionHasPreAndPostStep(t *testing.T) {
|
||||||
|
newStep := func(runs model.ActionRuns) actionStep {
|
||||||
|
return &stepActionRemote{action: &model.Action{Runs: runs}}
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
assert.False(t, hasPreStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
||||||
|
assert.False(t, hasPostStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
||||||
|
|
||||||
|
withStages := model.ActionRuns{Using: "docker", Image: "Dockerfile", PreEntrypoint: "pre.sh", PostEntrypoint: "post.sh"}
|
||||||
|
assert.True(t, hasPreStep(newStep(withStages))(ctx))
|
||||||
|
assert.True(t, hasPostStep(newStep(withStages))(ctx))
|
||||||
|
}
|
||||||
|
|||||||
312
act/runner/cancellation_test.go
Normal file
312
act/runner/cancellation_test.go
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/exprparser"
|
||||||
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"go.yaml.in/yaml/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestCancelledJobStatusEnablesAlwaysAndCancelledSteps verifies that once a job is
|
||||||
|
// cancelled, getJobContext reports the "cancelled" status so the step `if` functions
|
||||||
|
// evaluate the way GitHub Actions does: cancelled()/always() are true, success()/failure()
|
||||||
|
// are false. A step that defaults to success() is therefore skipped while an always() step
|
||||||
|
// still runs. Before the fix the status could only ever be success/failure, so cancelled()
|
||||||
|
// was structurally impossible and cancel-only cleanup steps never ran.
|
||||||
|
func TestCancelledJobStatusEnablesAlwaysAndCancelledSteps(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
rc.markCancelled()
|
||||||
|
|
||||||
|
// The core fix: the job status context now reports "cancelled" instead of being
|
||||||
|
// pinned to success/failure.
|
||||||
|
jobCtx := rc.getJobContext()
|
||||||
|
require.Equal(t, "cancelled", jobCtx.Status)
|
||||||
|
|
||||||
|
// Feed that status through the step-context expression functions, which is what a
|
||||||
|
// step `if` evaluates. On a cancelled job only always()/cancelled() are true.
|
||||||
|
interp := exprparser.NewInterpeter(
|
||||||
|
&exprparser.EvaluationEnvironment{Job: jobCtx},
|
||||||
|
exprparser.Config{Context: "step"},
|
||||||
|
)
|
||||||
|
for expr, want := range map[string]bool{
|
||||||
|
"cancelled()": true,
|
||||||
|
"always()": true,
|
||||||
|
"success()": false,
|
||||||
|
"failure()": false,
|
||||||
|
"!cancelled()": false,
|
||||||
|
} {
|
||||||
|
got, err := interp.Evaluate(expr, exprparser.DefaultStatusCheckNone)
|
||||||
|
require.NoErrorf(t, err, "Evaluate(%q)", expr)
|
||||||
|
assert.Equalf(t, want, got, "Evaluate(%q) on a cancelled job", expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A step without an `if` defaults to success() and must be skipped on cancel,
|
||||||
|
// while an `if: always()` step must still run.
|
||||||
|
disabled, err := interp.Evaluate("", exprparser.DefaultStatusCheckSuccess)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, false, disabled, "default-success step must be skipped on a cancelled job")
|
||||||
|
|
||||||
|
enabled, err := interp.Evaluate("always()", exprparser.DefaultStatusCheckSuccess)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, true, enabled, "`if: always()` step must run on a cancelled job")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMainStepsExecutorRunsAlwaysStepsAfterCancel verifies that newMainStepsExecutor does
|
||||||
|
// not abandon the remaining steps when the run is cancelled mid-pipeline. The later step
|
||||||
|
// still runs (so a main-stage always() step is reached), it runs under a fresh,
|
||||||
|
// non-cancelled context, and the job is marked cancelled. The interrupt error is still
|
||||||
|
// propagated so callers up the chain see the cancellation.
|
||||||
|
func TestMainStepsExecutorRunsAlwaysStepsAfterCancel(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var ran []string
|
||||||
|
var laterStepCtxErr error
|
||||||
|
steps := []common.Executor{
|
||||||
|
func(_ context.Context) error {
|
||||||
|
ran = append(ran, "step1")
|
||||||
|
cancel() // server cancellation lands while step1 runs
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(c context.Context) error {
|
||||||
|
ran = append(ran, "always-step")
|
||||||
|
laterStepCtxErr = c.Err()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := newMainStepsExecutor(rc, steps)(ctx)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, context.Canceled, "interrupt error is propagated")
|
||||||
|
assert.Equal(t, []string{"step1", "always-step"}, ran, "the always() step still runs after cancel")
|
||||||
|
require.NoError(t, laterStepCtxErr, "remaining steps run under a fresh, non-cancelled context")
|
||||||
|
assert.True(t, rc.jobCancelled, "the job is marked cancelled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMainStepsExecutorMarksFailedOnTimeoutBetweenSteps guards the timeout path's symmetry with the cancel path.
|
||||||
|
// When the job deadline (timeout-minutes) lands in the gap between two steps, the job must be marked as failed (not cancelled),
|
||||||
|
// so always()/failure() cleanup steps run while default success() steps skip, and so the timed-out job is not reported as success.
|
||||||
|
func TestMainStepsExecutorMarksFailedOnTimeoutBetweenSteps(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
// A short deadline that we let elapse between steps, so no step records the error itself.
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var ran []string
|
||||||
|
var laterStepCtxErr error
|
||||||
|
steps := []common.Executor{
|
||||||
|
func(c context.Context) error {
|
||||||
|
ran = append(ran, "step1")
|
||||||
|
// Block until the job deadline elapses, then return cleanly: the interrupt lands in the loop's between-steps check, not inside a step.
|
||||||
|
<-c.Done()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(c context.Context) error {
|
||||||
|
ran = append(ran, "always-step")
|
||||||
|
laterStepCtxErr = c.Err()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := newMainStepsExecutor(rc, steps)(ctx)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, context.DeadlineExceeded, "the timeout error is propagated")
|
||||||
|
assert.Equal(t, []string{"step1", "always-step"}, ran, "the always() step still runs after a timeout")
|
||||||
|
require.NoError(t, laterStepCtxErr, "remaining steps run under a fresh, non-expired context")
|
||||||
|
assert.True(t, rc.jobFailed, "a job timeout marks the job failed")
|
||||||
|
assert.False(t, rc.jobCancelled, "a timeout is not a cancellation")
|
||||||
|
|
||||||
|
// The status the real main-step `if` evaluation sees: "failure", so default success() steps skip while always()/failure() steps run.
|
||||||
|
assert.Equal(t, "failure", rc.getJobContext().Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestStepsExecutorRunsMainStepsAfterPreCancel verifies that a cancellation landing during the
|
||||||
|
// pre phase does not abandon the main steps: newStepsExecutor still runs the main-steps executor,
|
||||||
|
// so a main-stage always()/cancelled() step is reached (under a fresh, non-cancelled context),
|
||||||
|
// the job is marked cancelled, and the cancellation is propagated. Before the fix the `.Then(...)`
|
||||||
|
// short-circuit skipped the main steps entirely when a pre step was cancelled.
|
||||||
|
func TestStepsExecutorRunsMainStepsAfterPreCancel(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var ran []string
|
||||||
|
var mainStepCtxErr error
|
||||||
|
preSteps := []common.Executor{
|
||||||
|
func(_ context.Context) error {
|
||||||
|
ran = append(ran, "pre1")
|
||||||
|
cancel() // server cancellation lands during the pre phase
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
steps := []common.Executor{
|
||||||
|
func(c context.Context) error {
|
||||||
|
ran = append(ran, "always-step")
|
||||||
|
mainStepCtxErr = c.Err()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := newStepsExecutor(rc, preSteps, steps)(ctx)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, context.Canceled, "the cancellation is propagated")
|
||||||
|
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-phase cancel")
|
||||||
|
require.NoError(t, mainStepCtxErr, "the main step runs under a fresh, non-cancelled context")
|
||||||
|
assert.True(t, rc.jobCancelled, "the job is marked cancelled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestStepsExecutorRunsMainStepsAfterPreFailure verifies that a failing pre step does not abandon
|
||||||
|
// the main steps: they still run (so a main-stage always()/failure() step is reached), and the
|
||||||
|
// pre-step error is propagated so the job is reported as failed. The main steps' own `if`
|
||||||
|
// evaluation is what skips success()-default steps, so running them here is safe.
|
||||||
|
func TestStepsExecutorRunsMainStepsAfterPreFailure(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
var ran []string
|
||||||
|
preSteps := []common.Executor{
|
||||||
|
func(_ context.Context) error {
|
||||||
|
ran = append(ran, "pre1")
|
||||||
|
return assert.AnError
|
||||||
|
},
|
||||||
|
}
|
||||||
|
steps := []common.Executor{
|
||||||
|
func(_ context.Context) error {
|
||||||
|
ran = append(ran, "always-step")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := newStepsExecutor(rc, preSteps, steps)(context.Background())
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, assert.AnError, "the pre-step error is propagated")
|
||||||
|
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-step failure")
|
||||||
|
assert.False(t, rc.jobCancelled, "a pre-step failure is not a cancellation")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPreStepFailureAffectsMainStepIfStatus verifies the status path used by real
|
||||||
|
// main-step `if` evaluation. A pre-step failure is not present in StepResults, so
|
||||||
|
// recording only the context job error is not enough: getJobContext must also report
|
||||||
|
// failure so success()-default main steps skip and failure() steps run.
|
||||||
|
func TestPreStepFailureAffectsMainStepIfStatus(t *testing.T) {
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
ctx := common.WithJobErrorContainer(context.Background())
|
||||||
|
|
||||||
|
reportStepError(ctx, rc, assert.AnError)
|
||||||
|
|
||||||
|
assert.Equal(t, "failure", rc.getJobContext().Status)
|
||||||
|
require.ErrorIs(t, common.JobError(ctx), assert.AnError)
|
||||||
|
|
||||||
|
defaultStep := &stepRun{
|
||||||
|
RunContext: rc,
|
||||||
|
Step: &model.Step{ID: "default-step"},
|
||||||
|
env: map[string]string{},
|
||||||
|
}
|
||||||
|
defaultEnabled, err := isStepEnabled(ctx, defaultStep.getIfExpression(ctx, stepStageMain), defaultStep, stepStageMain)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.False(t, defaultEnabled, "default success() main step must skip after a pre-step failure")
|
||||||
|
|
||||||
|
failureStep := &stepRun{
|
||||||
|
RunContext: rc,
|
||||||
|
Step: &model.Step{
|
||||||
|
ID: "failure-step",
|
||||||
|
If: yaml.Node{Value: "failure()"},
|
||||||
|
},
|
||||||
|
env: map[string]string{},
|
||||||
|
}
|
||||||
|
failureEnabled, err := isStepEnabled(ctx, failureStep.getIfExpression(ctx, stepStageMain), failureStep, stepStageMain)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, failureEnabled, "failure() main step must run after a pre-step failure")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPostStepsContextCancelledIsUsableForFailingStep guards against a panic: post/cleanup
|
||||||
|
// steps run on a context derived from the cancelled job context, and a failing post step
|
||||||
|
// records its error via common.SetJobError. If that derived context lacks a job-error container,
|
||||||
|
// SetJobError dereferences a nil map and panics. The post context must therefore be detached
|
||||||
|
// from cancellation (so the steps run) yet still carry a usable error container.
|
||||||
|
func TestPostStepsContextCancelledIsUsableForFailingStep(t *testing.T) {
|
||||||
|
cancelled, cancel := context.WithCancel(common.WithJobErrorContainer(context.Background()))
|
||||||
|
cancel()
|
||||||
|
require.ErrorIs(t, cancelled.Err(), context.Canceled)
|
||||||
|
|
||||||
|
postCtx, done := postStepsContext(cancelled)
|
||||||
|
defer done()
|
||||||
|
|
||||||
|
// Detached from cancellation, so the post steps actually run.
|
||||||
|
require.NoError(t, postCtx.Err(), "post context must not be cancelled")
|
||||||
|
|
||||||
|
// A failing post step records its error instead of panicking.
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
common.SetJobError(postCtx, assert.AnError)
|
||||||
|
}, "a failing post step must not panic on the cancel path")
|
||||||
|
assert.ErrorIs(t, common.JobError(postCtx), assert.AnError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPostStepsContextDeadlinePreservesJobError verifies the job-timeout path keeps the original
|
||||||
|
// job-error container (via context.WithoutCancel), so the timeout failure and any post-step error
|
||||||
|
// survive into the post phase and the job is still reported as failed.
|
||||||
|
func TestPostStepsContextDeadlinePreservesJobError(t *testing.T) {
|
||||||
|
base := common.WithJobErrorContainer(context.Background())
|
||||||
|
common.SetJobError(base, assert.AnError)
|
||||||
|
expired, cancel := context.WithDeadline(base, time.Now().Add(-time.Hour))
|
||||||
|
defer cancel()
|
||||||
|
require.ErrorIs(t, expired.Err(), context.DeadlineExceeded)
|
||||||
|
|
||||||
|
postCtx, done := postStepsContext(expired)
|
||||||
|
defer done()
|
||||||
|
|
||||||
|
require.NoError(t, postCtx.Err(), "post context must not carry the expired deadline")
|
||||||
|
assert.ErrorIs(t, common.JobError(postCtx), assert.AnError, "the timeout job error must be preserved")
|
||||||
|
}
|
||||||
|
|
||||||
|
// reportStepError must treat a context.Canceled (e.g. a teardown-cancelled read) as an
|
||||||
|
// interruption, never a job failure.
|
||||||
|
func TestReportStepErrorTreatsCancelAsInterruption(t *testing.T) {
|
||||||
|
rc := &RunContext{}
|
||||||
|
|
||||||
|
// stray read cancellation while the job context is live: ignored, not a failure
|
||||||
|
live := common.WithJobErrorContainer(context.Background())
|
||||||
|
reportStepError(live, rc, context.Canceled)
|
||||||
|
require.NoError(t, common.JobError(live))
|
||||||
|
assert.False(t, rc.jobFailed)
|
||||||
|
assert.False(t, rc.jobCancelled)
|
||||||
|
|
||||||
|
// genuine job cancellation: recorded as cancelled, still not a failure
|
||||||
|
cancelled, cancel := context.WithCancel(common.WithJobErrorContainer(context.Background()))
|
||||||
|
cancel()
|
||||||
|
reportStepError(cancelled, rc, context.Canceled)
|
||||||
|
require.NoError(t, common.JobError(cancelled))
|
||||||
|
assert.False(t, rc.jobFailed)
|
||||||
|
assert.True(t, rc.jobCancelled)
|
||||||
|
|
||||||
|
// a real error still fails the job
|
||||||
|
failed := common.WithJobErrorContainer(context.Background())
|
||||||
|
reportStepError(failed, rc, assert.AnError)
|
||||||
|
require.ErrorIs(t, common.JobError(failed), assert.AnError)
|
||||||
|
assert.True(t, rc.jobFailed)
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commandPatternGA *regexp.Regexp
|
var commandPatternGA *regexp.Regexp
|
||||||
@@ -48,10 +48,13 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
|
|||||||
if resumeCommand != "" && command != resumeCommand {
|
if resumeCommand != "" && command != resumeCommand {
|
||||||
// There should not be any emojis in the log output for Gitea.
|
// There should not be any emojis in the log output for Gitea.
|
||||||
// The code in the switch statement is the same.
|
// The code in the switch statement is the same.
|
||||||
|
// Return true (not false) so the line still reaches the raw_output
|
||||||
|
// log handler; otherwise everything between ::stop-commands:: and
|
||||||
|
// its end token is silently dropped from the step log.
|
||||||
logger.Infof("%s", line)
|
logger.Infof("%s", line)
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
arg = unescapeCommandData(arg)
|
arg = UnescapeCommandData(arg)
|
||||||
kvPairs = unescapeKvPairs(kvPairs)
|
kvPairs = unescapeKvPairs(kvPairs)
|
||||||
switch command {
|
switch command {
|
||||||
case "set-env":
|
case "set-env":
|
||||||
@@ -120,7 +123,7 @@ func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string,
|
|||||||
|
|
||||||
result, ok := rc.StepResults[stepID]
|
result, ok := rc.StepResults[stepID]
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Infof(" \U00002757 no outputs used step '%s'", stepID)
|
logger.Infof("No outputs registered for step '%s'", stepID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,30 +154,25 @@ func parseKeyValuePairs(kvPairs, separator string) map[string]string {
|
|||||||
return rtn
|
return rtn
|
||||||
}
|
}
|
||||||
|
|
||||||
func unescapeCommandData(arg string) string {
|
// A Replacer never rescans what it wrote, so "%250A" stays a literal "%0A".
|
||||||
escapeMap := map[string]string{
|
var (
|
||||||
"%25": "%",
|
commandDataEscaper = strings.NewReplacer("%", "%25", "\r", "%0D", "\n", "%0A")
|
||||||
"%0D": "\r",
|
commandDataUnescaper = strings.NewReplacer("%25", "%", "%0D", "\r", "%0A", "\n")
|
||||||
"%0A": "\n",
|
commandPropertyUnescaper = strings.NewReplacer("%25", "%", "%0D", "\r", "%0A", "\n", "%3A", ":", "%2C", ",")
|
||||||
}
|
)
|
||||||
for k, v := range escapeMap {
|
|
||||||
arg = strings.ReplaceAll(arg, k, v)
|
// escapeCommandData encodes the data part of a "::cmd::" or "##[cmd]" line the runner writes itself,
|
||||||
}
|
// so the log renderer decodes it back. Lines forwarded from step output are already escaped.
|
||||||
return arg
|
func escapeCommandData(arg string) string {
|
||||||
|
return commandDataEscaper.Replace(arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnescapeCommandData(arg string) string {
|
||||||
|
return commandDataUnescaper.Replace(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unescapeCommandProperty(arg string) string {
|
func unescapeCommandProperty(arg string) string {
|
||||||
escapeMap := map[string]string{
|
return commandPropertyUnescaper.Replace(arg)
|
||||||
"%25": "%",
|
|
||||||
"%0D": "\r",
|
|
||||||
"%0A": "\n",
|
|
||||||
"%3A": ":",
|
|
||||||
"%2C": ",",
|
|
||||||
}
|
|
||||||
for k, v := range escapeMap {
|
|
||||||
arg = strings.ReplaceAll(arg, k, v)
|
|
||||||
}
|
|
||||||
return arg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
|
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -28,6 +28,29 @@ func TestSetEnv(t *testing.T) {
|
|||||||
a.Equal("valz", rc.Env["x"])
|
a.Equal("valz", rc.Env["x"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStopCommandsKeepsSuppressedLinesInLog(t *testing.T) {
|
||||||
|
a := assert.New(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
rc := new(RunContext)
|
||||||
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
|
// Stop command processing until the matching end token is seen.
|
||||||
|
a.True(handler("::stop-commands::my-end-token\n"))
|
||||||
|
|
||||||
|
// A command-shaped line while stopped must not be executed (env unchanged),
|
||||||
|
// but must still return true so it reaches the raw_output log handler and is
|
||||||
|
// not dropped from the step log.
|
||||||
|
a.True(handler("::set-env name=x::valz\n"))
|
||||||
|
a.NotContains(rc.Env, "x")
|
||||||
|
|
||||||
|
// The matching end token resumes command processing.
|
||||||
|
a.True(handler("::my-end-token::\n"))
|
||||||
|
|
||||||
|
// Commands are processed again after resuming.
|
||||||
|
a.True(handler("::set-env name=y::valy\n"))
|
||||||
|
a.Equal("valy", rc.Env["y"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestSetOutput(t *testing.T) {
|
func TestSetOutput(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -191,3 +214,10 @@ func TestSaveState(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "state-value", rc.IntraActionState["step"]["state-name"])
|
assert.Equal(t, "state-value", rc.IntraActionState["step"]["state-name"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEscapeCommandData(t *testing.T) {
|
||||||
|
a := assert.New(t)
|
||||||
|
|
||||||
|
a.Equal("a%25b%0Dc%0Ad%250A", escapeCommandData("a%b\rc\nd%0A"))
|
||||||
|
a.Equal("a%b\rc\nd%0A", UnescapeCommandData("a%25b%0Dc%0Ad%250A"))
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/act_runner/act/exprparser"
|
"gitea.com/gitea/runner/act/exprparser"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map
|
|||||||
for _, needs := range jobNeeds {
|
for _, needs := range jobNeeds {
|
||||||
using[needs] = exprparser.Needs{
|
using[needs] = exprparser.Needs{
|
||||||
Outputs: jobs[needs].Outputs,
|
Outputs: jobs[needs].Outputs,
|
||||||
Result: jobs[needs].Result,
|
Result: jobs[needs].NeedsResult(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,9 +95,7 @@ func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map
|
|||||||
Inputs: inputs,
|
Inputs: inputs,
|
||||||
HashFiles: getHashFilesFunction(ctx, rc),
|
HashFiles: getHashFilesFunction(ctx, rc),
|
||||||
}
|
}
|
||||||
if rc.JobContainer != nil {
|
ee.Runner = rc.getRunnerContext(ctx)
|
||||||
ee.Runner = rc.JobContainer.GetRunnerContext(ctx)
|
|
||||||
}
|
|
||||||
return expressionEvaluator{
|
return expressionEvaluator{
|
||||||
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
||||||
Run: rc.Run,
|
Run: rc.Run,
|
||||||
@@ -127,7 +125,7 @@ func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step)
|
|||||||
for _, needs := range jobNeeds {
|
for _, needs := range jobNeeds {
|
||||||
using[needs] = exprparser.Needs{
|
using[needs] = exprparser.Needs{
|
||||||
Outputs: jobs[needs].Outputs,
|
Outputs: jobs[needs].Outputs,
|
||||||
Result: jobs[needs].Result,
|
Result: jobs[needs].NeedsResult(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,9 +147,7 @@ func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step)
|
|||||||
Inputs: inputs,
|
Inputs: inputs,
|
||||||
HashFiles: getHashFilesFunction(ctx, rc),
|
HashFiles: getHashFilesFunction(ctx, rc),
|
||||||
}
|
}
|
||||||
if rc.JobContainer != nil {
|
ee.Runner = rc.getRunnerContext(ctx)
|
||||||
ee.Runner = rc.JobContainer.GetRunnerContext(ctx)
|
|
||||||
}
|
|
||||||
return expressionEvaluator{
|
return expressionEvaluator{
|
||||||
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
||||||
Run: rc.Run,
|
Run: rc.Run,
|
||||||
@@ -229,7 +225,8 @@ func (ee expressionEvaluator) evaluate(ctx context.Context, in string, defaultSt
|
|||||||
logger.Debugf("evaluating expression '%s'", in)
|
logger.Debugf("evaluating expression '%s'", in)
|
||||||
evaluated, err := ee.interpreter.Evaluate(in, defaultStatusCheck)
|
evaluated, err := ee.interpreter.Evaluate(in, defaultStatusCheck)
|
||||||
|
|
||||||
printable := regexp.MustCompile(`::add-mask::.*`).ReplaceAllString(fmt.Sprintf("%t", evaluated), "::add-mask::***)")
|
// evaluated is an any: %t renders everything but a bool as "%!t(string=...)"
|
||||||
|
printable := regexp.MustCompile(`::add-mask::.*`).ReplaceAllString(fmt.Sprintf("%v", evaluated), "::add-mask::***)")
|
||||||
logger.Debugf("expression '%s' evaluated to '%s'", in, printable)
|
logger.Debugf("expression '%s' evaluated to '%s'", in, printable)
|
||||||
|
|
||||||
return evaluated, err
|
return evaluated, err
|
||||||
@@ -405,7 +402,6 @@ func escapeFormatString(in string) string {
|
|||||||
return strings.ReplaceAll(strings.ReplaceAll(in, "{", "{{"), "}", "}}")
|
return strings.ReplaceAll(strings.ReplaceAll(in, "{", "{{"), "}", "}}")
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (string, error) { //nolint:unparam // pre-existing issue from nektos/act
|
func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (string, error) { //nolint:unparam // pre-existing issue from nektos/act
|
||||||
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
||||||
return in, nil
|
return in, nil
|
||||||
@@ -472,7 +468,6 @@ func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (str
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo // function handles many cases
|
|
||||||
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]any {
|
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]any {
|
||||||
inputs := map[string]any{}
|
inputs := map[string]any{}
|
||||||
|
|
||||||
@@ -499,11 +494,7 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod
|
|||||||
if value == nil {
|
if value == nil {
|
||||||
value = v.Default
|
value = v.Default
|
||||||
}
|
}
|
||||||
if v.Type == "boolean" {
|
inputs[k] = coerceInputValue(value, v.Type)
|
||||||
inputs[k] = value == "true"
|
|
||||||
} else {
|
|
||||||
inputs[k] = value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -516,17 +507,26 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod
|
|||||||
if value == nil {
|
if value == nil {
|
||||||
value = v.Default
|
value = v.Default
|
||||||
}
|
}
|
||||||
if v.Type == "boolean" {
|
inputs[k] = coerceInputValue(value, v.Type)
|
||||||
inputs[k] = value == "true"
|
|
||||||
} else {
|
|
||||||
inputs[k] = value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return inputs
|
return inputs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// coerceInputValue converts an input value to the type declared by the workflow.
|
||||||
|
// The event payload carries natively typed JSON values on newer Gitea versions,
|
||||||
|
// while defaults and older servers provide strings.
|
||||||
|
func coerceInputValue(value any, inputType string) any {
|
||||||
|
if inputType != "boolean" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
if b, ok := value.(bool); ok {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
return value == "true"
|
||||||
|
}
|
||||||
|
|
||||||
func setupWorkflowInputs(ctx context.Context, inputs *map[string]any, rc *RunContext) {
|
func setupWorkflowInputs(ctx context.Context, inputs *map[string]any, rc *RunContext) {
|
||||||
if rc.caller != nil {
|
if rc.caller != nil {
|
||||||
config := rc.Run.Workflow.WorkflowCallConfig()
|
config := rc.Run.Workflow.WorkflowCallConfig()
|
||||||
@@ -550,7 +550,7 @@ func setupWorkflowInputs(ctx context.Context, inputs *map[string]any, rc *RunCon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(*inputs)[name] = value
|
(*inputs)[name] = coerceInputValue(value, input.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -564,15 +564,15 @@ func getWorkflowSecrets(ctx context.Context, rc *RunContext) map[string]string {
|
|||||||
secrets = rc.caller.runContext.Config.Secrets
|
secrets = rc.caller.runContext.Config.Secrets
|
||||||
}
|
}
|
||||||
|
|
||||||
if secrets == nil {
|
// Interpolate into a new map. secrets may be the shared Config.Secrets (or the job's
|
||||||
secrets = map[string]string{}
|
// map), which other parallel jobs read concurrently (e.g. log masking), so mutating it
|
||||||
}
|
// in place is a data race.
|
||||||
|
interpolated := make(map[string]string, len(secrets))
|
||||||
for k, v := range secrets {
|
for k, v := range secrets {
|
||||||
secrets[k] = rc.caller.runContext.ExprEval.Interpolate(ctx, v)
|
interpolated[k] = rc.caller.runContext.ExprEval.Interpolate(ctx, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return secrets
|
return interpolated
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc.Config.Secrets
|
return rc.Config.Secrets
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/exprparser"
|
"gitea.com/gitea/runner/act/exprparser"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
yaml "go.yaml.in/yaml/v4"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -321,3 +323,82 @@ func TestRewriteSubExpressionForceFormat(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetEvaluatorInputsBoolean(t *testing.T) {
|
||||||
|
workflows := map[string]string{
|
||||||
|
"workflow_call": `
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
flag:
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
default: gitea
|
||||||
|
`,
|
||||||
|
"workflow_dispatch": `
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
flag:
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
default: gitea
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
|
||||||
|
tables := []struct {
|
||||||
|
name string
|
||||||
|
event map[string]any
|
||||||
|
flag any
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// Gitea >= 1.27 resolves the inputs server-side and sends native JSON types
|
||||||
|
name: "native bool true",
|
||||||
|
event: map[string]any{"inputs": map[string]any{"flag": true}},
|
||||||
|
flag: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "native bool false",
|
||||||
|
event: map[string]any{"inputs": map[string]any{"flag": false}},
|
||||||
|
flag: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "string true",
|
||||||
|
event: map[string]any{"inputs": map[string]any{"flag": "true"}},
|
||||||
|
flag: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "string false",
|
||||||
|
event: map[string]any{"inputs": map[string]any{"flag": "false"}},
|
||||||
|
flag: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default is used when the event carries no inputs",
|
||||||
|
event: map[string]any{},
|
||||||
|
flag: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for eventName, workflow := range workflows {
|
||||||
|
for _, table := range tables {
|
||||||
|
t.Run(eventName+"/"+table.name, func(t *testing.T) {
|
||||||
|
wf, err := model.ReadWorkflow(strings.NewReader(workflow))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
rc := &RunContext{
|
||||||
|
Config: &Config{Workdir: "."},
|
||||||
|
Run: &model.Run{JobID: "job1", Workflow: wf},
|
||||||
|
}
|
||||||
|
ghc := &model.GithubContext{EventName: eventName, Event: table.event}
|
||||||
|
|
||||||
|
inputs := getEvaluatorInputs(context.Background(), rc, nil, ghc)
|
||||||
|
assert.Equal(t, table.flag, inputs["flag"])
|
||||||
|
assert.Equal(t, "gitea", inputs["name"])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
52
act/runner/helpers_test.go
Normal file
52
act/runner/helpers_test.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
|
||||||
|
mobyclient "github.com/moby/moby/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// requireLinuxDocker skips on non-Linux hosts. Some integration workflows need Docker features
|
||||||
|
// that only a Linux daemon provides (host networking, host /proc bind mounts); Docker Desktop
|
||||||
|
// on macOS/Windows does not, so those tests can only run on Linux.
|
||||||
|
func requireLinuxDocker(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
if runtime.GOOS != "linux" {
|
||||||
|
t.Skip("skipping: requires a Linux Docker host")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// requireDocker skips the test unless a reachable docker daemon is available.
|
||||||
|
// GetDockerClient succeeds even without a running daemon (its ping is best-effort),
|
||||||
|
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
||||||
|
func requireDocker(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
ctx := context.Background()
|
||||||
|
cli, err := container.GetDockerClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("skipping: docker client unavailable: %v", err)
|
||||||
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
if _, err := cli.Ping(ctx, mobyclient.PingOptions{}); err != nil {
|
||||||
|
t.Skipf("skipping: docker daemon unreachable: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// requireHostTools skips the test unless every named executable is on PATH. Used by the
|
||||||
|
// self-hosted (host environment) suite, which runs steps directly on the host.
|
||||||
|
func requireHostTools(t *testing.T, tools ...string) {
|
||||||
|
t.Helper()
|
||||||
|
for _, tool := range tools {
|
||||||
|
if _, err := exec.LookPath(tool); err != nil {
|
||||||
|
t.Skipf("skipping: required host tool %q not found: %v", tool, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,13 +5,46 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
"gitea.com/gitea/runner/act/exprparser"
|
||||||
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxJobSummaryBytes = 1024 * 1024
|
||||||
|
|
||||||
|
// jobSummaryTruncationMarker is appended to a summary that exceeded the size limit
|
||||||
|
// so the rendered output makes the truncation visible instead of silently cutting off.
|
||||||
|
const jobSummaryTruncationMarker = "\n\n---\n\n*Job summary truncated: it exceeded the maximum allowed size.*\n"
|
||||||
|
|
||||||
|
var (
|
||||||
|
jobSummaryUploadRetryDelay = time.Second
|
||||||
|
// jobSummaryUploadRequestTimeout bounds a single step upload request. It is kept
|
||||||
|
// below jobSummaryUploadPhaseTimeout so one slow or unreachable request times out
|
||||||
|
// and lets the remaining steps still upload within the phase budget, instead of a
|
||||||
|
// single stuck request consuming the whole phase.
|
||||||
|
jobSummaryUploadRequestTimeout = 5 * time.Second
|
||||||
|
// jobSummaryUploadPhaseTimeout bounds the total time spent uploading all step
|
||||||
|
// summaries. The uploads run inside the job cleanup budget that is also used to
|
||||||
|
// stop and remove the container, so a slow or unreachable endpoint must not be
|
||||||
|
// allowed to consume it; this keeps the remaining budget available for teardown.
|
||||||
|
jobSummaryUploadPhaseTimeout = 15 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type jobInfo interface {
|
type jobInfo interface {
|
||||||
@@ -24,16 +57,87 @@ type jobInfo interface {
|
|||||||
result(result string)
|
result(result string)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:contextcheck,gocyclo // composes many step executors
|
// reportStepError records a step error so the job is reported failed — except a
|
||||||
|
// cancellation, which is an interruption, not a failure.
|
||||||
|
func reportStepError(ctx context.Context, rc *RunContext, err error) {
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
// Defer to the job context: a genuine cancel reports cancelled, a stray teardown
|
||||||
|
// cancellation on a live ctx is ignored — never a step FAILURE.
|
||||||
|
rc.markInterrupted(ctx.Err())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
common.Logger(ctx).Errorf("##[error]%s", escapeCommandData(err.Error()))
|
||||||
|
common.SetJobError(ctx, err)
|
||||||
|
rc.markFailed()
|
||||||
|
}
|
||||||
|
|
||||||
|
// actionPreparer is implemented by steps that download an action before they run, so the job
|
||||||
|
// executor can fetch all of them up front.
|
||||||
|
type actionPreparer interface {
|
||||||
|
prepareActionExecutor() common.Executor
|
||||||
|
actionDownloadInfo() (reference, sha string, ok bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// printPrepareActions downloads every action the job uses before its first step runs and reports
|
||||||
|
// them as actions/runner's "Prepare all required actions" section does. The steps still call
|
||||||
|
// prepareActionExecutor themselves; it is a no-op once the action is resolved here.
|
||||||
|
func printPrepareActions(rc *RunContext, preparers []actionPreparer) common.Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
if len(preparers) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
||||||
|
rawLogger.Infof("Prepare all required actions")
|
||||||
|
|
||||||
|
for _, preparer := range preparers {
|
||||||
|
if err := preparer.prepareActionExecutor()(ctx); err != nil {
|
||||||
|
// No step has run yet, so the failure belongs to the job.
|
||||||
|
reportStepError(ctx, rc, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
reference, sha, ok := preparer.actionDownloadInfo()
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sha == "" {
|
||||||
|
rawLogger.Infof("Download action repository '%s'", reference)
|
||||||
|
} else {
|
||||||
|
rawLogger.Infof("Download action repository '%s' (SHA:%s)", reference, sha)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// printCompleteJobName closes the setup section the way actions/runner ends its "Set up job" step.
|
||||||
|
func printCompleteJobName(rc *RunContext) common.Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
// Name holds a matrix combination; JobName is the shared name GitHub reports.
|
||||||
|
name := rc.JobName
|
||||||
|
if name == "" {
|
||||||
|
name = rc.Name
|
||||||
|
}
|
||||||
|
if name == "" && rc.Run != nil {
|
||||||
|
name = rc.Run.JobID
|
||||||
|
}
|
||||||
|
common.Logger(ctx).WithField(rawOutputField, true).Infof("Complete job name: %s", name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executor {
|
func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executor {
|
||||||
steps := make([]common.Executor, 0)
|
steps := make([]common.Executor, 0)
|
||||||
preSteps := make([]common.Executor, 0)
|
preSteps := make([]common.Executor, 0)
|
||||||
|
// Collected separately: every action is downloaded before the first pre step runs.
|
||||||
|
stepPreSteps := make([]common.Executor, 0)
|
||||||
|
preparers := make([]actionPreparer, 0)
|
||||||
var postExecutor common.Executor
|
var postExecutor common.Executor
|
||||||
|
|
||||||
steps = append(steps, func(ctx context.Context) error {
|
steps = append(steps, func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
if len(info.matrix()) > 0 {
|
if len(info.matrix()) > 0 {
|
||||||
logger.Infof("\U0001F9EA Matrix: %v", info.matrix())
|
logger.Infof("Matrix: %v", info.matrix())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -74,35 +178,46 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
return common.NewErrorExecutor(err)
|
return common.NewErrorExecutor(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if preparer, ok := step.(actionPreparer); ok {
|
||||||
|
preparers = append(preparers, preparer)
|
||||||
|
}
|
||||||
|
|
||||||
|
stepIdx := stepModel.Number
|
||||||
preExec := step.pre()
|
preExec := step.pre()
|
||||||
preSteps = append(preSteps, useStepLogger(rc, stepModel, stepStagePre, func(ctx context.Context) error {
|
stepPreSteps = append(stepPreSteps, useStepLogger(rc, stepModel, stepStagePre, func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
rc.CurrentStepIndex = stepIdx
|
||||||
preErr := preExec(ctx)
|
preErr := preExec(ctx)
|
||||||
if preErr != nil {
|
if preErr != nil {
|
||||||
logger.Errorf("%v", preErr)
|
reportStepError(ctx, rc, preErr)
|
||||||
common.SetJobError(ctx, preErr)
|
|
||||||
} else if ctx.Err() != nil {
|
} else if ctx.Err() != nil {
|
||||||
logger.Errorf("%v", ctx.Err())
|
reportStepError(ctx, rc, ctx.Err())
|
||||||
common.SetJobError(ctx, ctx.Err())
|
|
||||||
}
|
}
|
||||||
return preErr
|
return preErr
|
||||||
}))
|
}))
|
||||||
|
|
||||||
stepExec := step.main()
|
stepExec := step.main()
|
||||||
steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error {
|
steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
rc.CurrentStepIndex = stepIdx
|
||||||
err := stepExec(ctx)
|
err := stepExec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("%v", err)
|
reportStepError(ctx, rc, err)
|
||||||
common.SetJobError(ctx, err)
|
|
||||||
} else if ctx.Err() != nil {
|
} else if ctx.Err() != nil {
|
||||||
logger.Errorf("%v", ctx.Err())
|
reportStepError(ctx, rc, ctx.Err())
|
||||||
common.SetJobError(ctx, ctx.Err())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}))
|
}))
|
||||||
|
|
||||||
postExec := useStepLogger(rc, stepModel, stepStagePost, step.post())
|
postFn := step.post()
|
||||||
|
postExec := useStepLogger(rc, stepModel, stepStagePost, func(ctx context.Context) error {
|
||||||
|
rc.CurrentStepIndex = stepIdx
|
||||||
|
err := postFn(ctx)
|
||||||
|
if err != nil {
|
||||||
|
reportStepError(ctx, rc, err)
|
||||||
|
} else if ctx.Err() != nil {
|
||||||
|
reportStepError(ctx, rc, ctx.Err())
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
if postExecutor != nil {
|
if postExecutor != nil {
|
||||||
// run the post executor in reverse order
|
// run the post executor in reverse order
|
||||||
postExecutor = postExec.Finally(postExecutor)
|
postExecutor = postExec.Finally(postExecutor)
|
||||||
@@ -111,15 +226,31 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The setup section of the job log. The started hook goes first, so what it sets up is
|
||||||
|
// in place for the first action download and the first step.
|
||||||
|
preSteps = append(preSteps, rc.runJobStartedHook)
|
||||||
|
preSteps = append(preSteps, printPrepareActions(rc, preparers))
|
||||||
|
preSteps = append(preSteps, stepPreSteps...)
|
||||||
|
preSteps = append(preSteps, printCompleteJobName(rc))
|
||||||
|
|
||||||
|
// Ahead of the teardown below, while the job environment is still up.
|
||||||
|
postExecutor = postExecutor.Finally(rc.runJobCompletedHook)
|
||||||
|
|
||||||
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
||||||
jobError := common.JobError(ctx)
|
jobError := common.JobError(ctx)
|
||||||
var err error
|
var err error
|
||||||
if rc.Config.AutoRemove || jobError == nil {
|
// jobError == nil keeps a failed job's container alive for post-mortem debugging when
|
||||||
|
// AutoRemove is off (the act-CLI --rm behavior; the shipped runner always sets
|
||||||
|
// AutoRemove). A cancelled run is not a failure to inspect, and the cancel-path post
|
||||||
|
// context now carries its own error container so a failing post step makes jobError
|
||||||
|
// non-nil — OR in rc.jobCancelled so cancellation still always tears the container down.
|
||||||
|
if rc.Config.AutoRemove || jobError == nil || rc.jobCancelled {
|
||||||
// always allow 1 min for stopping and removing the runner, even if we were cancelled
|
// always allow 1 min for stopping and removing the runner, even if we were cancelled
|
||||||
ctx, cancel := context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), time.Minute)
|
ctx, cancel := context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
tryUploadJobSummary(ctx, rc)
|
||||||
// For Gitea
|
// For Gitea
|
||||||
// We don't need to call `stopServiceContainers` here since it will be called by following `info.stopContainer`
|
// We don't need to call `stopServiceContainers` here since it will be called by following `info.stopContainer`
|
||||||
// logger.Infof("Cleaning up services for job %s", rc.JobName)
|
// logger.Infof("Cleaning up services for job %s", rc.JobName)
|
||||||
@@ -137,7 +268,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
// if !rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == "" {
|
// if !rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == "" {
|
||||||
// // clean network in docker mode only
|
// // clean network in docker mode only
|
||||||
// // if the value of `ContainerNetworkMode` is empty string,
|
// // if the value of `ContainerNetworkMode` is empty string,
|
||||||
// // it means that the network to which containers are connecting is created by `act_runner`,
|
// // it means that the network to which containers are connecting is created by `runner`,
|
||||||
// // so, we should remove the network at last.
|
// // so, we should remove the network at last.
|
||||||
// networkName, _ := rc.networkName()
|
// networkName, _ := rc.networkName()
|
||||||
// logger.Infof("Cleaning up network for job %s, and network name is: %s", rc.JobName, networkName)
|
// logger.Infof("Cleaning up network for job %s, and network name is: %s", rc.JobName, networkName)
|
||||||
@@ -152,40 +283,136 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
||||||
pipeline := make([]common.Executor, 0)
|
stepsExecutor := newStepsExecutor(rc, preSteps, steps)
|
||||||
pipeline = append(pipeline, preSteps...)
|
|
||||||
pipeline = append(pipeline, steps...)
|
|
||||||
|
|
||||||
return common.NewPipelineExecutor(info.startContainer(), common.NewPipelineExecutor(pipeline...).
|
return common.NewPipelineExecutor(info.startContainer(), stepsExecutor.
|
||||||
Finally(func(ctx context.Context) error { //nolint:contextcheck // intentionally detaches from canceled parent
|
Finally(func(ctx context.Context) error {
|
||||||
var cancel context.CancelFunc
|
// Record an interrupt (backstop for interrupts that land outside the main
|
||||||
if ctx.Err() == context.Canceled {
|
// step loop) so the post steps observe the cancelled/failed job status.
|
||||||
// in case of an aborted run, we still should execute the
|
rc.markInterrupted(ctx.Err())
|
||||||
// post steps to allow cleanup.
|
postCtx, cancel := postStepsContext(ctx)
|
||||||
ctx, cancel = context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), 5*time.Minute)
|
defer cancel()
|
||||||
defer cancel()
|
return postExecutor(postCtx)
|
||||||
}
|
|
||||||
return postExecutor(ctx)
|
|
||||||
}).
|
}).
|
||||||
Finally(info.interpolateOutputs()).
|
Finally(info.interpolateOutputs()).
|
||||||
Finally(info.closeContainer()))
|
Finally(info.closeContainer()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// postStepsContext derives the context used to run the job's post/cleanup steps from the
|
||||||
|
// finished main-pipeline context. Cleanup has to run even when the run was interrupted, so the
|
||||||
|
// returned context always carries a fresh bounded deadline and is never itself cancelled.
|
||||||
|
//
|
||||||
|
// - context.Canceled (server cancel): detach from the cancelled context via a fresh root so
|
||||||
|
// the post steps can run.
|
||||||
|
// - context.DeadlineExceeded (job timeout): detach the deadline with WithoutCancel, which
|
||||||
|
// keeps the original values — including the job-error container — so the timeout failure and
|
||||||
|
// any post-step error are preserved and the job is still reported as failed.
|
||||||
|
// - otherwise: run on the live context unchanged.
|
||||||
|
func postStepsContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||||
|
switch ctx.Err() {
|
||||||
|
case context.Canceled:
|
||||||
|
// The cancelled context is abandoned for a fresh root, which drops the job-error
|
||||||
|
// container installed at the job root. Re-attach a fresh one so a failing post step
|
||||||
|
// records its error via SetJobError instead of panicking on a nil container.
|
||||||
|
return context.WithTimeout(common.WithJobErrorContainer(common.WithLogger(context.Background(), common.Logger(ctx))), 5*time.Minute)
|
||||||
|
case context.DeadlineExceeded:
|
||||||
|
return context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
|
||||||
|
default:
|
||||||
|
return ctx, func() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// newStepsExecutor sequences the job's pre steps and main steps.
|
||||||
|
//
|
||||||
|
// The pre steps run as a normal pipeline that short-circuits on the first failure or
|
||||||
|
// cancellation. The main-steps executor then runs unconditionally — even if a pre step failed
|
||||||
|
// or the job was interrupted — so always()/cancelled()/failure() main steps still run, mirroring
|
||||||
|
// GitHub Actions. This is safe because each main step re-evaluates its own `if` (a pre-step
|
||||||
|
// failure flips the expression job status to failure, so success()-default steps skip) and
|
||||||
|
// newMainStepsExecutor detaches from an interrupted context before running the remaining steps.
|
||||||
|
//
|
||||||
|
// A pre-step failure or interrupt is still propagated so the job is reported with the correct
|
||||||
|
// conclusion; the pre error takes precedence since it happened first.
|
||||||
|
func newStepsExecutor(rc *RunContext, preSteps, steps []common.Executor) common.Executor {
|
||||||
|
preExecutor := common.NewPipelineExecutor(preSteps...)
|
||||||
|
mainExecutor := newMainStepsExecutor(rc, steps)
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
preErr := preExecutor(ctx)
|
||||||
|
mainErr := mainExecutor(ctx)
|
||||||
|
if preErr != nil {
|
||||||
|
return preErr
|
||||||
|
}
|
||||||
|
return mainErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// newMainStepsExecutor runs the job's main-stage step executors in order. Unlike a plain
|
||||||
|
// pipeline, an interruption (context.Canceled from a server cancel, or context.DeadlineExceeded
|
||||||
|
// from the job timeout) does not abandon the remaining steps: it marks the job cancelled when
|
||||||
|
// appropriate and keeps iterating under a fresh, bounded context so steps whose `if` still
|
||||||
|
// evaluates true — always() and cancelled() — run for cleanup, mirroring GitHub Actions. Steps
|
||||||
|
// that default to success() skip themselves because success() is false once the job is no longer
|
||||||
|
// successful. The main-step wrappers report their own errors and return nil, so the loop drives
|
||||||
|
// step ordering off the context, not return values.
|
||||||
|
func newMainStepsExecutor(rc *RunContext, steps []common.Executor) common.Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
for i, step := range steps {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return runMainStepsAfterInterrupt(ctx, rc, steps[i:])
|
||||||
|
}
|
||||||
|
_ = step(ctx)
|
||||||
|
}
|
||||||
|
// An interrupt can land during the final step, after the loop's last context
|
||||||
|
// check; record it so the post steps still observe the cancelled/failed status.
|
||||||
|
rc.markInterrupted(ctx.Err())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// runMainStepsAfterInterrupt runs the remaining main steps after the job context was cancelled or
|
||||||
|
// timed out. It detaches from the interrupted context (keeping its values: logger and job error)
|
||||||
|
// and applies a fresh deadline so always()/cancelled() steps run to completion. The original
|
||||||
|
// interrupt error is returned so callers up the chain still see the job as cancelled/timed out.
|
||||||
|
func runMainStepsAfterInterrupt(ctx context.Context, rc *RunContext, steps []common.Executor) error {
|
||||||
|
interruptErr := ctx.Err()
|
||||||
|
rc.markInterrupted(interruptErr)
|
||||||
|
freshCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
for _, step := range steps {
|
||||||
|
_ = step(freshCtx)
|
||||||
|
}
|
||||||
|
return interruptErr
|
||||||
|
}
|
||||||
|
|
||||||
func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success bool) {
|
func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success bool) {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
jobResult := "success"
|
// Matrix combinations share one *model.Job and run in parallel; serialize the
|
||||||
// we have only one result for a whole matrix build, so we need
|
// read-modify-write of the job result so a failing combination is not lost-updated by a
|
||||||
// to keep an existing result state if we run a matrix
|
// concurrent succeeding one.
|
||||||
if len(info.matrix()) > 0 && rc.Run.Job().Result != "" {
|
job := rc.Run.Job()
|
||||||
jobResult = rc.Run.Job().Result
|
var continueOnError bool
|
||||||
}
|
|
||||||
|
|
||||||
if !success {
|
if !success {
|
||||||
jobResult = "failure"
|
// Use a fresh context so an expired job timeout cannot block expression evaluation.
|
||||||
|
evalCtx := common.WithLogger(context.Background(), common.Logger(ctx))
|
||||||
|
continueOnError = evaluateJobContinueOnError(evalCtx, rc, job)
|
||||||
}
|
}
|
||||||
|
jobResult := func() string {
|
||||||
|
defer lockJob(job)()
|
||||||
|
result := "success"
|
||||||
|
// we have only one result for a whole matrix build, so we need
|
||||||
|
// to keep an existing result state if we run a matrix
|
||||||
|
if len(info.matrix()) > 0 && job.Result != "" {
|
||||||
|
result = job.Result
|
||||||
|
}
|
||||||
|
if !success {
|
||||||
|
result = "failure"
|
||||||
|
job.SetContinueOnError(continueOnError)
|
||||||
|
}
|
||||||
|
info.result(result)
|
||||||
|
return result
|
||||||
|
}()
|
||||||
|
|
||||||
info.result(jobResult)
|
|
||||||
if rc.caller != nil {
|
if rc.caller != nil {
|
||||||
// set reusable workflow job result
|
// set reusable workflow job result
|
||||||
rc.caller.setReusedWorkflowJobResult(rc.JobName, jobResult) // For Gitea
|
rc.caller.setReusedWorkflowJobResult(rc.JobName, jobResult) // For Gitea
|
||||||
@@ -197,7 +424,7 @@ func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success boo
|
|||||||
jobResultMessage = "failed"
|
jobResultMessage = "failed"
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithField("jobResult", jobResult).Infof("\U0001F3C1 Job %s", jobResultMessage)
|
logger.WithField("jobResult", jobResult).Infof("Job %s", jobResultMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setJobOutputs(ctx context.Context, rc *RunContext) {
|
func setJobOutputs(ctx context.Context, rc *RunContext) {
|
||||||
@@ -211,10 +438,214 @@ func setJobOutputs(ctx context.Context, rc *RunContext) {
|
|||||||
callerOutputs[k] = ee.Interpolate(ctx, ee.Interpolate(ctx, v.Value))
|
callerOutputs[k] = ee.Interpolate(ctx, ee.Interpolate(ctx, v.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.caller.runContext.Run.Job().Outputs = callerOutputs
|
// Matrix combinations of a reusable-workflow caller share the caller's *model.Job;
|
||||||
|
// serialize the write so parallel combos don't race on its Outputs field.
|
||||||
|
callerJob := rc.caller.runContext.Run.Job()
|
||||||
|
defer lockJob(callerJob)()
|
||||||
|
callerJob.Outputs = callerOutputs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// applyJobTimeout applies the job-level timeout-minutes to ctx, mirroring the
|
||||||
|
// step-level evaluateStepTimeout in step.go.
|
||||||
|
func applyJobTimeout(ctx context.Context, rc *RunContext, job *model.Job) (context.Context, context.CancelFunc) {
|
||||||
|
timeout := rc.ExprEval.Interpolate(ctx, job.TimeoutMinutes)
|
||||||
|
if timeout != "" {
|
||||||
|
if timeoutMinutes, err := strconv.ParseInt(timeout, 10, 64); err == nil {
|
||||||
|
return context.WithTimeout(ctx, time.Duration(timeoutMinutes)*time.Minute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx, func() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// evaluateJobContinueOnError evaluates the job-level continue-on-error expression.
|
||||||
|
func evaluateJobContinueOnError(ctx context.Context, rc *RunContext, job *model.Job) bool {
|
||||||
|
expr := strings.TrimSpace(job.RawContinueOnError)
|
||||||
|
if expr == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
continueOnError, err := EvalBool(ctx, rc.NewExpressionEvaluator(ctx), expr, exprparser.DefaultStatusCheckNone)
|
||||||
|
if err != nil {
|
||||||
|
common.Logger(ctx).Warnf("continue-on-error expression %q evaluation failed: %v", expr, err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return continueOnError
|
||||||
|
}
|
||||||
|
|
||||||
|
func tryUploadJobSummary(ctx context.Context, rc *RunContext) {
|
||||||
|
if rc == nil || rc.JobContainer == nil || rc.Config == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Bound the whole upload phase so a slow or unreachable endpoint cannot consume
|
||||||
|
// the job cleanup budget reserved for stopping and removing the container.
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, jobSummaryUploadPhaseTimeout)
|
||||||
|
defer cancel()
|
||||||
|
env := rc.GetEnv()
|
||||||
|
caps := strings.TrimSpace(env["GITEA_ACTIONS_CAPABILITIES"])
|
||||||
|
if !hasJobSummaryCapability(caps) {
|
||||||
|
// Server did not advertise support. Do not attempt upload.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
runtimeURL := strings.TrimSpace(env["ACTIONS_RUNTIME_URL"])
|
||||||
|
runtimeToken := strings.TrimSpace(env["ACTIONS_RUNTIME_TOKEN"])
|
||||||
|
runID := strings.TrimSpace(env["GITEA_RUN_ID"])
|
||||||
|
if runtimeURL == "" || runtimeToken == "" || runID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if rc.Run == nil || rc.Run.Job() == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// The numeric ActionRunJob ID is not exposed in the proto Task message or task context,
|
||||||
|
// but the server signs it into the ACTIONS_RUNTIME_TOKEN JWT claims. We decode the
|
||||||
|
// unverified claims to retrieve it; the server re-verifies the token on the request.
|
||||||
|
jobID := extractJobIDFromRuntimeToken(runtimeToken)
|
||||||
|
if jobID <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
base := strings.TrimRight(runtimeURL, "/") + "/_apis/pipelines/workflows/" + runID +
|
||||||
|
"/jobs/" + strconv.FormatInt(jobID, 10) + "/steps/"
|
||||||
|
actPath := rc.JobContainer.GetActPath()
|
||||||
|
// Reuse a single client across all step uploads so connections can be pooled.
|
||||||
|
client := &http.Client{Timeout: jobSummaryUploadRequestTimeout}
|
||||||
|
for i := range rc.Run.Job().Steps {
|
||||||
|
summaryPath := path.Join(actPath, "workflow", "step-summary-"+strconv.Itoa(i)+".md")
|
||||||
|
body, ok := readSingleFileFromContainerArchive(ctx, rc.JobContainer, summaryPath, maxJobSummaryBytes)
|
||||||
|
if !ok || len(body) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
uploadJobSummary(ctx, client, base+strconv.Itoa(i)+"/summary", runtimeToken, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractJobIDFromRuntimeToken returns the JobID claim from an ACTIONS_RUNTIME_TOKEN JWT
|
||||||
|
// without verifying its signature. Returns 0 if the token is unparseable or has no JobID.
|
||||||
|
func extractJobIDFromRuntimeToken(token string) int64 {
|
||||||
|
parts := strings.Split(token, ".")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var claims struct {
|
||||||
|
JobID int64 `json:"JobID"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(payload, &claims); err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return claims.JobID
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasJobSummaryCapability(caps string) bool {
|
||||||
|
return slices.Contains(strings.FieldsFunc(caps, func(r rune) bool {
|
||||||
|
return r == ',' || unicode.IsSpace(r)
|
||||||
|
}), "job-summary")
|
||||||
|
}
|
||||||
|
|
||||||
|
func uploadJobSummary(ctx context.Context, client *http.Client, url, runtimeToken string, body []byte) {
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
|
var lastStatus int
|
||||||
|
var lastErr error
|
||||||
|
for attempt := 0; attempt < 2; attempt++ {
|
||||||
|
status, err := putJobSummary(ctx, client, url, runtimeToken, body)
|
||||||
|
if err == nil && status/100 == 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lastStatus = status
|
||||||
|
lastErr = err
|
||||||
|
if attempt == 1 || !isTransientJobSummaryUploadFailure(status, err) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
timer := time.NewTimer(jobSummaryUploadRetryDelay)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
timer.Stop()
|
||||||
|
lastErr = ctx.Err()
|
||||||
|
attempt = 1
|
||||||
|
case <-timer.C:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Best-effort only; do not fail job, but log because capability was advertised.
|
||||||
|
if lastErr != nil {
|
||||||
|
logger.WithError(lastErr).Warn("job summary upload failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Warnf("job summary upload failed: status=%d", lastStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
func putJobSummary(ctx context.Context, client *http.Client, url, runtimeToken string, body []byte) (int, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", "Bearer "+runtimeToken)
|
||||||
|
req.Header.Set("Content-Type", "text/markdown; charset=utf-8")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
_, _ = io.Copy(io.Discard, resp.Body)
|
||||||
|
return resp.StatusCode, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isTransientJobSummaryUploadFailure(status int, err error) bool {
|
||||||
|
return err != nil || status == http.StatusRequestTimeout || status == http.StatusTooManyRequests || status/100 == 5
|
||||||
|
}
|
||||||
|
|
||||||
|
func readSingleFileFromContainerArchive(ctx context.Context, env container.ExecutionsEnvironment, p string, maxBytes int64) ([]byte, bool) {
|
||||||
|
rc, err := env.GetContainerArchive(ctx, p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
defer rc.Close()
|
||||||
|
|
||||||
|
tr := tar.NewReader(rc)
|
||||||
|
for {
|
||||||
|
header, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
if header.Typeflag != tar.TypeReg {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !archiveEntryMatchesPath(header.Name, p) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Summaries larger than the limit are truncated rather than dropped, so the
|
||||||
|
// user still gets the leading content (mirroring how GitHub caps oversized
|
||||||
|
// step summaries instead of discarding them). Read one extra byte so an
|
||||||
|
// over-limit file is detected from the actual stream rather than trusting
|
||||||
|
// header.Size, then cap the returned content at maxBytes.
|
||||||
|
b, err := io.ReadAll(io.LimitReader(tr, maxBytes+1))
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
if int64(len(b)) > maxBytes {
|
||||||
|
// Reserve room for the marker so the marked-up result still fits in maxBytes.
|
||||||
|
marker := []byte(jobSummaryTruncationMarker)
|
||||||
|
keep := max(maxBytes-int64(len(marker)), 0)
|
||||||
|
b = append(b[:keep], marker...)
|
||||||
|
common.Logger(ctx).Warnf("job summary truncated: path=%s max=%d", p, maxBytes)
|
||||||
|
}
|
||||||
|
return b, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func archiveEntryMatchesPath(entryName, requestedPath string) bool {
|
||||||
|
entryName = path.Clean(strings.TrimPrefix(entryName, "/"))
|
||||||
|
requestedPath = path.Clean(strings.TrimPrefix(requestedPath, "/"))
|
||||||
|
return entryName == requestedPath || entryName == path.Base(requestedPath)
|
||||||
|
}
|
||||||
|
|
||||||
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, rc.ExprEval.Interpolate(ctx, stepModel.String()), stage.String())
|
ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, rc.ExprEval.Interpolate(ctx, stepModel.String()), stage.String())
|
||||||
@@ -232,6 +663,11 @@ func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, execu
|
|||||||
oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
|
oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
|
||||||
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)
|
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)
|
||||||
|
|
||||||
|
// Flush any buffered, not-yet-newline-terminated trailing line once the
|
||||||
|
// step has finished, so the final line of the step's output is not lost
|
||||||
|
// when it is not newline-terminated.
|
||||||
|
defer common.FlushWriter(logWriter)
|
||||||
|
|
||||||
return executor(ctx)
|
return executor(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,40 +5,49 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/act_runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/act_runner/act/model"
|
"gitea.com/gitea/runner/act/model"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
logrustest "github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
yaml "go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJobExecutor(t *testing.T) {
|
func TestJobExecutor(t *testing.T) {
|
||||||
if testing.Short() {
|
t.Parallel()
|
||||||
t.Skip("skipping integration test")
|
// Dryrun only checks syntax/planning; all cases resolve locally, so this runs offline.
|
||||||
}
|
|
||||||
tables := []TestJobFileInfo{
|
tables := []TestJobFileInfo{
|
||||||
{workdir, "uses-and-run-in-one-step", "push", "Invalid run/uses syntax for job:test step:Test", platforms, secrets},
|
{workdir, "uses-and-run-in-one-step", "push", "Invalid run/uses syntax for job:test step:Test", platforms, secrets},
|
||||||
{workdir, "uses-github-empty", "push", "Expected format {org}/{repo}[/path]@ref", platforms, secrets},
|
{workdir, "uses-github-empty", "push", "Expected format {org}/{repo}[/path]@ref", platforms, secrets},
|
||||||
{workdir, "uses-github-noref", "push", "Expected format {org}/{repo}[/path]@ref", platforms, secrets},
|
{workdir, "uses-github-noref", "push", "Expected format {org}/{repo}[/path]@ref", platforms, secrets},
|
||||||
{workdir, "uses-github-root", "push", "", platforms, secrets},
|
{workdir, "uses-github-root", "push", "", platforms, secrets},
|
||||||
{workdir, "uses-github-path", "push", "", platforms, secrets},
|
|
||||||
{workdir, "uses-docker-url", "push", "", platforms, secrets},
|
{workdir, "uses-docker-url", "push", "", platforms, secrets},
|
||||||
{workdir, "uses-github-full-sha", "push", "", platforms, secrets},
|
|
||||||
{workdir, "uses-github-short-sha", "push", "Unable to resolve action `actions/hello-world-docker-action@b136eb8`, the provided ref `b136eb8` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `b136eb8894c5cb1dd5807da824be97ccdf9b5423` instead", platforms, secrets},
|
|
||||||
{workdir, "job-nil-step", "push", "invalid Step 0: missing run or uses key", platforms, secrets},
|
{workdir, "job-nil-step", "push", "invalid Step 0: missing run or uses key", platforms, secrets},
|
||||||
}
|
}
|
||||||
// These tests are sufficient to only check syntax.
|
// These tests are sufficient to only check syntax.
|
||||||
ctx := common.WithDryrun(context.Background(), true)
|
ctx := common.WithDryrun(context.Background(), true)
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.workflowPath, func(t *testing.T) {
|
t.Run(table.workflowPath, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
table.runTest(ctx, t, &Config{})
|
table.runTest(ctx, t, &Config{})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -105,6 +114,182 @@ func (sfm *stepFactoryMock) newStep(model *model.Step, rc *RunContext) (step, er
|
|||||||
return args.Get(0).(step), args.Error(1)
|
return args.Get(0).(step), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// actionPreparerMock stands in for a step whose action is downloaded before the job's first step.
|
||||||
|
type actionPreparerMock struct {
|
||||||
|
reference string
|
||||||
|
sha string
|
||||||
|
ok bool
|
||||||
|
err error
|
||||||
|
prepared int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apm *actionPreparerMock) prepareActionExecutor() common.Executor {
|
||||||
|
return func(context.Context) error {
|
||||||
|
apm.prepared++
|
||||||
|
return apm.err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (apm *actionPreparerMock) actionDownloadInfo() (string, string, bool) {
|
||||||
|
return apm.reference, apm.sha, apm.ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintPrepareActionsGolden(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
logger := log.New()
|
||||||
|
logger.SetOutput(buf)
|
||||||
|
logger.SetLevel(log.InfoLevel)
|
||||||
|
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
||||||
|
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
||||||
|
|
||||||
|
preparers := []actionPreparer{
|
||||||
|
&actionPreparerMock{reference: "actions/checkout@v7", sha: "9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0", ok: true},
|
||||||
|
// A resolved commit is best effort; the ref alone is reported when it is unknown.
|
||||||
|
&actionPreparerMock{reference: "actions/setup-go@v6", ok: true},
|
||||||
|
// A step that downloads nothing, such as the checkout of the workflow's own repository.
|
||||||
|
&actionPreparerMock{ok: false},
|
||||||
|
}
|
||||||
|
require.NoError(t, printPrepareActions(&RunContext{}, preparers)(ctx))
|
||||||
|
|
||||||
|
want := strings.Join([]string{
|
||||||
|
"[j1] | Prepare all required actions",
|
||||||
|
"[j1] | Download action repository 'actions/checkout@v7' (SHA:9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0)",
|
||||||
|
"[j1] | Download action repository 'actions/setup-go@v6'",
|
||||||
|
"",
|
||||||
|
}, "\n")
|
||||||
|
assert.Equal(t, want, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintPrepareActionsSkipsWithoutActions(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
logger := log.New()
|
||||||
|
logger.SetOutput(buf)
|
||||||
|
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
||||||
|
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
||||||
|
|
||||||
|
require.NoError(t, printPrepareActions(&RunContext{}, nil)(ctx))
|
||||||
|
|
||||||
|
assert.Empty(t, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintPrepareActionsFailsJobOnDownloadError(t *testing.T) {
|
||||||
|
logger, _ := logrustest.NewNullLogger()
|
||||||
|
ctx := common.WithJobErrorContainer(common.WithLogger(context.Background(), logger.WithField("job", "j1")))
|
||||||
|
|
||||||
|
downloadErr := errors.New("failed to fetch \"actions/checkout\"")
|
||||||
|
rc := &RunContext{}
|
||||||
|
remaining := &actionPreparerMock{reference: "actions/setup-go@v6", ok: true}
|
||||||
|
|
||||||
|
err := printPrepareActions(rc, []actionPreparer{
|
||||||
|
&actionPreparerMock{err: downloadErr},
|
||||||
|
remaining,
|
||||||
|
})(ctx)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, downloadErr)
|
||||||
|
// No step has run yet, so the failure has to be recorded against the job itself.
|
||||||
|
assert.Equal(t, downloadErr, common.JobError(ctx))
|
||||||
|
assert.True(t, rc.jobFailed)
|
||||||
|
assert.Zero(t, remaining.prepared)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintCompleteJobName(t *testing.T) {
|
||||||
|
for name, tt := range map[string]struct {
|
||||||
|
rc *RunContext
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
"job name": {rc: &RunContext{JobName: "lint", Name: "lint-1"}, want: "lint"},
|
||||||
|
"falls back to name": {rc: &RunContext{Name: "lint-1"}, want: "lint-1"},
|
||||||
|
"falls back to jobID": {rc: &RunContext{Run: &model.Run{JobID: "lint"}}, want: "lint"},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
logger := log.New()
|
||||||
|
logger.SetOutput(buf)
|
||||||
|
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
||||||
|
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
||||||
|
|
||||||
|
require.NoError(t, printCompleteJobName(tt.rc)(ctx))
|
||||||
|
|
||||||
|
assert.Equal(t, "[j1] | Complete job name: "+tt.want+"\n", buf.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// actionStepMock is a step whose action has to be downloaded before it can run.
|
||||||
|
type actionStepMock struct {
|
||||||
|
*stepMock
|
||||||
|
*actionPreparerMock
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep pins the shape of the setup section:
|
||||||
|
// every action is downloaded before any step runs, and the job name closes the section. A pre
|
||||||
|
// step that downloaded its own action would leave the log interleaved with the downloads.
|
||||||
|
func TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep(t *testing.T) {
|
||||||
|
ctx := common.WithJobErrorContainer(context.Background())
|
||||||
|
jim := &jobInfoMock{}
|
||||||
|
sfm := &stepFactoryMock{}
|
||||||
|
rc := &RunContext{
|
||||||
|
JobContainer: &jobContainerMock{},
|
||||||
|
Run: &model.Run{
|
||||||
|
JobID: "test",
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Jobs: map[string]*model.Job{"test": {}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Config: &Config{},
|
||||||
|
}
|
||||||
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
||||||
|
|
||||||
|
steps := []*model.Step{{ID: "1"}, {ID: "2"}}
|
||||||
|
executorOrder := make([]string, 0)
|
||||||
|
|
||||||
|
jim.On("steps").Return(steps)
|
||||||
|
jim.On("matrix").Return(map[string]any{})
|
||||||
|
jim.On("startContainer").Return(func(context.Context) error { return nil })
|
||||||
|
jim.On("stopContainer").Return(func(context.Context) error { return nil })
|
||||||
|
jim.On("closeContainer").Return(func(context.Context) error { return nil })
|
||||||
|
jim.On("interpolateOutputs").Return(func(context.Context) error { return nil })
|
||||||
|
jim.On("result", "success")
|
||||||
|
|
||||||
|
for _, stepModel := range steps {
|
||||||
|
sm := &stepMock{}
|
||||||
|
apm := &actionPreparerMock{reference: "actions/checkout@v" + stepModel.ID, ok: true}
|
||||||
|
sfm.On("newStep", stepModel, rc).Return(&actionStepMock{stepMock: sm, actionPreparerMock: apm}, nil)
|
||||||
|
|
||||||
|
sm.On("pre").Return(func(context.Context) error {
|
||||||
|
executorOrder = append(executorOrder, "pre"+stepModel.ID)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
sm.On("main").Return(func(context.Context) error {
|
||||||
|
executorOrder = append(executorOrder, "step"+stepModel.ID)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
sm.On("post").Return(func(context.Context) error { return nil })
|
||||||
|
|
||||||
|
defer sm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger, hook := logrustest.NewNullLogger()
|
||||||
|
err := newJobExecutor(jim, sfm, rc)(common.WithLogger(ctx, logger.WithField("job", "test")))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"pre1", "pre2", "step1", "step2"}, executorOrder)
|
||||||
|
|
||||||
|
setup := make([]string, 0)
|
||||||
|
for _, entry := range hook.AllEntries() {
|
||||||
|
if strings.HasPrefix(entry.Message, "Prepare all required actions") || strings.HasPrefix(entry.Message, "Download action") ||
|
||||||
|
strings.HasPrefix(entry.Message, "Complete job name") {
|
||||||
|
setup = append(setup, entry.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(t, []string{
|
||||||
|
"Prepare all required actions",
|
||||||
|
"Download action repository 'actions/checkout@v1'",
|
||||||
|
"Download action repository 'actions/checkout@v2'",
|
||||||
|
"Complete job name: test",
|
||||||
|
}, setup)
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewJobExecutor(t *testing.T) {
|
func TestNewJobExecutor(t *testing.T) {
|
||||||
table := []struct {
|
table := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -331,7 +516,7 @@ func TestNewJobExecutor(t *testing.T) {
|
|||||||
|
|
||||||
executor := newJobExecutor(jim, sfm, rc)
|
executor := newJobExecutor(jim, sfm, rc)
|
||||||
err := executor(ctx)
|
err := executor(ctx)
|
||||||
assert.Nil(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.executedSteps, executorOrder)
|
assert.Equal(t, tt.executedSteps, executorOrder)
|
||||||
|
|
||||||
jim.AssertExpectations(t)
|
jim.AssertExpectations(t)
|
||||||
@@ -341,3 +526,559 @@ func TestNewJobExecutor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestNewJobExecutorRunsPostStepsAfterTimeout guards the timeout-minutes cleanup
|
||||||
|
// path: when a job exceeds its timeout the job context is DeadlineExceeded, but
|
||||||
|
// the post steps (cleanup hooks like actions/checkout post and cache save) must
|
||||||
|
// still run against a fresh, non-expired context, and the job must still be
|
||||||
|
// reported as failed.
|
||||||
|
func TestNewJobExecutorRunsPostStepsAfterTimeout(t *testing.T) {
|
||||||
|
ctx := common.WithJobErrorContainer(context.Background())
|
||||||
|
// The timeout is generous so the main step (which blocks on ctx.Done below) is
|
||||||
|
// always reached before the deadline fires; otherwise the pipeline would
|
||||||
|
// short-circuit before the step runs and the job error would never be set.
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
jim := &jobInfoMock{}
|
||||||
|
sfm := &stepFactoryMock{}
|
||||||
|
rc := &RunContext{
|
||||||
|
JobContainer: &jobContainerMock{},
|
||||||
|
Run: &model.Run{
|
||||||
|
JobID: "test",
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Jobs: map[string]*model.Job{
|
||||||
|
"test": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Config: &Config{},
|
||||||
|
}
|
||||||
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
||||||
|
|
||||||
|
stepModel := &model.Step{ID: "1"}
|
||||||
|
jim.On("steps").Return([]*model.Step{stepModel})
|
||||||
|
jim.On("matrix").Return(map[string]any{})
|
||||||
|
jim.On("startContainer").Return(func(ctx context.Context) error { return nil })
|
||||||
|
jim.On("interpolateOutputs").Return(func(ctx context.Context) error { return nil })
|
||||||
|
jim.On("closeContainer").Return(func(ctx context.Context) error { return nil })
|
||||||
|
// The job timed out, so it must be reported as failed. stopContainer is left
|
||||||
|
// unexpected on purpose: a timed-out (failed) job preserves its error state, so
|
||||||
|
// the graceful stop is skipped exactly like any other failure without AutoRemove.
|
||||||
|
jim.On("result", "failure")
|
||||||
|
|
||||||
|
sm := &stepMock{}
|
||||||
|
sfm.On("newStep", stepModel, rc).Return(sm, nil)
|
||||||
|
sm.On("pre").Return(func(ctx context.Context) error { return nil })
|
||||||
|
// The main step runs past the job timeout: it blocks until the job context is
|
||||||
|
// done, mirroring a step that overruns timeout-minutes.
|
||||||
|
sm.On("main").Return(func(ctx context.Context) error {
|
||||||
|
<-ctx.Done()
|
||||||
|
return ctx.Err()
|
||||||
|
})
|
||||||
|
|
||||||
|
var postRan bool
|
||||||
|
var postCtxErr error
|
||||||
|
sm.On("post").Return(func(ctx context.Context) error {
|
||||||
|
postRan = true
|
||||||
|
postCtxErr = ctx.Err()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
executor := newJobExecutor(jim, sfm, rc)
|
||||||
|
// The executor itself returns nil on timeout: the failure is surfaced through
|
||||||
|
// the job result ("failure", asserted via the result mock below), not the
|
||||||
|
// return value.
|
||||||
|
require.NoError(t, executor(ctx))
|
||||||
|
|
||||||
|
assert.True(t, postRan, "post step must run after a job timeout")
|
||||||
|
require.NoError(t, postCtxErr, "post step must run against a fresh, non-expired context")
|
||||||
|
|
||||||
|
jim.AssertExpectations(t)
|
||||||
|
sfm.AssertExpectations(t)
|
||||||
|
sm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSetJobResultMatrixContinueOnError exercises the parallel-matrix path
|
||||||
|
// end-to-end: two combinations share one *model.Job and continue-on-error is
|
||||||
|
// keyed on matrix.experimental, so one combination tolerates its failure and the
|
||||||
|
// other does not. The job is reported as continue-on-error only when EVERY failing
|
||||||
|
// combination was tolerated; a single firm failure makes the whole job firm, and
|
||||||
|
// handleFailure then fails the run.
|
||||||
|
func TestSetJobResultMatrixContinueOnError(t *testing.T) {
|
||||||
|
const jobYAML = "continue-on-error: ${{ matrix.experimental }}\nruns-on: ubuntu-latest"
|
||||||
|
|
||||||
|
newSharedJob := func(t *testing.T) (*model.Job, *model.Workflow) {
|
||||||
|
t.Helper()
|
||||||
|
var job *model.Job
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte(jobYAML), &job))
|
||||||
|
return job, &model.Workflow{
|
||||||
|
Name: "workflow1",
|
||||||
|
Jobs: map[string]*model.Job{"job1": job},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
planFor := func(wf *model.Workflow) *model.Plan {
|
||||||
|
return &model.Plan{Stages: []*model.Stage{{Runs: []*model.Run{{Workflow: wf, JobID: "job1"}}}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// fail drives a single matrix combination through the failure path; each
|
||||||
|
// RunContext is its own jobInfo (rc implements jobInfo) and shares the job.
|
||||||
|
fail := func(wf *model.Workflow, experimental bool) {
|
||||||
|
rc := newTestRC(wf, map[string]any{"experimental": experimental})
|
||||||
|
setJobResult(ctx, rc, rc, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("one tolerated and one firm failure fails the run", func(t *testing.T) {
|
||||||
|
job, wf := newSharedJob(t)
|
||||||
|
// Order is intentional: the tolerated combination finishes first, then the
|
||||||
|
// firm one. The firm-failure latch must still win regardless of order.
|
||||||
|
fail(wf, true)
|
||||||
|
fail(wf, false)
|
||||||
|
|
||||||
|
assert.Equal(t, "failure", job.Result)
|
||||||
|
assert.False(t, job.ContinueOnError, "a single firm failure must make the whole job firm")
|
||||||
|
assert.Error(t, handleFailure(planFor(wf))(ctx))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("all tolerated failures do not fail the run", func(t *testing.T) {
|
||||||
|
job, wf := newSharedJob(t)
|
||||||
|
fail(wf, true)
|
||||||
|
fail(wf, true)
|
||||||
|
|
||||||
|
assert.Equal(t, "failure", job.Result)
|
||||||
|
assert.True(t, job.ContinueOnError, "every failing combination was tolerated")
|
||||||
|
assert.NoError(t, handleFailure(planFor(wf))(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasJobSummaryCapability(t *testing.T) {
|
||||||
|
assert.True(t, hasJobSummaryCapability("cache,job-summary artifacts"))
|
||||||
|
assert.True(t, hasJobSummaryCapability("cache,\njob-summary\tartifacts"))
|
||||||
|
assert.False(t, hasJobSummaryCapability("not-job-summary,job-summary-v2"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeRuntimeToken builds a JWT-shaped string whose middle (claims) segment encodes
|
||||||
|
// the given JobID. The header and signature segments are filler — the runner does not
|
||||||
|
// verify the signature; the server does.
|
||||||
|
func fakeRuntimeToken(jobID int64) string {
|
||||||
|
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`))
|
||||||
|
claims := base64.RawURLEncoding.EncodeToString(fmt.Appendf(nil, `{"JobID":%d}`, jobID))
|
||||||
|
sig := base64.RawURLEncoding.EncodeToString([]byte("sig"))
|
||||||
|
return header + "." + claims + "." + sig
|
||||||
|
}
|
||||||
|
|
||||||
|
func newJobSummaryRC(env map[string]string, jobContainer container.ExecutionsEnvironment, stepCount int) *RunContext {
|
||||||
|
steps := make([]*model.Step, stepCount)
|
||||||
|
for i := range steps {
|
||||||
|
steps[i] = &model.Step{ID: strconv.Itoa(i)}
|
||||||
|
}
|
||||||
|
return &RunContext{
|
||||||
|
Config: &Config{},
|
||||||
|
JobContainer: jobContainer,
|
||||||
|
Env: env,
|
||||||
|
Run: &model.Run{
|
||||||
|
JobID: "test",
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Jobs: map[string]*model.Job{
|
||||||
|
"test": {Steps: steps},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTryUploadJobSummaryRetriesTransientFailure(t *testing.T) {
|
||||||
|
oldDelay := jobSummaryUploadRetryDelay
|
||||||
|
jobSummaryUploadRetryDelay = 0
|
||||||
|
defer func() {
|
||||||
|
jobSummaryUploadRetryDelay = oldDelay
|
||||||
|
}()
|
||||||
|
|
||||||
|
runtimeToken := fakeRuntimeToken(34)
|
||||||
|
|
||||||
|
requests := 0
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
requests++
|
||||||
|
assert.Equal(t, http.MethodPut, r.Method)
|
||||||
|
assert.Equal(t, "/_apis/pipelines/workflows/12/jobs/34/steps/0/summary", r.URL.Path)
|
||||||
|
assert.Equal(t, "Bearer "+runtimeToken, r.Header.Get("Authorization"))
|
||||||
|
assert.Equal(t, "text/markdown; charset=utf-8", r.Header.Get("Content-Type"))
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []byte("# summary"), body)
|
||||||
|
if requests == 1 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
cm := &containerMock{}
|
||||||
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
rc := newJobSummaryRC(map[string]string{
|
||||||
|
"GITEA_ACTIONS_CAPABILITIES": "cache, job-summary",
|
||||||
|
"ACTIONS_RUNTIME_URL": server.URL,
|
||||||
|
"ACTIONS_RUNTIME_TOKEN": runtimeToken,
|
||||||
|
"GITEA_RUN_ID": "12",
|
||||||
|
}, cm, 1)
|
||||||
|
|
||||||
|
tryUploadJobSummary(ctx, rc)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, requests)
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTryUploadJobSummaryStopsAtPhaseTimeout(t *testing.T) {
|
||||||
|
oldPhase := jobSummaryUploadPhaseTimeout
|
||||||
|
jobSummaryUploadPhaseTimeout = 100 * time.Millisecond
|
||||||
|
defer func() {
|
||||||
|
jobSummaryUploadPhaseTimeout = oldPhase
|
||||||
|
}()
|
||||||
|
|
||||||
|
runtimeToken := fakeRuntimeToken(34)
|
||||||
|
|
||||||
|
// The server blocks until either the request context is cancelled (the behaviour
|
||||||
|
// under test: the phase timeout aborts the in-flight upload) or the test tears it
|
||||||
|
// down. Without the phase timeout the upload would hang until the 30s client
|
||||||
|
// timeout instead of releasing the cleanup budget. The release channel guarantees
|
||||||
|
// the handler always returns so server.Close() cannot itself hang.
|
||||||
|
release := make(chan struct{})
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
select {
|
||||||
|
case <-r.Context().Done():
|
||||||
|
case <-release:
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
defer close(release)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
cm := &containerMock{}
|
||||||
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
rc := newJobSummaryRC(map[string]string{
|
||||||
|
"GITEA_ACTIONS_CAPABILITIES": "job-summary",
|
||||||
|
"ACTIONS_RUNTIME_URL": server.URL,
|
||||||
|
"ACTIONS_RUNTIME_TOKEN": runtimeToken,
|
||||||
|
"GITEA_RUN_ID": "12",
|
||||||
|
}, cm, 1)
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
tryUploadJobSummary(ctx, rc)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("tryUploadJobSummary did not honour the phase timeout")
|
||||||
|
}
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTryUploadJobSummaryUploadsEachStepIndependently(t *testing.T) {
|
||||||
|
runtimeToken := fakeRuntimeToken(34)
|
||||||
|
|
||||||
|
type upload struct {
|
||||||
|
path string
|
||||||
|
body string
|
||||||
|
}
|
||||||
|
var got []upload
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
got = append(got, upload{r.URL.Path, string(body)})
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
cm := &containerMock{}
|
||||||
|
// Three steps: 0 has content, 1 has empty content (skipped), 2 has content.
|
||||||
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "first"}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-1.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-1.md", body: ""}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-2.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-2.md", body: "third"}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
rc := newJobSummaryRC(map[string]string{
|
||||||
|
"GITEA_ACTIONS_CAPABILITIES": "job-summary",
|
||||||
|
"ACTIONS_RUNTIME_URL": server.URL,
|
||||||
|
"ACTIONS_RUNTIME_TOKEN": runtimeToken,
|
||||||
|
"GITEA_RUN_ID": "12",
|
||||||
|
}, cm, 3)
|
||||||
|
|
||||||
|
tryUploadJobSummary(ctx, rc)
|
||||||
|
|
||||||
|
assert.Equal(t, []upload{
|
||||||
|
{"/_apis/pipelines/workflows/12/jobs/34/steps/0/summary", "first"},
|
||||||
|
{"/_apis/pipelines/workflows/12/jobs/34/steps/2/summary", "third"},
|
||||||
|
}, got)
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTryUploadJobSummaryRequiresExactCapability(t *testing.T) {
|
||||||
|
requests := 0
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
requests++
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
rc := newJobSummaryRC(map[string]string{
|
||||||
|
"GITEA_ACTIONS_CAPABILITIES": "not-job-summary,job-summary-v2",
|
||||||
|
"ACTIONS_RUNTIME_URL": server.URL,
|
||||||
|
"ACTIONS_RUNTIME_TOKEN": fakeRuntimeToken(34),
|
||||||
|
"GITEA_RUN_ID": "12",
|
||||||
|
}, &containerMock{}, 1)
|
||||||
|
|
||||||
|
tryUploadJobSummary(context.Background(), rc)
|
||||||
|
|
||||||
|
assert.Equal(t, 0, requests)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTryUploadJobSummarySkipsWhenJobIDMissingFromToken(t *testing.T) {
|
||||||
|
requests := 0
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
requests++
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
rc := newJobSummaryRC(map[string]string{
|
||||||
|
"GITEA_ACTIONS_CAPABILITIES": "job-summary",
|
||||||
|
"ACTIONS_RUNTIME_URL": server.URL,
|
||||||
|
"ACTIONS_RUNTIME_TOKEN": "not-a-jwt",
|
||||||
|
"GITEA_RUN_ID": "12",
|
||||||
|
}, &containerMock{}, 1)
|
||||||
|
|
||||||
|
tryUploadJobSummary(context.Background(), rc)
|
||||||
|
|
||||||
|
assert.Equal(t, 0, requests)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractJobIDFromRuntimeToken(t *testing.T) {
|
||||||
|
assert.Equal(t, int64(42), extractJobIDFromRuntimeToken(fakeRuntimeToken(42)))
|
||||||
|
assert.Equal(t, int64(0), extractJobIDFromRuntimeToken("not-a-jwt"))
|
||||||
|
assert.Equal(t, int64(0), extractJobIDFromRuntimeToken("a.b.c"))
|
||||||
|
assert.Equal(t, int64(0), extractJobIDFromRuntimeToken(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadSingleFileFromContainerArchiveFindsMatchingRegularFile(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
cm := &containerMock{}
|
||||||
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t,
|
||||||
|
tarEntry{name: "workflow", typeflag: tar.TypeDir},
|
||||||
|
tarEntry{name: "other.md", body: "wrong"},
|
||||||
|
tarEntry{name: "SUMMARY.md", body: "right"},
|
||||||
|
))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
body, ok := readSingleFileFromContainerArchive(ctx, cm, "/var/run/act/workflow/SUMMARY.md", 1024)
|
||||||
|
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, []byte("right"), body)
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadSingleFileFromContainerArchiveTruncatesWhenTooLarge(t *testing.T) {
|
||||||
|
logger, hook := logrustest.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
cm := &containerMock{}
|
||||||
|
content := strings.Repeat("a", 300)
|
||||||
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "SUMMARY.md", body: content}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
const maxBytes = 200
|
||||||
|
body, ok := readSingleFileFromContainerArchive(ctx, cm, "/var/run/act/workflow/SUMMARY.md", maxBytes)
|
||||||
|
|
||||||
|
// Oversized summaries are truncated to the limit (reserving room for the marker)
|
||||||
|
// rather than dropped entirely, and the truncation marker is appended.
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.LessOrEqual(t, len(body), maxBytes)
|
||||||
|
keep := maxBytes - len(jobSummaryTruncationMarker)
|
||||||
|
assert.Equal(t, []byte(content[:keep]+jobSummaryTruncationMarker), body)
|
||||||
|
if assert.Len(t, hook.Entries, 1) {
|
||||||
|
assert.Contains(t, hook.Entries[0].Message, "job summary truncated")
|
||||||
|
}
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadSingleFileFromContainerArchiveKeepsExactLimitWithoutWarning(t *testing.T) {
|
||||||
|
logger, hook := logrustest.NewNullLogger()
|
||||||
|
ctx := common.WithLogger(context.Background(), logger)
|
||||||
|
cm := &containerMock{}
|
||||||
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "SUMMARY.md", body: "abc"}))),
|
||||||
|
nil,
|
||||||
|
).Once()
|
||||||
|
|
||||||
|
body, ok := readSingleFileFromContainerArchive(ctx, cm, "/var/run/act/workflow/SUMMARY.md", 3)
|
||||||
|
|
||||||
|
// A summary that is exactly at the limit is kept whole and not flagged as truncated.
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, []byte("abc"), body)
|
||||||
|
assert.Empty(t, hook.Entries)
|
||||||
|
cm.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
type tarEntry struct {
|
||||||
|
name string
|
||||||
|
body string
|
||||||
|
typeflag byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func tarArchive(t *testing.T, entries ...tarEntry) []byte {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
tw := tar.NewWriter(buf)
|
||||||
|
for _, entry := range entries {
|
||||||
|
typeflag := entry.typeflag
|
||||||
|
if typeflag == 0 {
|
||||||
|
typeflag = tar.TypeReg
|
||||||
|
}
|
||||||
|
header := &tar.Header{
|
||||||
|
Name: entry.name,
|
||||||
|
Typeflag: typeflag,
|
||||||
|
Mode: 0o644,
|
||||||
|
Size: int64(len(entry.body)),
|
||||||
|
}
|
||||||
|
if typeflag == tar.TypeDir {
|
||||||
|
header.Mode = 0o755
|
||||||
|
header.Size = 0
|
||||||
|
}
|
||||||
|
require.NoError(t, tw.WriteHeader(header))
|
||||||
|
if typeflag == tar.TypeReg {
|
||||||
|
_, err := tw.Write([]byte(entry.body))
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.NoError(t, tw.Close())
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestRC(wf *model.Workflow, matrix map[string]any) *RunContext {
|
||||||
|
return &RunContext{
|
||||||
|
Config: &Config{
|
||||||
|
Workdir: ".",
|
||||||
|
Platforms: map[string]string{
|
||||||
|
"ubuntu-latest": "ubuntu-latest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
StepResults: map[string]*model.StepResult{},
|
||||||
|
Env: map[string]string{},
|
||||||
|
Matrix: matrix,
|
||||||
|
Run: &model.Run{JobID: "job1", Workflow: wf},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTestRC(t *testing.T, jobYAML string) *RunContext {
|
||||||
|
t.Helper()
|
||||||
|
var job *model.Job
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte(jobYAML), &job))
|
||||||
|
rc := newTestRC(&model.Workflow{
|
||||||
|
Name: "workflow1",
|
||||||
|
Jobs: map[string]*model.Job{"job1": job},
|
||||||
|
}, nil)
|
||||||
|
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
||||||
|
return rc
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApplyJobTimeout(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
yaml string
|
||||||
|
wantTimeout bool
|
||||||
|
}{
|
||||||
|
{"empty", "runs-on: ubuntu-latest", false},
|
||||||
|
{"integer", "timeout-minutes: 5\nruns-on: ubuntu-latest", true},
|
||||||
|
{"non-numeric ignored", "timeout-minutes: abc\nruns-on: ubuntu-latest", false},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
rc := makeTestRC(t, tc.yaml)
|
||||||
|
ctx := context.Background()
|
||||||
|
newCtx, cancel := applyJobTimeout(ctx, rc, rc.Run.Job())
|
||||||
|
defer cancel()
|
||||||
|
_, hasDeadline := newCtx.Deadline()
|
||||||
|
assert.Equal(t, tc.wantTimeout, hasDeadline)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEvaluateJobContinueOnError(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
yaml string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"absent", "runs-on: ubuntu-latest", false},
|
||||||
|
{"true", "continue-on-error: true\nruns-on: ubuntu-latest", true},
|
||||||
|
{"false", "continue-on-error: false\nruns-on: ubuntu-latest", false},
|
||||||
|
{"expression true", "continue-on-error: ${{ 'x' == 'x' }}\nruns-on: ubuntu-latest", true},
|
||||||
|
{"expression false", "continue-on-error: ${{ 'x' != 'x' }}\nruns-on: ubuntu-latest", false},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
rc := makeTestRC(t, tc.yaml)
|
||||||
|
got := evaluateJobContinueOnError(context.Background(), rc, rc.Run.Job())
|
||||||
|
assert.Equal(t, tc.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobSetContinueOnError(t *testing.T) {
|
||||||
|
t.Run("first call true", func(t *testing.T) {
|
||||||
|
j := &model.Job{}
|
||||||
|
j.SetContinueOnError(true)
|
||||||
|
assert.True(t, j.ContinueOnError)
|
||||||
|
})
|
||||||
|
t.Run("first call false", func(t *testing.T) {
|
||||||
|
j := &model.Job{}
|
||||||
|
j.SetContinueOnError(false)
|
||||||
|
assert.False(t, j.ContinueOnError)
|
||||||
|
})
|
||||||
|
t.Run("true then false locks to false", func(t *testing.T) {
|
||||||
|
j := &model.Job{}
|
||||||
|
j.SetContinueOnError(true)
|
||||||
|
j.SetContinueOnError(false)
|
||||||
|
assert.False(t, j.ContinueOnError)
|
||||||
|
})
|
||||||
|
t.Run("false then true stays false", func(t *testing.T) {
|
||||||
|
j := &model.Job{}
|
||||||
|
j.SetContinueOnError(false)
|
||||||
|
j.SetContinueOnError(true)
|
||||||
|
assert.False(t, j.ContinueOnError)
|
||||||
|
})
|
||||||
|
t.Run("true then true stays true", func(t *testing.T) {
|
||||||
|
j := &model.Job{}
|
||||||
|
j.SetContinueOnError(true)
|
||||||
|
j.SetContinueOnError(true)
|
||||||
|
assert.True(t, j.ContinueOnError)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user