简体   繁体   English

docker 在 Github 上运行 dynamodb-local 操作工作流挂起

[英]docker run dynamodb-local on Github Actions Workflow hanging

I'm currently working on a small CICD project that will run a series of tests on Github Actions using dynamodb-local whenever I update my code and then package and deploy if the tests are successful.我目前正在开发一个小型 CICD 项目,该项目将在我更新代码时使用 dynamodb-local 对 Github 操作运行一系列测试,然后在测试成功时使用 package 并部署。

I have the following workflow:我有以下工作流程:

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

When I try running the workflow in Github Actions, it will get to the 'Setup local DynamoDB' step, output the text below, and then hang.当我尝试在 Github 操作中运行工作流时,它将进入“设置本地 DynamoDB”步骤,output 下面的文本,然后挂起。

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: *

Seems like it can find the docker image and download it fine, but stops upon initializing?似乎它可以找到 docker 图像并正常下载,但在初始化时停止? This is my first time working with Github Actions and Docker, so I'm not really sure why it's hanging on Github Actions and not when I run it on my own computer, so any help would be appreciated!这是我第一次使用 Github Actions 和 Docker,所以我不太确定为什么它会挂在 Github Actions 上,所以当我在自己的计算机上运行它时,我将不胜感激!

When you run the command docker run -p 8000:8000 amazon/dynamodb-local the process never exits, so the Github run block doesn't actually know when to move on to the next step—it just hangs there forever.当您运行命令docker run -p 8000:8000 amazon/dynamodb-local时,该进程永远不会退出,因此 Github run块实际上并不知道何时继续执行下一步——它只是永远挂在那里。

What I did in my project is simply background it, by using the & after the command:我在我的项目中所做的只是将它作为背景,通过在命令后使用&

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

Github Workflows will start the Docker container and move to the next run step, and when all the steps are done it'll just kill the container as part of normal cleanup. Github 工作流将启动 Docker 容器并移至下一个run步骤,当所有步骤完成后,它将作为正常清理的一部分杀死容器。 Because of this, you don't need to worry about shutting it down at the end.因此,您无需担心最终将其关闭。

The difficulty with this approach is that it takes several seconds for DynamoDB-local to start up, but your next step relies on it and will likely throw ECONNREFUSED errors.这种方法的困难在于 DynamoDB-local 需要几秒钟才能启动,但您的下一步依赖于它,并且可能会引发 ECONNREFUSED 错误。

What I've done in my project is to have the next run step execute a script that attempts to list tables, retrying with a short delay until it gets back a response.我在我的项目中所做的是让下一个run步骤执行一个尝试列出表的脚本,并在短时间内重试,直到它得到响应。

The bash command is simply (you would need to put this in a while+try/catch loop): bash 命令很简单(您需要将其放入 while+try/catch 循环中):

aws dynamodb list-tables --endpoint-url http://localhost:8000

As a guide, this is (roughly) what I do in JavaScript, using the aws-sdk and NodeJS@16:作为指导,这(大致)是我在 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)
    })

Then I simply have that in the run steps:然后我只是在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 -inMemory 标志的情况下在 Docker 中运行 dynamodb-local - How to run dynamodb-local in Docker without -inMemory flag 如何使用导出的表和行创建DynamoDB-Local Docker实例? - How to create a DynamoDB-Local Docker instance with exported table and rows? 在私有 docker 容器中运行整个 GitHub 操作工作流作业 - Run entire GitHub Actions workflow job in private docker container 无法将 AWS SAM 本地 API 连接到在 docker 实例中运行的 dynamodb-local - Unable to connect AWS SAM local API to dynamodb-local running in docker instance 更改 amazon/dynamodb-local 端口 - Change amazon/dynamodb-local Port 如何在dynamodb-本地docker实例中使用保存的DynamoDBLocal.jar - How to use a saved DynamoDBLocal.jar in dynamodb-local docker instance Github 操作工作流到 docker 容器环境 - Github actions workflow to docker container environment 使用 Dockerfile 在 Docker 图像上运行 GitHub 工作流? - Run GitHub workflow on Docker image with a Dockerfile? 如何在 GitHub Actions 工作流程中实现 Docker 图像的语义版本控制? - How to implement semantic versioning of Docker images in GitHub Actions workflow? github 动作:在同一个 docker 中运行多个作业 - github actions: run multiple jobs in the same docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM