docker在Github Actions工作流挂起的地方运行dynerdb



我目前正在做一个小型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 Action上,而不是当我在自己的计算机上运行它时,所以如果有任何帮助,我将不胜感激!

当您运行命令docker run -p 8000:8000 amazon/dynamodb-local时,进程永远不会退出,因此Githubrun块实际上不知道何时进入下一步——它只是永远挂在那里。

我在我的项目中所做的只是通过在命令之后使用&将其作为背景

- name: Setup local DynamoDB
run: docker run -d -p 8000:8000 amazon/dynamodb-local

Github工作流将启动Docker容器并进入下一个run步骤,当所有步骤完成后,它将作为正常清理的一部分杀死容器。正因为如此,你不需要担心最后会关闭它。

这种方法的困难在于,DynamoDB本地启动需要几秒钟的时间,但您的下一步依赖于它,可能会出现ECONNREFUSED错误。

我在项目中所做的是让下一个run步骤执行一个脚本,该脚本尝试列出表,并在短时间内重试,直到它得到响应。

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

最新更新