I am currently building a project with GitLab CI/CD and have come across the following Error message:
ModuleNotFoundError: No module named 'Submodule1.Submodule2.filename'
Submodule2 is a submodule inside Submodule1. In .gitmodules local paths were used (via ssh, see below).
In .gitlab-ci.yml I have tried using:
variables:
GIT_SUBMODULE_STRATEGY: recursive
and (with various combinations):
before_script:
- git submodule update --init --recursive
- git config --global alias.clone = 'clone --init --recursive'
- git submodule sync --recursive
Nothing seems to work. I would be thankful for each tip I can get. Here is my gitlab-ci.yml file:
image: docker:latest
services:
- docker:dind
- postgres:12.2-alpine
variables:
#GIT_SUBMODULE_STRATEGY: recursive
GIT_SUBMODULE_UPDATE_FLAGS: --jobs 4
CI_DEBUG_TRACE: "true"
POSTGRES_HOST_AUTH_METHOD: trust
stages:
- build
build-run:
stage: build
before_script:
- apk update && apk add git
- git submodule update --init --recursive
script:
- echo Build docker image
- docker build -t file -f Projectname .
- docker build -t django -f django.Dockerfile .
- echo Run docker compose
- docker-compose -f docker-compose.unittest.yml up -d
- sleep 60
- docker logs unittests
tags:
- docker
My .gitmodules looks something like:
[submodule "Submodule1"]
path = Submodule1
url =../../...git
[submodule "Submodule1/Submodule2"]
path = Submodule1/Submodule2
url=../../...git
I am trying to build and tag a docker image in Github Actions runner and am getting this error from the runner
unable to prepare context: path " " not found
Error: Process completed with exit code 1.
I have gone through all other similar issues on StackOverflow and implemented them but still, no way forward.
The interesting thing is, I have other microservices using similar workflow and Dockerfile working perfectly fine.
My workflow
name: some-tests
on:
pull_request:
branches: [ main ]
jobs:
tests:
runs-on: ubuntu-latest
env:
AWS_REGION: us-east-1
IMAGE_NAME: service
IMAGE_TAG: 1.1.0
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Create cluster
uses: helm/kind-action#v1.2.0
- name: Read secrets from AWS Secrets Manager into environment variables
uses: abhilash1in/aws-secrets-manager-action#v1.1.0
id: read-secrets
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
secrets: |
users-service/secrets
parse-json: true
- name: Build and Tag Image
id: build-image
run: |
# Build a docker container and Tag
docker build --file Dockerfile \
--build-arg APP_API=$USERS_SERVICE_SECRETS_APP_API \
-t $IMAGE_NAME:$IMAGE_TAG .
echo "::set-output name=image::$IMAGE_NAME:$IMAGE_TAG"
- name: Push Image to Kind cluster
id: kind-cluster-image-push
env:
KIND_IMAGE: ${{ steps.build-image.outputs.image }}
CLUSTER_NAME: chart-testing
CLUSTER_CONTROLLER: chart-testing-control-plane
run: |
kind load docker-image $KIND_IMAGE --name $CLUSTER_NAME
docker exec $CLUSTER_CONTROLLER crictl images
Dockerfile*
FROM node:14 AS base
WORKDIR /app
FROM base AS development
COPY .npmrc .npmrc
COPY package.json ./
RUN npm install --production
RUN cp -R node_modules /tmp/node_modules
RUN npm install
RUN rm -f .npmrc
COPY . .
FROM development AS builder
COPY .npmrc .npmrc
RUN yarn run build
RUN rm -f .npmrc
RUN ls -la
FROM node:14-alpine AS production
# Install curl
RUN apk update && apk add curl
COPY --from=builder /tmp/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./
ARG APP_API
# set environmental variables
ENV APP_API=$APP_API
EXPOSE ${PORT}
CMD [ "yarn", "start" ]
I guess the problem is coming from the building command or something, these are the different things I have tried
I used --file explicitly with period(.)*
docker build --file Dockerfile \
--build-arg APP_API=$USERS_SERVICE_SECRETS_APP_API \
-t $IMAGE_NAME:$IMAGE_TAG .
echo "::set-output name=image::$IMAGE_NAME:$IMAGE_TAG"
I used only period (.)
docker build \
--build-arg APP_API=$USERS_SERVICE_SECRETS_APP_API \
-t $IMAGE_NAME:$IMAGE_TAG .
echo "::set-output name=image::$IMAGE_NAME:$IMAGE_TAG"
I used relative path for Dockerfile (./Dockerfile)
docker build --file ./Dockerfile \
--build-arg APP_API=$USERS_SERVICE_SECRETS_APP_API \
-t $IMAGE_NAME:$IMAGE_TAG .
echo "::set-output name=image::$IMAGE_NAME:$IMAGE_TAG"
I used relative path for the period (./)
docker build \
--build-arg APP_API=$USERS_SERVICE_SECRETS_APP_API \
-t $IMAGE_NAME:$IMAGE_TAG ./
echo "::set-output name=image::$IMAGE_NAME:$IMAGE_TAG"
I have literally exhausted everything I've read from SO
The problem was basically a white-spacing issue. Nothing could show this. Thanks to This Github answer
I have a azure pipeline that I want to use to deploy my rails app.
The app has a Dockerfile and a docker-compose file.
I am trying to set the RAILS_MASTER_KEY as a secret variable in my pipeline and then reference it as environment variable in my docker compose file.
I can confirm that the agent is setting the variable correctly using echo in the pipeline yaml. However, The environment variable is not getting passed/set properly to my docker-compose and ultimately Dockerfile.
I have troubleshoot this for days reading azure docs, stack overflow and its not working.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables
Here is my code:
azure-pipelines.yaml:
steps:
- script: echo "test $(RAILS_MASTER_KEY)"
- script: echo "test $(testvar)"
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'de-identified'
azureContainerRegistry: '{"loginServer":""de-identified"", "id" : "de-identified"}'
dockerComposeFile: '**/docker-compose.yml'
dockerComposeFileArgs: |
RAILS_MASTER_KEY=$(RAILS_MASTER_KEY)
testvar=$(testvar)
action: 'Build services'
env:
RAILS_MASTER_KEY: $(RAILS_MASTER_KEY)
docker-compose.yml:
version: "3.3"
services:
web:
build: .
command: command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' && echo ${RAILS_MASTER_KEY}"
volumes:
- .:/app
ports:
- "3000:3000"
environment:
RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
testvar: ${testvar}
Dockerfile:
FROM ruby:3.1.0-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
postgresql-client \
git \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
ENV BUNDLER_VERSION 2.3.5
ENV RAILS_ENV production
ENV RAILS_MASTER_KEY ${RAILS_MASTER_KEY}
ENV testvar ${testvar}
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN echo "----------------key is 1. ${RAILS_MASTER_KEY}"
RUN echo "----------------key is 11. $( testvar )"
RUN echo "----------------key is 12. $testvar"
RUN echo "----------------key is 2. $[variables.RAILS_MASTER_KEY]"
RUN echo "----------------key is 4. $(RAILS_MASTER_KEY)"
RUN gem install bundler -v $BUNDLER_VERSION
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
RUN rm -f /app/tmp/pids/server.pid
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s", "-e", "production", "-b", "0.0.0.0"]
As you can see, I have echo trying when trying to debug this so please ignore all echo statements.
Any help/guide will be appreciated.
Thank you.
From the above snippets, it seems you're using the dockerComposeFileArgs to specify environment variables.
One option that comes in handy for similar situations (and for local debugging purposes) is to use ARG into Dockerfile.
e.g.
FROM ruby:3.1.0-slim
RUN ...
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
...
so you'll have the possibility to specify the RAILS_MASTER_KEY value at build-time using secret variables from Azure DevOps
Here you can find some useful posts talking about ENV and ARGS keywords:
https://vsupalov.com/docker-build-time-env-values/
https://blog.bitsrc.io/how-to-pass-environment-info-during-docker-builds-1f7c5566dd0e
If a GitLab project is configured on GitLab CI, is there a way to run the build locally?
I don't want to turn my laptop into a build "runner", I just want to take advantage of Docker and .gitlab-ci.yml to run tests locally (i.e. it's all pre-configured). Another advantage of that is that I'm sure that I'm using the same environment locally and on CI.
Here is an example of how to run Travis builds locally using Docker, I'm looking for something similar with GitLab.
Since a few months ago this is possible using gitlab-runner:
gitlab-runner exec docker my-job-name
Note that you need both docker and gitlab-runner installed on your computer to get this working.
You also need the image key defined in your .gitlab-ci.yml file. Otherwise won't work.
Here's the line I currently use for testing locally using gitlab-runner:
gitlab-runner exec docker test --docker-volumes "/home/elboletaire/.ssh/id_rsa:/root/.ssh/id_rsa:ro"
Note: You can avoid adding a --docker-volumes with your key setting it by default in /etc/gitlab-runner/config.toml. See the official documentation for more details. Also, use gitlab-runner exec docker --help to see all docker-based runner options (like variables, volumes, networks, etc.).
Due to the confusion in the comments, I paste here the gitlab-runner --help result, so you can see that gitlab-runner can make builds locally:
gitlab-runner --help
NAME:
gitlab-runner - a GitLab Runner
USAGE:
gitlab-runner [global options] command [command options] [arguments...]
VERSION:
1.1.0~beta.135.g24365ee (24365ee)
AUTHOR(S):
Kamil TrzciĆski <ayufan#ayufan.eu>
COMMANDS:
exec execute a build locally
[...]
GLOBAL OPTIONS:
--debug debug mode [$DEBUG]
[...]
As you can see, the exec command is to execute a build locally.
Even though there was an issue to deprecate the current gitlab-runner exec behavior, it ended up being reconsidered and a new version with greater features will replace the current exec functionality.
Note that this process is to use your own machine to run the tests using docker containers. This is not to define custom runners. To do so, just go to your repo's CI/CD settings and read the documentation there. If you wanna ensure your runner is executed instead of one from gitlab.com, add a custom and unique tag to your runner, ensure it only runs tagged jobs and tag all the jobs you want your runner to be responsible of.
I use this docker-based approach:
Edit: 2022-10
docker run --entrypoint bash --rm -w $PWD -v $PWD:$PWD -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest -c 'git config --global --add safe.directory "*";gitlab-runner exec docker test'
For all git versions > 2.35.2. You must add safe.directory within the container to avoid fatal: detected dubious ownership in repository at.... This also true for patched git versions < 2.35.2. The old command will not work anymore.
Details
0. Create a git repo to test this answer
mkdir my-git-project
cd my-git-project
git init
git commit --allow-empty -m"Initialize repo to showcase gitlab-runner locally."
1. Go to your git directory
cd my-git-project
2. Create a .gitlab-ci.yml
Example .gitlab-ci.yml
image: alpine
test:
script:
- echo "Hello Gitlab-Runner"
3. Create a docker container with your project dir mounted
docker run -d \
--name gitlab-runner \
--restart always \
-v $PWD:$PWD \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
(-d) run container in background and print container ID
(--restart always) or not?
(-v $PWD:$PWD) Mount current directory into the current directory of the container - Note: On Windows you could bind your dir to a fixed location, e.g. -v ${PWD}:/opt/myapp. Also $PWD will only work at powershell not at cmd
(-v /var/run/docker.sock:/var/run/docker.sock) This gives the container access to the docker socket of the host so it can start "sibling containers" (e.g. Alpine).
(gitlab/gitlab-runner:latest) Just the latest available image from dockerhub.
4. Execute with
Avoid fatal: detected dubious ownership in repository at... More info
docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"
Actual execution
docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
# ^ ^ ^ ^ ^ ^
# | | | | | |
# (a) (b) (c) (d) (e) (f)
(a) Working dir within the container. Note: On Windows you could use a fixed location, e.g. /opt/myapp.
(b) Name of the docker container
(c) Execute the command "gitlab-runner" within the docker container
(d)(e)(f) run gitlab-runner with "docker executer" and run a job named "test"
5. Prints
...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...
Note: The runner will only work on the commited state of your code base. Uncommited changes will be ignored. Exception: The .gitlab-ci.yml itself does not have be commited to be taken into account.
Note: There are some limitations running locally. Have a look at limitations of gitlab runner locally.
I'm currently working on making a gitlab runner that works locally.
Still in the early phases, but eventually it will become very relevant.
It doesn't seem like gitlab want/have time to make this, so here you go.
https://github.com/firecow/gitlab-runner-local
If you are running Gitlab using the docker image there: https://hub.docker.com/r/gitlab/gitlab-ce, it's possible to run pipelines by exposing the local docker.sock with a volume option: -v /var/run/docker.sock:/var/run/docker.sock. Adding this option to the Gitlab container will allow your workers to access to the docker instance on the host.
The GitLab runner appears to not work on Windows yet and there is an open issue to resolve this.
So, in the meantime I am moving my script code out to a bash script, which I can easily map to a docker container running locally and execute.
In this case I want to build a docker container in my job, so I create a script 'build':
#!/bin/bash
docker build --pull -t myimage:myversion .
in my .gitlab-ci.yaml I execute the script:
image: docker:latest
services:
- docker:dind
before_script:
- apk add bash
build:
stage: build
script:
- chmod 755 build
- build
To run the script locally using powershell I can start the required image and map the volume with the source files:
$containerId = docker run --privileged -d -v ${PWD}:/src docker:dind
install bash if not present:
docker exec $containerId apk add bash
Set permissions on the bash script:
docker exec -it $containerId chmod 755 /src/build
Execute the script:
docker exec -it --workdir /src $containerId bash -c 'build'
Then stop the container:
docker stop $containerId
And finally clean up the container:
docker container rm $containerId
Another approach is to have a local build tool that is installed on your pc and your server at the same time.
So basically, your .gitlab-ci.yml will basically call your preferred build tool.
Here an example .gitlab-ci.yml that i use with nuke.build:
stages:
- build
- test
- pack
variables:
TERM: "xterm" # Use Unix ASCII color codes on Nuke
before_script:
- CHCP 65001 # Set correct code page to avoid charset issues
.job_template: &job_definition
except:
- tags
build:
<<: *job_definition
stage: build
script:
- "./build.ps1"
test:
<<: *job_definition
stage: test
script:
- "./build.ps1 test"
variables:
GIT_CHECKOUT: "false"
pack:
<<: *job_definition
stage: pack
script:
- "./build.ps1 pack"
variables:
GIT_CHECKOUT: "false"
only:
- master
artifacts:
paths:
- output/
And in nuke.build i've defined 3 targets named like the 3 stages (build, test, pack)
In this way you have a reproducible setup (all other things are configured with your build tool) and you can test directly the different targets of your build tool.
(i can call .\build.ps1 , .\build.ps1 test and .\build.ps1 pack when i want)
I am on Windows using VSCode with WSL
I didn't want to register my work PC as a runner so instead I'm running my yaml stages locally to test them out before I upload them
$ sudo apt-get install gitlab-runner
$ gitlab-runner exec shell build
yaml
image: node:10.19.0 # https://hub.docker.com/_/node/
# image: node:latest
cache:
# untracked: true
key: project-name
# key: ${CI_COMMIT_REF_SLUG} # per branch
# key:
# files:
# - package-lock.json # only update cache when this file changes (not working) #jkr
paths:
- .npm/
- node_modules
- build
stages:
- prepare # prepares builds, makes build needed for testing
- test # uses test:build specifically #jkr
- build
- deploy
# before_install:
before_script:
- npm ci --cache .npm --prefer-offline
prepare:
stage: prepare
needs: []
script:
- npm install
test:
stage: test
needs: [prepare]
except:
- schedules
tags:
- linux
script:
- npm run build:dev
- npm run test:cicd-deps
- npm run test:cicd # runs puppeteer tests #jkr
artifacts:
reports:
junit: junit.xml
paths:
- coverage/
build-staging:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build:stage
- zip -r build.zip build
# cache:
# paths:
# - build
# <<: *global_cache
# policy: push
artifacts:
paths:
- build.zip
deploy-dev:
stage: deploy
needs: [build-staging]
tags: [linux]
only:
- schedules
# # - branches#gitlab-org/gitlab
before_script:
- apt-get update && apt-get install -y lftp
script:
# temporarily using 'verify-certificate no'
# for more on verify-certificate #jkr: https://www.versatilewebsolutions.com/blog/2014/04/lftp-ftps-and-certificate-verification.html
# variables do not work with 'single quotes' unless they are "'surrounded by doubles'"
- lftp -e "set ssl:verify-certificate no; open mediajackagency.com; user $LFTP_USERNAME $LFTP_PASSWORD; mirror --reverse --verbose build/ /var/www/domains/dev/clients/client/project/build/; bye"
# environment:
# name: staging
# url: http://dev.mediajackagency.com/clients/client/build
# # url: https://stg2.client.co
when: manual
allow_failure: true
build-production:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build
- zip -r build.zip build
# cache:
# paths:
# - build
# <<: *global_cache
# policy: push
artifacts:
paths:
- build.zip
deploy-client:
stage: deploy
needs: [build-production]
tags: [linux]
only:
- schedules
# - master
before_script:
- apt-get update && apt-get install -y lftp
script:
- sh deploy-prod
environment:
name: production
url: http://www.client.co
when: manual
allow_failure: true
The idea is to keep check commands outside of .gitlab-ci.yml. I use Makefile to run something like make check and my .gitlab-ci.yml runs the same make commands that I use locally to check various things before committing.
This way you'll have one place with all/most of your commands (Makefile) and .gitlab-ci.yml will have only CI-related stuff.
I have written a tool to run all GitLab-CI job locally without have to commit or push, simply with the command ci-toolbox my_job_name.
The URL of the project : https://gitlab.com/mbedsys/citbx4gitlab
Years ago I build this simple solution with Makefile and docker-compose to run the gitlab runner in docker, you can use it to execute jobs locally as well and should work on all systems where docker works:
https://gitlab.com/1oglop1/gitlab-runner-docker
There are few things to change in the docker-compose.override.yaml
version: "3"
services:
runner:
working_dir: <your project dir>
environment:
- REGISTRATION_TOKEN=<token if you want to register>
volumes:
- "<your project dir>:<your project dir>"
Then inside your project you can execute it the same way as mentioned in other answers:
docker exec -it -w $PWD runner gitlab-runner exec <commands>..
I recommend using gitlab-ci-local
https://github.com/firecow/gitlab-ci-local
It's able to run specific jobs as well.
It's a very cool project and I have used it to run simple pipelines on my laptop.
I have an angular8 project and trying to use the Gitlab shell runner.
Gitlab-CI should build and then stop existing docker container and spawn a new one.
In my host, I have installed docker and Node(using mvn). When the runner executes, I am getting command not found.
Following is my code:
gitlab-ci.yml
stages:
- build-vf
- deploy-vf
build-min-code-vf:
stage: build-vf
tags:
- branch/angular8/VF
only:
- branch/angular8/VF
artifacts:
paths:
- dist/testprofilefrontend-app/
before_script:
- export PATH=$PATH
script:
- node -v
- npm install
- ng build --prod
deploy-production:
stage: deploy-vf
tags:
- branch/angular8/VF
only:
- migration/angular8/VF
before_script:
- export PATH=$PATH
script:
- dockerAll=$(docker ps -a --filter="name=VF-FRONTEND" -q | xargs)
- dockerImage=$(docker images | grep "<image_name>")
- if [ ! -z "$dockerAll" ];
then
docker rm $dockerAll -f &&
docker image build -t <image_name> . &&
docker container run -d -v <dir_name> -p 8082:8080 -- name <name> <image_name> &&
docker push <image>;
else
docker image build -t <image_name> . &&
docker container run -d -v <dir_name> -p 8082:8080 --name <name> <image_name> &&
docker push <image_name>;
fi;
Docker path
/usr/bin/docker
$PATH
/root/.nvm/versions/node/v10.16.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
My assumption is that the runner is checking only /usr/local?
Unfortunately, I could not find any resources related to my case on google. I am quite new with CI/CD knowledge and would appreciate your help.
Thanks.