gpt4 book ai didi

docker run dynamodb-local on Github Actions Workflow 挂起

转载 作者:行者123 更新时间:2023-12-04 14:07:11 24 4
gpt4 key购买 nike

我目前正在开发一个小型 CICD 项目,每当我更新代码时,该项目将使用 dynamodb-local 在 Github Actions 上运行一系列测试,如果测试成功,则打包和部署。
我有以下工作流程:

name: backend_actions
on:
workflow_dispatch:
push:
paths:
- 'backend/*'
branches:
- master
jobs:
test-locally:
runs-on: ubuntu-latest
outputs:
test-result: ${{ steps.run-tests.outputs.result }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.9'
- uses: aws-actions/setup-sam@v1
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Setup local DynamoDB
run: docker run -p 8000:8000 amazon/dynamodb-local
- name: Create table
run: aws dynamodb create-table --cli-input-json file://backend/src/test/make_table.json --endpoint-url http://localhost:8000
- name: start local API Gateway
run: sam local start-api --env-vars backend/env.json
- id: run-tests
name: Run tests
run: |
python backend/src/test_dynamoDB_lambda.py
echo "::set-output name=result::$?"
update_backend:
needs: test-locally
if: ${{ needs.test-locally.outputs.test-result == '0' }}
runs-on: ubuntu-latest
steps:
- name: Package and deploy
run: |
aws cloudformation package --s3-bucket cloud-resume-bucket \
--template-file backend/template.yaml --output-template-file backend/gen/template-gen.yaml
aws cloudformation deploy --template-file backend/gen/template-gen.yaml --stack-name cloud-formation-resume \
--capabilities CAPABILITY_IAM
当我尝试在 Github Actions 中运行工作流时,它将进入“设置本地 DynamoDB”步骤,输出下面的文本,然后挂起。
Run docker run -p 8000:8000 amazon/dynamodb-local
Unable to find image 'amazon/dynamodb-local:latest' locally
latest: Pulling from amazon/dynamodb-local
2cbe74538cb5: Pulling fs layer
137077f50205: Pulling fs layer
58932e640a40: Pulling fs layer
58932e640a40: Verifying Checksum
58932e640a40: Download complete
2cbe74538cb5: Verifying Checksum
2cbe74538cb5: Download complete
137077f50205: Verifying Checksum
137077f50205: Download complete
2cbe74538cb5: Pull complete
137077f50205: Pull complete
58932e640a40: Pull complete
Digest: sha256:bdd26570dc0e0ae49e1ea9d49ff662a6a1afe9121dd25793dc40d02802e7e806
Status: Downloaded newer image for amazon/dynamodb-local:latest
Initializing DynamoDB Local with the following configuration:
Port: 8000
InMemory: true
DbPath: null
SharedDb: false
shouldDelayTransientStatuses: false
CorsParams: *
似乎它可以找到 docker 镜像并正常下载,但在初始化时停止?这是我第一次使用 Github Actions 和 Docker,所以我不确定为什么它卡在 Github Actions 上而不是在我自己的计算机上运行它时,所以任何帮助将不胜感激!

最佳答案

当您运行命令时 docker run -p 8000:8000 amazon/dynamodb-local进程永远不会退出,所以 Github run block 实际上并不知道什么时候进入下一步——它只是永远卡在那里。
我在我的项目中所做的只是通过使用 & 将它作为背景。命令后:

      - name: Setup local DynamoDB
run: docker run -p 8000:8000 amazon/dynamodb-local &
Github Workflows 将启动 Docker 容器并移至下一个 run步骤,当所有步骤都完成后,它只会作为正常清理的一部分杀死容器。因此,您无需担心最后将其关闭。
这种方法的困难在于 DynamoDB-local 需要几秒钟才能启动,但您的下一步依赖于它,并且可能会抛出 ECONNREFUSED 错误。
我在我的项目中所做的是拥有下一个 run step 执行一个尝试列出表的脚本,并在短时间内重试,直到得到响应。
bash 命令很简单(你需要把它放在一个 while+try/catch 循环中):
aws dynamodb list-tables --endpoint-url http://localhost:8000
作为指南,这(大致)是我在 JavaScript 中所做的,使用 aws-sdk和 NodeJS@16:
// wait-for-dynamodb.js
import timers from 'timers/promises'
import AWS from 'aws-sdk'

const dynamodb = new AWS.DynamoDB()

const waitForDynamoDbToStart = async () => {
try {
await dynamodb.listTables().promise()
} catch (error) {
console.log('Waiting for Docker container to start...')
await timers.setTimeout(500)
return waitForDynamoDbToStart()
}
}

const start = Date.now()
waitForDynamoDbToStart()
.then(() => {
console.log(`DynamoDB-local started after ${Date.now() - start}ms.`)
process.exit(0)
})
.catch(error => {
console.log('Error starting DynamoDB-local!', error)
process.exit(1)
})
然后我只是在 run 中找到了它脚步:
      - name: Setup local DynamoDB
run: docker run -p 8000:8000 amazon/dynamodb-local &
- name: Wait for it to boot up
run: node ./wait-for-dynamodb.js
# now you're guaranteed to have DynamoDB-local running

关于docker run dynamodb-local on Github Actions Workflow 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67592818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com