How can I write a Groovy Jenkinsfile for a Django application to run my tests? - django

I have recently started using Jenkins and I am wanting to use Multibranch Pipelines so I can test the various feature branches in my project.
The project is using django 1.8. So far my Jenkinsfile looks like this and fails in the testing stage as django can not find my settings file even though it is there:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get the code from a GitHub repository
git credentialsId: 'mycredentials', url: 'https://github.com/<user>/<project>/'
// Mark the code build 'stage'....
stage 'Build'
env.WORKSPACE = pwd()
sh 'virtualenv --python=python34 venv'
sh 'source venv/bin/activate'
sh 'pip install -r requirements.txt'
env.DJANGO_SETTINGS_MODULE = "<appname>.settings.jenkins"
// Start the tests
stage 'Test'
sh 'python34 manage.py test --keepdb'
}

venv/bin/activate does no more than setting up proper environmental paths.
You can do it on your own by adding at the beginning assuming that env.WORKSPACE is your project directory:
env.PATH="${env.WORKSPACE}/venv/bin:/usr/bin:${env.PATH}"
Later, if you want to call virtualenved python, just need to prepend it with specified path like here:
stage 'Test'
sh "${env.WORKSPACE}/venv/bin/python34 manage.py test --keepdb'
Or to call pip
sh "${env.WORKSPACE}/venv/bin/pip install -r requirements.txt"

Related

Can't launch Chromium as executable doesn't exists if customized image is build from Dockerfile

FROM alpine:latest
# Copy source to container
RUN mkdir -p /usr/app
# Copy source code
COPY package.json /usr/app
COPY package-lock.json /usr/app
COPY . /usr/app
# Set working directory
WORKDIR /usr/app
# Environment variables
ENV BASE_URL="Local https url"
ENV PARALLEL_RUN=false
ENV TAG=int
ENV PLAYWRIGHT_BROWSERS_PATH=/usr/lib
# npm install
RUN apk add --update npm
RUN apk add chromium
# Run tests
RUN npm run codeceptjs
Above is the Dockerfile. When tried to Build the image from docker file then I am getting below error:
13 8.596 Error: browserType.launch: Failed to launch chromium because executable doesn't exist at /usr/lib/chromium-888113/chrome-linux/chrome
#13 8.596 Try re-installing playwright with "npm install playwright"**
Although, I can see chromium is getting installed at the mentioned path but still it saying "executable not present".
I believe your problem lies with using alpine.
According to the playwright developers, there are no plans to support playwright on alpine. This makes your whole undertaking more complex. It's correct that you need to provide your own chromium and cannot use the browsers that come with playwright. Therefore, you should set PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD to prevent any (incompatible) browsers from being loaded.
The chromium executable should be in your Docker image under /usr/bin/chromium-browser. You need to use playwright's browserType.launch to set the path to the executable:
const { chromium } = require("playwright-chromium");
// ...
const browser = await chromium.launch({
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
});
If you want a simpler solution, I would suggest using the Docker image of Zanika, containing chromium and playwright already. Here the link to the tag on DockerHub. At the very least you can see it as a reference implementation if you still want to use your own image.

unable to complete django installation

I downloaded the necessary files from the link https://github.com/django/django.git and pasted them in to my virtual env directory
After setting up and activating the virtualenv, when I run the following command:
$ pip install -e django/
It produces this error:
(ENV) C:\WINDOWS\system32>pip install -e django/ django/ should either
be a path to a local project or a VCS url beginning with svn+, git+,
hg+, or bzr+
I am a Windows user. I think the command is for bash not for cmd.
Is it necessary to use this git tool to finally work with django ?
As instructed on the Django website :
If you're just starting out with Django development I'd recommend looking at some YouTube videos before jumping into the Django docs. Personally when I was starting out I found that the docs were quite hard to understand in the beginning, but as you get better you can refer back to them more and more.
Here's a good beginner video series to get you started.
In any case, I would recommend using virtualenvwrapper-win so that you can work on multiple Django projects without any conflicts.
First, ensure that you have added Python to the Windows environment. Open CMD and run pip install virtualenvwrapper-win.
Then cd to whichever directory your project files will be in and run mkvirtualenv projectname.
Finally run setprojectdir path/to/folder
Now whenever you want to enter that virtual environment and work on your project all you have to do is run the command workon projectname and it'll do the rest for you. You'll know it worked if on each new line in the command prompt it gives you (projectname) in brackets.
To actually install Django all you need to run is pip install django while in the virtual environment.
From your question, I suppose that you are trying to install django inside your virtual directory. If that is correct you dont need to get it from git.
Alternate way is to create a directory "main" and then project directory "mydjangoproject" inside it and a virtual environment "env".
C:\>mkdir main
C:\>cd main
C:\main>mkdir mydjangoproject
C:\main>virtualenv env
Now activate the virtual environment.
C:\main>env\Scripts\activate
Then install all the package in it. e.g
(env) C:\main>pip install django

