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
Related
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 }}
I am running my tests on aws lambda with the use of custom image. I'm using serverless for the configuration and the build of the image.
Process works fine locally. When I try to make it work with github actions, using serverless/github-action#v3 I'm getting an error:
Error: Could not find Docker installation. Ensure Docker is installed before continuing.
But since you can use 2 "uses:" I'm not sure how to work this one out. Any idea would be welcomed.
I don't know why the action cannot find docker installation but I resolved it by running the serverless command directly as follows:
steps:
- uses: actions/checkout#v1
- uses: actions/setup-node#v2
with:
node-version: "14"
- run: npm i -g serverless
- name: configure aws credentials
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: ap-northeast-1
- name: deploy
run: sls deploy --stage dev --verbose
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
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 }}
I have this code at .github/workflows/main.yaml
# .github/workflows/main.yaml
name: CI Workflow
on: [push]
jobs:
rspec-job:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
container:
image: I-stucked-here
volumes:
- /vendor/bundle
steps:
- code omitted for brevity
The main idea of this job is to run all steps in container mode. Not in Linux host mode.
Under the same repository, I have a public Docker image named ruby-rimy-2.6.3. Since it's not publicly hosted on DockerHub, I can't find a way to programmatically authenticate myself to GitHub Packages/Registry.
I did try with different syntax (see code below) but it didn't work.
# .github/workflows/main.yaml
name: CI Workflow
on: [push]
jobs:
rspec-job:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
container:
image: docker://docker.pkg.github.com/zulhfreelancer/rimy/ruby-rimy-2.6.3:latest
volumes:
- /vendor/bundle
steps:
- code omitted for brevity
From the docs, GitHub says the GITHUB_TOKEN is available when the job is running. How do I use this GITHUB_TOKEN environment variable to run something like docker login on top of that
container: section so that the job is able to pull the image?
Using GitHub Personal Token is not an option for me because that repository is just my experiment repository before applying the same thing to my GitHub organization. I don't want to put my personal token under my organization's repository environment variables/secrets — that will simply exposes my personal token to my co-workers.
You do not need to use the container instruction to run tests in a container.
The GitHub Actions host comes with docker and docker-compose installed. The way I do it, is have a docker-compose.yml in my repository, which includes a "service" that runs tests. Then, your workflow needs to do docker login and simply run the docker-compose run test command.
Note that the beauty of this approach, is that your tests are executed exactly the same on your own machine and on the CI machine. Same exact steps.
Something along these lines:
name: Test
on:
pull_request:
push: { branches: master }
jobs:
test:
name: Run test suite
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Docker login
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
- name: Build docker images
run: docker-compose build
- name: Run tests
run: docker-compose run test
I am doing the same with DockerHub, with great ease and success.
Of course, if you do not want to use docker-compose, you can still use any normal docker run ... commands after you login properly in the login step.
I am not sure that docker login command will work as is, see these for a deeper discussion:
https://github.com/actions/starter-workflows/issues/66
https://github.community/t5/GitHub-Actions/Github-Actions-Docker-login/td-p/29852/page/2