COPY failed in git actions - docker

Dockerfile :
FROM openjdk:11-jdk-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} backend.jar
ENTRYPOINT ["java","-jar","/backend.jar"]
deploy.yml:
jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout#v2
- name: Push to Docker Hub
uses: docker/build-push-action#v1
with:
path: backend
dockerfile: backend/Dockerfile
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
repository: myname/myrepo
tag_with_ref: true
Github actions result:
Status: Downloaded newer image for openjdk:11-jdk-slim
---> b4517d9514cb
Step 2/4 : ARG JAR_FILE=target/*.jar
---> Running in 640e9a32e282
Removing intermediate container 640e9a32e282
---> e9414330bf73
Step 3/4 : COPY ${JAR_FILE} backend.jar
COPY failed: no source files were specified
exit status 1
Error: exit status 1
I don't know why it says that source files were not specified, I have a other workflow, where I do exactly the same, but without build-push-action module, and it works fine.

Forgot to put a compile step before build:
- name: Maven Package
run: mvn -B clean compile package -DskipTests

Related

github action buildah fails to pull docker image

running buildah github action to build the container below
FROM docker.io/library/dart:stable AS build
COPY . /app
WORKDIR /app
RUN dart compile exe ./bin/server.dart -o ./server
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/ /bin
EXPOSE 8080
CMD ["server"]
I get this error
[1/2] STEP 1/5: FROM docker.io/library/dart:stable AS build
Trying to pull docker.io/library/dart:stable...
Getting image source signatures
[...]
Writing manifest to image destination
Storing signatures
[1/2] STEP 2/5: COPY . /app
[1/2] STEP 3/5: WORKDIR /app
[1/2] STEP 4/5: RUN mkdir build
[1/2] STEP 5/5: RUN dart compile exe ./bin/server.dart -o ./server
"./bin/server.dart" file not found.
error building at STEP "RUN dart compile exe ./bin/server.dart -o ./server": error while running runtime: exit status 255
[2/2] STEP 1/5: FROM scratch
time="2022-12-02T09:04:48Z" level=error msg="exit status 255"
Error: Error: buildah exited with code 255
Trying to pull docker.io/library/dart:stable...
Getting image source signatures
[...]
Writing manifest to image destination
Storing signatures
error building at STEP "RUN dart compile exe ./bin/server.dart -o ./...
but running it locally with podman I have no issues
here is my github workflow
name: 'publish containers'
on:
push:
branches:
- master
# https://github.com/redhat-actions/push-to-registry/blob/main/.github/workflows/ghcr-push.yaml
env:
REGISTRY_USER: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ github.token }}
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
IMAGE_TAGS: v1 ${{ github.sha }}
jobs:
push-ghcr:
name: build and push image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
# https://github.com/redhat-actions/podman-login
# https://github.com/redhat-actions/buildah-build#using-private-images
- name: login to ghcr
uses: redhat-actions/podman-login#v1
with:
username: ${{ env.REGISTRY_USER }}
password: ${{ env.REGISTRY_PASSWORD }}
registry: ${{ env.IMAGE_REGISTRY }}
- name: build image
id: build_image
uses: redhat-actions/buildah-build#v2
with:
image: server
tags: latest ${{ env.IMAGE_TAGS }}
containerfiles: |
server/Containerfile
- name: push to ghcr
uses: redhat-actions/push-to-registry#v2
id: push
with:
image: ${{ steps.build_image.outputs.image }}
tags: ${{ steps.build_image.outputs.tags }}
registry: ${{ env.IMAGE_REGISTRY }}
username: ${{ env.REGISTRY_USER }}
password: ${{ env.REGISTRY_PASSWORD }}
it seems to me that fails to pull the image from docker, but again it pulls ur locally
any idea what did I do wrong?

GitHub Action Is Not Publishing to ghcr.io What's Wrong with Dockerfile

I have a GitHub Action that needs to publish a Dockerfile to a specific organization.
The action looks like this:
name: Docker dataeng_github_metrics
# Run workflow on tags starting with v (eg. v2, v1.2.0)
on:
push:
branches: [ "master" ]
paths:
- ./data_pipelines/dataeng_github_metrics/*
pull_request:
branches: [ "master" ]
jobs:
Deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v1
- name: Login to GitHub Container Registry
uses: docker/login-action#v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_REGISTRY_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action#v2
with:
file: ./data_pipelines/dataeng_github_metrics/Dockerfile
push: true # Will only build if this is not here
tags: |
ghcr.io/mirantis/dataeng_github_metrics:latest
The problem is that when I run the Dockerfile locally it works, but on this specific action workflow, it does not work. Instead, I get the following:
ERROR: failed to solve: failed to compute cache key: "/go.sum" not found: not found
Error: buildx failed with: ERROR: failed to solve: failed to compute cache key: "/go.sum" not found: not found
And upon inspecting the Dockerfile:
###############
# CACHE IMAGE #
###############
ARG GO_IMAGE=golang:1.17.3-alpine3.14
ARG BASE_IMAGE=alpine:3.14.2
FROM ${GO_IMAGE} AS cache
# Add the keys
ARG GITHUB_ID
ENV GITHUB_ID=$GITHUB_ID
ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=$GITHUB_TOKEN
# Install Git
RUN apk add git
# TODO: ENCRYPT THE GITHUB_ID AND GITHUB_TOKEN
# Make Git Configuration
RUN git config \
--global \
url."https://${GITHUB_ID}:${GITHUB_TOKEN}#github.com/".insteadOf \
"https://github.com/"
WORKDIR /bin
COPY go.mod go.sum /bin/
RUN go mod download
##############
# BASE IMAGE #
##############
FROM cache AS dataeng_github_metrics
COPY . /bin
WORKDIR /bin
# Setup Git Terminal Prompt & Go Build
RUN go build .
###############
# FINAL IMAGE #
###############
FROM ${BASE_IMAGE}
COPY --from=dataeng_github_metrics /bin/dataeng_github_metrics bin/
ENTRYPOINT [ "bin/dataeng_github_metrics" ]
It fails at the following:
COPY go.mod go.sum /bin/
This builds locally so I don't understand what the issue is.
So to get past it, I had to add the full path to my context for the GitHub Action, I have another issue, but it's currently unrelated to the path context and not being able to find the files:
By adding in the following line, I was able to fix it by specifying the context path:
context: ./data_pipelines/dataeng_github_metrics/
name: Docker dataeng_github_metrics
# Run workflow on tags starting with v (eg. v2, v1.2.0)
on:
push:
branches: [ "master" ]
paths:
- ./data_pipelines/dataeng_github_metrics/*
pull_request:
branches: [ "master" ]
jobs:
Deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v1
- name: Login to GitHub Container Registry
uses: docker/login-action#v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_REGISTRY_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v2
- name: Build and Push Docker Image
uses: docker/build-push-action#v3
with:
context: ./data_pipelines/dataeng_github_metrics/
file: ./data_pipelines/dataeng_github_metrics/Dockerfile
push: true # Will only build if this is not here
tags: |
ghcr.io/mirantis/dataeng_github_metrics:latest

GitHub Actions fails to build a docker image for web app

Have a few queries around it
I have a docker build failing due to path. It works when I execute on local PC. What should be the path here?
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout#v3
- name: Node
uses: actions/setup-node#v3
with:
node-version: 14.15
- name: Dependencies
run: npm install --legacy-peer-deps
- name: Build
run: npm run build-prod
- name: Docker Login
run: docker login -u $USERNAME -p $PWD
- name: Build
run: docker build . --tag $REPO:latest
- name: Docker Push
run: docker push $REPO:latest
Error
> [3/3] COPY /dist/app-name /usr/share/nginx/html:
145
------
146
error: failed to solve: failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount67345738657/dist: lstat /var/lib/docker/tmp/buildkit-mount67345738657/dist: no such file or directory
Dockerfile
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY /dist/app-name /usr/share/nginx/html
How do I get the tag version when a new release is created to start the build with two tags: latest and {tag} created for the build. This is to keep build backups of old tags in docker. What will be the changes above to do two builds?

Github Actions Docker build: "/pmpn-lock.yaml" and "./nginx/default.conf" not found

So I'm having an issue with my docker build action with github actions and I definitely feel like the issue is obvious but I am failing to get it.
So here is my Github Action
name: Build / Publish
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build-and-push-docker-image:
name: Build Pinsel Docker image and push to repos
runs-on: ubuntu-latest
steps:
-
name: Checkout codebase
uses: actions/checkout#v2
-
name: Docker meta
id: meta
uses: docker/metadata-action#v3
with:
images: name/app
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
# Setup docker build
-
name: Set up Docker buildx
id: buildx
uses: docker/setup-buildx-action#v1
-
name: Login to Docker Hub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login into Github Packages
uses: docker/login-action#v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PAT }}
-
name: Build image and push it to both registries
uses: docker/build-push-action#v2
with:
context: ../../
file: ./Dockerfile.prod
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
And here is my Dockerfile located in main directory
FROM node:16.4.2-alpine as build
RUN npm install -g pnpm#next-7
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY /pnpm-lock.yaml ./
RUN pnpm fetch --prod
ADD . ./
RUN pnpm install -r --offline --prod
FROM nginx:1.21-alpine
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
and lastly the error
#11 [build 5/8] COPY /package.json ./
#11 ERROR: failed to calculate checksum of ref ypf6stpm3t9h2xl6ezvwo7876::kvjk90tcw8wfykmdkvu1uvzgs: "/package.json": not found
#12 [stage-1 2/3] COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
#12 CANCELED
Dockerfile.prod:13
--------------------
11 |
12 | FROM nginx:1.21-alpine
13 | >>> COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
14 | COPY --from=build /app/build /usr/share/nginx/html
15 | EXPOSE 80
--------------------
error: failed to solve: failed to compute cache key: failed to calculate checksum of ref ypf6stpm3t9h2xl6ezvwo7876::kvjk90tcw8wfykmdkvu1uvzgs: "/nginx/default.conf": not found
Error: buildx failed with: error: failed to solve: failed to compute cache key: failed to calculate checksum of ref ypf6stpm3t9h2xl6ezvwo7876::kvjk90tcw8wfykmdkvu1uvzgs: "/nginx/default.conf": not found
Everything goes well on github actions until it needs to copy files. It says it can't find pnpm-lock.yaml, nor can it find ./nginx both of which are there.
I feel like the answer is super simple and I am just mentally fried and it's not standing out. If I can get a nudge in the right direction, it'd be appreciated!
I have realized that github/checkout puts you at the root directory of the github project, meaning I didn't need to add ../../ for context, I made the mistake of thinking I was navigating from the github action workflow directory.
So for anyone else who ends up this issue: actions/checkout will put you in the root directory of the repo, so you only need to use navigate via context if your dockerfiles are somewhere other than the root folder (like in their own docker folder)

Docker compose fails with 'COPY failed: stat usr/src/app/dist: file does not exist' - only on github actions

I'm creating web application (angular + nest.js) and I'm trying to make multi stage build - I want to reduce size of docker image.
Angular build works good.
Nest.js one builds successfully on my pc using docker compose build, but fails on github actions with following error: Status: COPY failed: stat usr/src/app/dist: file does not exist, Code: 1
What can i do to fix it?
Dockerfile:
FROM node:16.13.1-alpine AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
RUN npm run build
COPY . .
FROM node:16.13.1-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=build /usr/src/app/dist ./dist
EXPOSE 7000
CMD ["node", "dist/main"]
previous single stage Dockerfile (builds successfully):
FROM node:16.13.1-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm run build
COPY . .
EXPOSE 7000
CMD [ "npm", "start" ]
docker-compose.yml:
version: '3.8'
services:
backend:
container_name: nestjs
image: ghcr.io/<gh-nickname/repo-name>/backend
build:
context: backend
dockerfile: Dockerfile
ports:
- 7000:7000
frontend:
container_name: angular
image: ghcr.io/<gh-nickname/repo-name>/frontend
build:
context: frontend
dockerfile: Dockerfile
ports:
- 8000:80
github workflow file:
name: Build and publish to github packages
on:
push:
branches:
- master
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Log in to the Container registry
uses: docker/login-action#v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action#v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build image
run: docker compose build
- name: Publish image
run: docker compose push
Try with the following Dockerfile:
FROM node:16.13.1-alpine AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
RUN npm run build
COPY . .
FROM node:16.13.1-alpine
COPY --from=build /usr/src/app/dist ./dist
EXPOSE 7000
CMD ["node", "dist/main"]
Caching multistage builds on CI Servers is not that easy as locally. You have to tell it where to get the layers from. As schema:
docker build --target build-image --cache-from=you/repo/backend:build-stage --tag you/repo/backend:build-stage
docker build --target prod-image --cache-from=you/repo/backend:build-stage --cache-from=you/repo/backend:prod-stage --tag you/repo/backend:prod-stage
docker push
So for GH Actions you can use the internal caching of GH and do something like:
steps:
- name: Build base image
id: docker_build_base
uses: docker/build-push-action#v2
with:
context: .
file: Dockerfile
push: false
cache-from: |
type=gha,scope=base
cache-to: |
type=gha,scope=base,mode=max
target: base
tags: you/repo/backend:build-stage
- name: Build prod image
id: docker_build_prod
uses: docker/build-push-action#v2
with:
context: .
file: Dockerfile
push: true
cache-from: |
type=gha,scope=prod
type=gha,scope=base
cache-to: |
type=gha,scope=prod,mode=max
target: prod
tags: you/repo/backend:prod-stage
Remark: It's showing you the right direction and no copy&paste solution. Please adjust as you need..
I managed to solved it by moving COPY . . line before RUN npm run build

Resources