Search code examples
dockergithub-actionscaprover

Refer to Docker image variable name in Github Action


I'm very new with github actions, caprover and docker images so I might be asking very stupid questions, sorry if it is the case. I searched for a good amount of time and could not understand it by myself...

So, I'm trying to deploy to caprover a docker image built just before in my github action. Please see below my .yml file:

name: Docker Image CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag my-image-name:latest
    
    - name: Print image names
      run: docker images -q my-image-name
      
    - name: Deploy image
      uses: floms/action-caprover@v1
      with:
        host: '${{ secrets.CAPROVER_SERVER }}'
        password: '${{ secrets.CAPROVER_PASSWORD }}'
        app: '${{ secrets.APP_NAME }}'
        image: my-image-name:latest

The Build the Docker image step was successful, but the Deploy image one was not. The error message I got was:

Build started for ***
An explicit image name was provided (my-image-name:latest). Therefore, no build process is needed.
Pulling this image: my-image-name:latest This process might take a few minutes.
Build has failed!
----------------------
Deploy failed!
Error: (HTTP code 404) unexpected - pull access denied for my-image-name, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

Based on the error message, I believe I do not give the correct image name to the floms/action-caprover@v1 action. It is why I created step 2 Print image names to try and understand better what is the real name of the image created. I tried several solutions for the field name image, but all resulted in an error...

Thanks for the all the help you could provide!


Solution

  • Thanks Yoel Nunez for pointing I should deploy to the registry before trying to publish to caprover.

    I followed the doc on github and finally managed to publish to caprover using a github action. Below is the .yml file that worked perfectly.

    name: Publish to caprover
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
        
    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: ${{ github.repository }}
    
    jobs:
    
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - name: Checkout repository
          uses: actions/checkout@v3
        
        - name: Log in to the Container registry
          uses: docker/login-action@v2
          with:
            registry: ghcr.io
            username: ${{ github.actor }}
            password: ${{ secrets.PAT }}
            
        - name: Extract metadata (tags, labels) for Docker
          id: meta
          uses: docker/metadata-action@v4
          with:
            images:  ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
    
        - name: Build and push
          uses: docker/build-push-action@v3
          with:
            context: .
            push: true
            tags: ${{ steps.meta.outputs.tags }}
            labels: ${{ steps.meta.outputs.labels }}
        
        - name: Deploy image
          uses: floms/action-caprover@v1
          with:
            host: '${{ secrets.CAPROVER_SERVER }}'
            password: '${{ secrets.CAPROVER_PASSWORD }}'
            app: '${{ secrets.APP_NAME }}'
            image: ${{ steps.meta.outputs.tags }}