kciter / aws-ecr-action

This Action allows you to create Docker images and push into a ECR repository.
MIT License
146 stars 116 forks source link

Simple caching with ECR #9

Closed Torvaney closed 3 years ago

Torvaney commented 4 years ago

Hello there,

Thank you for making this action available to the community. I have found it to be both pleasant to use and very helpful to me.

I have one question, though: Have you considered making caching from ECR available as a top-level argument to this action? I believe I can acheive the same thing by supplying --cache-from to the extra_build_args argument; however, this results in duplicating some of the environment variables.

Thanks

kciter commented 3 years ago

I will. But very busy now. 😢

Torvaney commented 3 years ago

@jondavidjohn - I got a notification from you commenting on this thread, although it doesn't seem to be here anymore(?)

In answer to your question, I haven't yet succeeded with using extra_build_args - have you found a solution?

jondavidjohn commented 3 years ago

I have, and happy to share. I tried just using the extra_build_args on it's own, and it never seemed to work.

I found out that --cache-from expects this image to already be pulled and available locally for it to work. This is why when I tested it locally it worked because I already had the image built locally!

Adding another step just before this action to pull the image made it work as expected. You do have to authenticate with ecr/docker first and then pull the image you want to reference with your cache.

Also, make sure you're tagging a latest in your build step so you can reference that for the cache.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Pull Cache Image
      run: |
        aws ecr get-login-password | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.    
 .dkr.ecr.us-east-2.amazonaws.com
        docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.us-east-2.amazonaws.com/rails:latest
      env:
        AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
        AWS_ACCESS_KEY_ID: ${{ secrets.DEPLOY_USER_AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_USER_SECRET_ACCESS_KEY }}
        AWS_DEFAULT_REGION: 'us-east-2'
    - uses: docker://ghcr.io/kciter/aws-ecr-action:034328f198022c55596bf0dfef5befbdba5043b6
      with:
        access_key_id: ${{ secrets.DEPLOY_USER_AWS_ACCESS_KEY_ID }}
        secret_access_key: ${{ secrets.DEPLOY_USER_SECRET_ACCESS_KEY }}
        account_id: ${{ secrets.AWS_ACCOUNT_ID }}
        repo: rails
        region: us-east-2
        tags: latest,${{ github.sha }}
        extra_build_args: --cache-from ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.us-east-2.amazonaws.com/rails:latest
jondavidjohn commented 3 years ago

All this to say, it's a workaround, even if a bit ugly. Would love to see this cache functionality integrated in the action. I assume PRs would be welcome for this @kciter?

Torvaney commented 3 years ago

Ah, that is wonderful. Thank you so much for sharing, @jondavidjohn !

jondavidjohn commented 3 years ago

@Torvaney ended up throwing together a PR to better support this ^

kciter commented 3 years ago

Sorry, I'm too late merge.