How to runserver and see website for github project files

New to Django, I am trying to view the website with its associated functionality generated by the project files here:
https://github.com/tomwalker/django_quiz
When I use the usual: manage.py runserver on the command prompt, it says that there is no manage.py file, and there isn't.
Worryingly, I followed the instructions for installation which suggested: Run pip install -r requirements.txt
...and my computer proceeded to de-install Django 2.0? What is that about, and can anyone explain how to restore settings if I messed them up completely.
The second part of the instructions for installation asks to change something in the INSTALLED_APPS and urls.py section, but where? There is nothing in the root directory and it doesn't specify which folder/app to do this in?
I don't quite understand how to "run" (see/view) these files and see this quiz app in process on my local host. What do I need to add? Why is the manage.py file not included?
Any explanation or something to point me in the right direction would be appreciated
The github project contains only Django apps. Not whole project. You need to integrate this in your Django project. You can run it by following below steps.
Create New Django Project
Clone github repo in your project. Run following commands in your project directory.
git clone https://github.com/tomwalker/django_quiz.git
mv django_quiz/* .
rm -rf django_quiz
Add essay, true_false, quiz, multichoice in your installed apps
Install requirements with pip install -r requirements.txt
Create Migrations
Run Migrations
Add url(r'^q/', include('quiz.urls')) in your project urls.
Run server with python manage.py runserver

Heroku: run npm install and gulp build for a Django app

I have a Django app that I managed to deploy with Heroku. My Procfile file only contains :
web: gunicorn omegapp3.wsgi --log-file -
So when I run heroku local it works.
But when I deploy with heroku push master, the console detects a Node app because the app has a package.json and then the build fails.
What I would like to do is the following :
run npm install to install gulp etc.
run gulp build.
Do you know how I can do that ?
According to the official documentation, you should add multiple buildpacks to your setup, rather than a single multi buildpack.
For example, if you wanted to deploy an app that uses a Nodejs package (i.e. grunt, gulp, etc) to do some setup on your app, you would run this from your command line:
heroku buildpacks:add --index 1 heroku/nodejs
The add command adds the Nodejs buildpack as an additional buildpack, rather than replacing your current buildpack. Note the --index 1. This specifies that the Nodejs buildpack is the first in the order of buildpacks. This is important because the final buildpack is the one used for actual process type. You can call heroku buildpacks from the command line to verify the buildpack setup. I run a Python app, so my heroku buildpacks looks like this:
=== your_app_name_here Buildpack URLs
1. heroku/nodejs
2. https://github.com/heroku/heroku-buildpack-python
Then, as stated by rocketer, you can place this in your package.json file to be run on deploy:
"scripts": {
"postinstall": "./node_modules/.bin/gulp build"
}
Solved by using $ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git.
It allows to use node and python (you have to specify in a .buildpacks file).
In ordre to run gulp build, I added the following to my package.json :
"scripts": {
"postinstall": "./node_modules/.bin/gulp build"
}

VirtualEnv is installed, but cannot find bin/activate

We had a working project with git, and our instructor moved our project over to mercurial after making some changes.
I pulled the new project and started it up under a new folder.
I tried running a virtualenv for the new project but I get: Requirement already satisfied
I would usually then run $ . bin/activate but I cannot find a bin folder.
How do I get a virtualenv setup for this project at this point?
The place where you have your project isn't necessarily the place where you have the environment. Did you have the environment in git too?
Perhaps what you need to do (in case you don't have a versioned environment) is recreate the environment (virtualenv environment), install the dependencies (pip install -r req.txt) and then activate it (source path/to/environment/bin/activate).
Good luck.
Ok
Try this an awesome link to How to install django in virtual environment .
http://ayarshabeer.com/post/50973941605/install-multiple-django-version-using-virtualenvwrapper
Use the following steps
virtualenv virenv_name --no-site-packages - create new virtual environment in git
pip install -r requirements.pip - It can be used for install tools in
(requirements.pip files)
yolk -l - It will used for list install files
source git/virenv_name/bin/activate - Activate virtualenv
deactivate - Deactivate virtualenv