welcome: please sign in

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 4 as of 2020-02-13 16:44:42

location: versionControl / canvasSync

Gitlab to Canvas syncing

Gitlab CI

Gitlab CI is run through a file called .gitlab-ci.yml in the root folder of the repository. This file can setup the environment we need to run certain scripts, define how and what scripts should be run and define in what cases it should be run. For our example we use this file

   1 image: "python:3.7"
   2 
   3 before_script:
   4   - python3 -m pip install canvasapi
   5 
   6 canvas_push:
   7   script:
   8     - python3 to_canvas/canvas_sync.py
   9   stage: deploy
  10   only:
  11     refs:
  12       - master
  13     changes:
  14       - to_canvas/**/*.{pdf}

where image tells the runner to use a python3 docker image, before_script runs a command in the container created from the image that installs the package canvasapi and canvas_push runs a script if any of the specified files has changed for the specified branch.

Runners

See https://gitlab.control.lth.se/regler/<REPO>/-/settings/ci_cd, where <REPO> should be replaced with the repository name, for instructions about runners. Hopefully we will soon have some shared runners for the department, but for the moment they have to be set up for each project specifically.

Python script

The current script just takes all pdf files in one folder and pushed them to a folder on canvas. The script needs the names of the folders, the course code in canvas

   1 import os
   2 from canvasapi import Canvas
   3 
   4 # Set these three values
   5 canvas_course_code = 1342
   6 canvas_pdf_path = "from_gitlab"
   7 source_path = "to_canvas"
   8 
   9 canvas_base_path = "course files"
  10 API_URL = "https://canvas.education.lu.se/"
  11 API_TOKEN = os.environ["CANVAS_TOKEN".format(canvas_course_code)]
  12 
  13 canvas = Canvas(API_URL, API_TOKEN)
  14 course = canvas.get_course(canvas_course_code)
  15 
  16 # Check if folder exists, otherwise create it
  17 base_folder = None
  18 pdf_folder = None
  19 for folder in course.get_folders():
  20     if folder.full_name == "{}/{}".format(canvas_base_path, canvas_pdf_path):
  21         pdf_folder = folder
  22     if folder.full_name == canvas_base_path:
  23         base_folder = folder
  24 if pdf_folder is None:
  25     pdf_folder = base_folder.create_folder(canvas_pdf_path)
  26 
  27 # Find all pdf files in to_canvas and upload to canvas folder from_gitlab
  28 for f in os.listdir(source_path):
  29     if f.endswith(".pdf"):
  30         pdf_folder.upload(os.path.join(source_path, f))

Canvas API Token

The script assumes there exists a variable for the Canvas API token in the repository. This variable is added to the gitlab runner as an environment variable and can then be used without us having it hardcoded anywhere. A token can be added by navigating to https://gitlab.control.lth.se/regler/<REPO>/-/settings/ci_cd, where you replace <REPO> with the repository name, and looking under Variables. There you add a token with name CANVAS_TOKEN and value 13596~t2TYuTRlaJnfvyTznfvhDOqq7B85o2wD2jU8NBCsixzLirB8nceTFIvUzbldIa1K. The API token can also be generated in a canvas profile but this is a token generated from a department account.