Skip to content
Snippets Groups Projects
Unverified Commit 0415ea13 authored by Giuseppe Carboni's avatar Giuseppe Carboni Committed by GitHub
Browse files

Continuous integration (#919)

* Test for caching the virtual machine

* Update for triggering the workflow

* Fixed cache files path

* Updated download-vm workflow

* Updated deploy-discos workflow

* Removed download workflow. It will go on master.

* Updated deployment workflow

* Updated variable for branch name

* Shortened the job name

* Added push as CI event
parent cc4c6d63
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import os
import io
TOKEN_FILE = 'token.json'
VM_FILE_PATH = '/home/runner/discos_manager.ova'
ARCHIVE_FILE_PATH = '/home/runner/vagrant.tar.gz'
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
# Create the token file from the GH Secret
with open(TOKEN_FILE, 'w') as tokenfile:
tokenfile.write(os.environ.get('GOOGLE_DRIVE_TOKEN'))
# Authenticate with the token and eventually update it
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
if creds.expired and creds.refresh_token:
creds.refresh(Request())
# Download the VM
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
if creds.expired and creds.refresh_token:
creds.refresh(Request())
service = build('drive', 'v3', credentials=creds)
downloader = MediaIoBaseDownload(
io.FileIO(ARCHIVE_FILE_PATH, 'wb'),
service.files().get_media(
fileId=os.environ.get('PROVISIONED_ARCHIVE_GDRIVE_ID')
)
)
done = False
while not done:
_, done = downloader.next_chunk()
downloader = MediaIoBaseDownload(
io.FileIO(VM_FILE_PATH, 'wb'),
service.files().get_media(
fileId=os.environ.get('PROVISIONED_VM_GDRIVE_ID'),
),
chunksize=5*1024*1024
)
done = False
while not done:
_, done = downloader.next_chunk()
# Finally update the token file
with open(TOKEN_FILE, 'w') as tokenfile:
tokenfile.write(creds.to_json())
google-auth
google-auth-oauthlib
google-auth-httplib2
google-api-python-client
...@@ -2,10 +2,22 @@ name: DISCOS deployment and build ...@@ -2,10 +2,22 @@ name: DISCOS deployment and build
on: on:
push: push:
workflow_dispatch: pull_request:
jobs: jobs:
wait-for-download:
name: Wait for any pending download
runs-on: ubuntu-latest
concurrency:
group: download
cancel-in-progress: false
steps:
- name: Go on with deployment
run: true
deploy-discos: deploy-discos:
name: Deploy DISCOS
runs-on: ubuntu-latest
needs: wait-for-download
env: env:
REPOSITORY_TOKEN: "${{ secrets.DEPENDENCIES_TOKEN }}" REPOSITORY_TOKEN: "${{ secrets.DEPENDENCIES_TOKEN }}"
GH_TOKEN: "${{ secrets.GH_WORKFLOWS_TOKEN }}" GH_TOKEN: "${{ secrets.GH_WORKFLOWS_TOKEN }}"
...@@ -16,32 +28,37 @@ jobs: ...@@ -16,32 +28,37 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
station: ['SRT', 'Medicina', 'Noto'] station: ['SRT', 'Medicina', 'Noto']
runs-on: ubuntu-latest
steps: steps:
- name: Free up space - name: Free up space
uses: jlumbroso/free-disk-space@main uses: jlumbroso/free-disk-space@main
with: with:
tool-cache: true tool-cache: true
- name: Install Vagrant - name: Install Vagrant and VirtualBox
run: | run: |
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant sudo apt update && sudo apt install vagrant
- name: Install VirtualBox
run: |
sudo apt install virtualbox sudo apt install virtualbox
- name: Clone the repository
uses: actions/checkout@v4
- name: Set up Python - name: Set up Python
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3' python-version: '3'
check-latest: true check-latest: true
- name: Download the provisioned virtual machine from Google Drive - name: Clone the deployment repository
uses: actions/checkout@v4
with:
repository: 'discos/deployment'
- name: Install deployment package and dependencies
run: | run: |
pip install -r .github/utils/gdrive_requirements.txt python -m pip install -r requirements.txt
python .github/utils/download_from_gdrive.py pip install .
gh secret set GOOGLE_DRIVE_TOKEN --org discos --visibility selected --repos discos,deployment < token.json - name: Retrieve the VM from cache
uses: actions/cache/restore@v4
with:
key: discos-manager-vm
path: |
/home/runner/discos_manager.ova
/home/runner/vagrant.tar.gz
- name: Install the virtual machine - name: Install the virtual machine
run: | run: |
vboxmanage import discos_manager.ova --vsys 0 --options keepallmacs vboxmanage import discos_manager.ova --vsys 0 --options keepallmacs
...@@ -53,17 +70,16 @@ jobs: ...@@ -53,17 +70,16 @@ jobs:
sed -i "s/$(cat id)/$(vboxmanage list vms | grep 'discos_manager' | grep -oP '(?<=\{)[0-9a-fA-F-]+(?=\})')/" action_provision sed -i "s/$(cat id)/$(vboxmanage list vms | grep 'discos_manager' | grep -oP '(?<=\{)[0-9a-fA-F-]+(?=\})')/" action_provision
sed -i "s/$(cat id)/$(vboxmanage list vms | grep 'discos_manager' | grep -oP '(?<=\{)[0-9a-fA-F-]+(?=\})')/" id sed -i "s/$(cat id)/$(vboxmanage list vms | grep 'discos_manager' | grep -oP '(?<=\{)[0-9a-fA-F-]+(?=\})')/" id
working-directory: /home/runner/.deployment/.vagrant/machines/manager/virtualbox/ working-directory: /home/runner/.deployment/.vagrant/machines/manager/virtualbox/
- name: Clone the deployment repository - name: Set the branch name
uses: actions/checkout@v4
with:
repository: 'discos/deployment'
- name: Install deployment package and dependencies
run: | run: |
python -m pip install -r requirements.txt if [ "${{ github.event_name }}" == "push" ]; then
pip install . echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
fi
- name: Deploy DISCOS - name: Deploy DISCOS
run: | run: |
discos-deploy manager:development --deploy-only -s ${{ matrix.station }} -b ${{ github.ref_name }} discos-deploy manager:development --deploy-only -s ${{ matrix.station }} -b ${{ env.BRANCH_NAME }}
- name: Shutdown the virtual machine - name: Shutdown the virtual machine
run: | run: |
discos-vms stop discos-vms stop
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment