Refer to Docker image variable name in Github Action - docker

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!

To make sure your CapRover instance is able to pull the image from your github docker registry, it needs to be registered on your CapRover instance.
TLDR: I don't see a publish step (for your docker image) in your GitHub Actions configuration. If you want to use the image name to push it to CapRover you will need to publish it to a registry, whether it is GitHub's Container Registry, Nexus registry, or any other registry.
To do that in your CapRover instance, you need to go into Cluster > Docker Registry Configuration > Add Remote Registry. Then you will proceed to enter the configuration for your GitHub Container Registry. Typically you will need a Personal Access Token to allow CapRover to communicate with GitHub instead of using your password.
Docker Registry Configuration
Remote Registry Configuration
Docker Registry Configuration - Remote Registry Configured

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 }}

Related

docker build image with tag github sha

Hi I am working in github actions and kubernetes. I am building multiple docker images using matrix as below.
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action#98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ matrix.docker-image-name }}
flavor: latest=true
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
- name: Build and push Docker image ${{ matrix.name }}
uses: docker/build-push-action#ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: ${{matrix.context }}
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
This piece of code always build and pushes the image with latest tag. This code is working but i do not want to tag image with latest rather i would like to tag with github sha. So tired something like this
tags: ${{ github.sha }}
Whenever I add above code my code started failing and throwing the error
buildx call failed with: ERROR: denied: requested access to the
resource is denied
I want to push image with sha can someone pls help me to fix this. Any help would be appreciated. Thanks

Tag new Docker image push automatically for CI/CD deployment in Github Action

I want to be able to run a Github action that would build, automatically & uniquely tag and push an image to Docker hub.
I want it to also be that another Github action that deploys to the server will automatically know the image's tag and adds it to the image name to pull that specific image automatically.
Try the Publish Docker GitHub Action.
Example workflow that will trigger on new GH Release creation and will use the GH release version for tagging the Docker image:
name: Publish to Registry
on:
release:
types: [published]
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- id: pre-step
shell: bash
run: echo "release-version=$(echo ${GITHUB_REF:10})" >> $GITHUB_OUTPUT
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action#v5
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tags: "latest,${{ steps.pre-step.outputs.release-version }}"
Also, you can use tag_names when you want to push tags/release by their git name (e.g. refs/tags/MY_TAG_NAME):
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tag_names: true
For more details visit the Usage section.

GitHub actions and Docker-compose

guys!
I need you help to run docker-compose build on github action. I have a docker-compose file and I can't understand how to build and deploy it in correct way besides of just copying docker-compose by ssh and run scripts there.
There's docker/build-push-action#v2 but it's not working with docker-compose.yml.
This strongly depends where do you want to push your images. But for instance if you use Azure ACR you can use this action
on: [push]
name: AzureCLISample
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Azure Login
uses: azure/login#v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Azure CLI script
uses: azure/CLI#v1
with:
azcliversion: 2.0.72
inlineScript: |
az acr login --name <acrName>
docker-compose up
docker-compose push
And then just build and push your images. But this is an example. If you use ECR it would be similar I guess.
For DigitialOcean it would be like this:
steps:
- uses: actions/checkout#v2
- name: Build image
run: docker-compose up
- name: Install doctl # install the doctl on the runner
uses: digitalocean/action-doctl#v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: push image to digitalocean
run: |
doctl registry login
docker-compose push
You can find more details about this here

Github Actions: Push docker image build by gradle task

For building images of my current project, I use the gradle task bootBuildImage. This task creates a OCI image using Cloud Native Buildpacks.
- name: Build image with Gradle
run: ./gradlew bootBuildImage
With the next step I'm trying to push this docker image to my private GitHub registry using build-push-action.
- name: Push image to Registry
uses: docker/build-push-action#v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
registry: docker.pkg.github.com
repository: sullrich84/wettkampfdb-backend
tags: latest
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
As I can tell from the logs, the problem with this step is that it seems to rely on a Dockerfile located in the workspaces root directory which does not exist.
unable to prepare context: unable to evaluate symlinks in Dockerfile path:
lstat /github/workspace/Dockerfile: no such file or directory
Is it possible to push the image created via bootBuildImage to my private GitHub registry without using/creating a dedicated Dockerfile?
If you are just looking for something to deal with docker push, you can just use the native docker command to do it.
Something like this.
- name: run docker push
run: |
#docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
#docker push $BUILD_TAG
#docker push $LATEST_TAG
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
The github-action you are using is not for pushing an image you define by repository and tag but rahter build and push https://github.com/docker/build-push-action#build-push-action
Builds and pushes Docker images and will log in to a Docker registry if required.
Specifically this is also related to https://github.com/docker/build-push-action/issues/17 - so just building without pushing is possible, not vice versa.
This github action does yet not allow just pushing.
This is for now very common for a lot of CI/CD solutions, where build and push are one task.
I use publishRegistry option of gradle bootBuildImage.
Set parameter in your build.gradle (below is gradle.kts)
tasks.bootBuildImage {
imageName = "${imageName}:${project.version}"
isPublish = true
docker {
publishRegistry {
url = dockerUrl
username = dockerUsername
password = dockerPassword
}
}
}
check this document

Unable to push to docker hub from github action despite of proper image tag

Here is my workflow file:
name: Integration
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: run docker-build with tag
run: ./docker-build sudipbhandari/springboot-docker-app:$GITHUB_SHA
- name: list docker images
run: docker image ls
- name: Docker Login
uses: Azure/docker-login#v1
with:
login-server: 'docker.io'
username: '{{secrets.DOCKER_HUB_USERNAME}}'
password: '{{secrets.DOCKER_HUB_PASSWORD}}'
- name : docker push
run: docker push sudipbhandari/springboot-docker-app:$GITHUB_SHA
- name: clean up
run: rm ~/.docker/config.json
Image:
***/springboot-docker-app 8d0caac294f0e414b88cfe6cc433995ef7ca2f25
Docker push output:
Run docker push ***/springboot-docker-app:$GITHUB_SHA
[6](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:6)The push refers to repository [docker.io/***/springboot-docker-app]
[7](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:7)945b12e86d4e: Preparing
[8](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:8)fff0debc90b1: Preparing
[9](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:9)ceaf9e1ebef5: Preparing
[10](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:10)9b9b7f3d56a0: Preparing
[11](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:11)f1b5933fe4b5: Preparing
[12](https://github.com/sudipbhandari126/springboot-docker-app/runs/477583287#step:6:12)denied: requested access to the resource is denied
Image is properly tagged (username/imagename:tag)
Docker login is successful to registry (docker.io)
Still I am gettting permission denied.
I locally built and tried to push the image and it works just fine.
docker push sudipbhandari/springboot-docker-app:a
The push refers to repository [docker.io/sudipbhandari/springboot-docker-app]
f2f6c53c3c45: Layer already exists
9ff7271739b8: Layer already exists
ceaf9e1ebef5: Layer already exists
9b9b7f3d56a0: Layer already exists
f1b5933fe4b5: Layer already exists
a: digest: sha256:ecf90929be9690c052bcc457edd5bb12cbe231029b63536d94e0e86cd845b983 size: 1366
First, in GitHub go to Setting>Secrets and add two repository secrets for DOCKER_USERNAME and DOCKER_PASSWORD. Then try this:
- run:
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
env:
$DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
$DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

Resources