总结:
Github操作允许使用Docker容器来运行作业,但它似乎不允许为此容器映像提供动态值(使用环境变量)。
这是可行的(不是理想的解决方案):
jobs:
pytest-test:
container:
image: ghcr.io/ashrafgt/test:latest ...
这不起作用(理想的解决方案):
jobs:
pytest-test:
container:
# env variables defined at the start of the workflow
image: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
...
出现此错误:
Invalid workflow file : .github/workflows/workflow.yaml
The workflow is not valid. Unrecognized named-value: 'env'. Located at position 1 within expression: env.REGISTRY_NAME
除了做一个
run: docker run ...
?
完整示例:
在本例中,我尝试构建并推送Docker映像(使用当前提交SHA标记),然后使用相同的映像运行单元测试:
name: Main CI Pipeline
on: [push]
env:
REGISTRY_NAME: ghcr.io/${{ github.repository_owner }}
REGISTRY_USERNAME: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
IMAGE_TAG: ${{ github.sha }}
jobs:
docker-build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY_NAME }}
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ env.REGISTRY_PASSWORD }}
- uses: docker/build-push-action@v2
with:
tags: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
push: true
pytest-test:
needs: docker-build
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
container:
image: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
steps:
- uses: actions/checkout@v2
- run: pytest
请查找完整的存储库
here
.
完整错误消息为:
Invalid workflow file : .github/workflows/workflow.yaml#L38
The workflow is not valid. .github/workflows/workflow.yaml (Line: 38, Col: 14): Unrecognized named-value: 'env'. Located at position 1 within expression: env.REGISTRY_NAME
请查找运行的Github操作
here
.
修复尝试:
-
仅使用
container
而不是
container.image
:
jobs:
pytest-test:
container: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
...
-
使用
docker://
单个步骤的语法:
jobs:
pytest-test:
steps:
- uses: docker://${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
entrypoint: pytest
...
两次修复尝试都失败,错误与原始语法相同。