From debde86048a03d0884c0e71d12aab2f24b32b316 Mon Sep 17 00:00:00 2001 From: moonstar-x Date: Fri, 1 Nov 2024 02:44:23 -0500 Subject: [PATCH] Added CI workflows. --- .github/workflows/callable-build.yml | 63 +++++++++++++ .github/workflows/callable-test.yml | 27 ++++++ .github/workflows/on-pr.yml | 14 +++ .github/workflows/on-push-main.yml | 23 +++++ _old/Jenkinsfile | 129 --------------------------- 5 files changed, 127 insertions(+), 129 deletions(-) create mode 100644 .github/workflows/callable-build.yml create mode 100644 .github/workflows/callable-test.yml create mode 100644 .github/workflows/on-pr.yml create mode 100644 .github/workflows/on-push-main.yml delete mode 100644 _old/Jenkinsfile diff --git a/.github/workflows/callable-build.yml b/.github/workflows/callable-build.yml new file mode 100644 index 0000000..4606f65 --- /dev/null +++ b/.github/workflows/callable-build.yml @@ -0,0 +1,63 @@ +name: Build Docker Image + +on: + workflow_call: + inputs: + image_tag: + type: string + required: true + ghcr_username: + type: string + required: true + ghcr_image_name: + type: string + required: true + secrets: + ghcr_token: + required: true + +jobs: + build: + name: Build Docker Image + runs-on: ubuntu-latest + + steps: + - name: Checkout the Repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Cache Docker Layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ inputs.ghcr_username }} + password: ${{ secrets.ghcr_token }} + + - name: Build & Push Docker Image + id: docker_build + uses: docker/build-push-action@v6 + env: + GHCR_IMAGE: ghcr.io/${{ inputs.ghcr_image_name }} + with: + context: . + push: true + tags: | + ${{ env.GHCR_IMAGE }}:${{ inputs.image_tag }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache + platforms: linux/amd64,linux/arm64/v8 + + - name: Image Digest + run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/.github/workflows/callable-test.yml b/.github/workflows/callable-test.yml new file mode 100644 index 0000000..2128ac6 --- /dev/null +++ b/.github/workflows/callable-test.yml @@ -0,0 +1,27 @@ +name: Run Tests + +on: + workflow_call: + +jobs: + test: + name: Run Tests + runs-on: ubuntu-latest + + steps: + - name: Checkout the Repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: 3.12 + cache: pip + + - name: Install Dependencies + run: pip install -r requirements.txt + + - name: Run Tests + run: pytest test diff --git a/.github/workflows/on-pr.yml b/.github/workflows/on-pr.yml new file mode 100644 index 0000000..8afa692 --- /dev/null +++ b/.github/workflows/on-pr.yml @@ -0,0 +1,14 @@ +name: On Pull Request + +on: + pull_request: + types: + - opened + - edited + - synchronize + - reopened + +jobs: + test: + name: Run Tests + uses: ./.github/workflows/callable-test.yml diff --git a/.github/workflows/on-push-main.yml b/.github/workflows/on-push-main.yml new file mode 100644 index 0000000..53eb54e --- /dev/null +++ b/.github/workflows/on-push-main.yml @@ -0,0 +1,23 @@ +name: On Push (Main) + +on: + push: + branches: + - main + +jobs: + test: + name: Run Tests + uses: ./.github/workflows/callable-test.yml + + build: + name: Build Docker Image + uses: ./.github/workflows/callable-build.yml + needs: + - test + with: + ghcr_username: ${{ github.actor }} + ghcr_image_name: ${{ github.repository }} + image_tag: latest + secrets: + ghcr_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/_old/Jenkinsfile b/_old/Jenkinsfile deleted file mode 100644 index 5450492..0000000 --- a/_old/Jenkinsfile +++ /dev/null @@ -1,129 +0,0 @@ -pipeline { - agent { - label 'main-agents' - } - - options { - ansiColor('xterm') - timestamps() - } - - environment { - DOCKER_IMAGE = 'webpep/python-rest-api' - DOCKER_REGISTRY = credentials('docker_registry') - } - - stages { - stage('Unlock Keychain (Mac Agent)') { - environment { - MAC_MINI_KEYCHAIN = credentials('mac_mini_keychain') - } - - steps { - script { - def isMac = sh(script: 'uname -a', returnStdout: true).contains('Darwin') - - if (isMac) { - echo "Unlocking macOS keychain..." - sh "security unlock-keychain -p $MAC_MINI_KEYCHAIN" - } else { - echo "Current agent is not macOS, skipping..." - } - } - } - } - - stage('Run Tests') { - agent { - docker { - image 'python:3.9' - args '-u root' - reuseNode true - } - } - - steps { - echo 'Running tests...' - - sh 'pip install -r requirements.txt' - sh 'python -m unittest discover -s test' - } - } - - stage('Build Docker Image') { - when { - allOf { - branch 'master' - - not { - changeRequest() - } - } - } - - steps { - echo 'Building docker image...' - - script { - image = docker.build(DOCKER_IMAGE) - } - } - } - - stage('Deploy Docker Image') { - when { - allOf { - branch 'master' - - not { - changeRequest() - } - } - } - - steps { - echo 'Deploying docker image to registry...' - - script { - docker.withRegistry(DOCKER_REGISTRY, 'gitea_packages_account') { - image.push('latest') - } - } - } - } - - stage('Update Application Deployment') { - when { - allOf { - branch 'master' - - not { - changeRequest() - } - } - } - - environment { - DEPLOY_SERVER_CREDS = credentials('deployment_ssh_creds') - DEPLOY_SERVER_HOST = credentials('deployment_ssh_host') - - DEPLOYMENT_DIR = '/home/dev/services/webpep' - } - - steps { - echo 'Updating application deployment...' - - script { - def remote = [:] - remote.name = 'deployment' - remote.host = DEPLOY_SERVER_HOST - remote.user = DEPLOY_SERVER_CREDS_USR - remote.password = DEPLOY_SERVER_CREDS_PSW - remote.allowAnyHosts = true - - sshCommand remote: remote, command: "cd ${DEPLOYMENT_DIR} && docker-compose stop && docker-compose rm -f && docker-compose pull && docker-compose up -d" - } - } - } - } -}