Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
organize pipelines with reusable workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Frecherenkel60 committed Feb 1, 2024
1 parent 6a4d28b commit 31d564c
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 264 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build-push-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build and push Docker Image to GitHub Container Registry
run-name: Build&Push Image of ${{ github.event.inputs.preview == 'true' && 'Preview' || 'Main' }} to GitHub Container Registry

on:
workflow_call:
inputs:
preview:
required: true
type: boolean
secrets:
GITHUB_TOKEN:
required: true

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-push-docker:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
${{ github.event.inputs.preview == 'true' && 'type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr' || ''}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: ./build/package/Dockerfile
91 changes: 22 additions & 69 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Continuous Delivery
run-name: Deploy to Production

on:
push:
Expand All @@ -9,74 +10,26 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

permissions:
contents: read
packages: write

jobs:
build-push-docker:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: ./build/package/Dockerfile

- name: Copy docker-compose prod file to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_PORT }}
source: "deployments/docker-compose.prod.yml,deployments/server_alpha_db.sql,deployments/provisioning/*,configs/*.yaml"
target: "/home/serveralpha"

- name: Setup SSH
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_PORT }}
script: |
set_key_value() {
key="$1"
value="$2"
file="$3"
if grep -q "^$key=" "$file"; then
sed -i "s/^$key=.*/$key=$value/" "$file"
else
echo "$key=$value" >> "$file"
fi
}

set_key_value "SERVER_IMAGE" "ghcr.io/wwi21seb-projekt/server-alpha:main" ".env"
set_key_value "TRAFFIC_RULE" "Host(\`server-alpha.tech\`)" ".env"
set_key_value "MONITORING_TRAFFIC_RULE" "Host(\`monitoring.server-alpha.tech\`)" ".env"

docker pull ${{ steps.meta.outputs.tags }}
docker-compose -f deployments/docker-compose.prod.yml -p server_alpha down
docker-compose -f deployments/docker-compose.prod.yml -p server_alpha up -d
uses: ./.github/workflows/build-push-image.yaml
with:
preview: false
secrets: inherit

deploy:
needs: build-push-docker
uses: ./.github/workflows/pr-cleanup.yaml
with:
preview: false
docker-image: ${{ steps.meta.outputs.tags }}
compose-name: "server_alpha"
ssh-script: |
set_key_value "SERVER_IMAGE" "ghcr.io/wwi21seb-projekt/server-alpha:main" ".env"
set_key_value "TRAFFIC_RULE" "Host(\`server-alpha.tech\`)" ".env"
set_key_value "MONITORING_TRAFFIC_RULE" "Host(\`monitoring.server-alpha.tech\`)" ".env"
secrets: inherit
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Continuous Integration
run-name: Continuous Integration

on:
pull_request:
Expand Down
61 changes: 61 additions & 0 deletions .github/workflows/comment-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: 'Comment on PR'
run-name: 'Comment on PR'

on:
workflow_call:
inputs:
message:
required: true
type: string
secrets:
GITHUB_TOKEN:
required: true

jobs:
comment:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Comment PR
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const issue_number = context.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const message = `
${{ github.event.inputs.message }}
`;
// Fetch all comments
const comments = await github.rest.issues.listComments({
owner: owner,
repo: repo,
issue_number: issue_number
});

// Find the previous bot comment
const botComment = comments.data.find(comment => comment.user.login === 'github-actions[bot]');

// If it exists, update it
if (botComment) {
await github.rest.issues.updateComment({
owner: owner,
repo: repo,
comment_id: botComment.id,
body: message
});
}
// Else, create a new one
else {
const comment = await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: issue_number,
body: message
});
}
79 changes: 79 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Deploy to Server
run-name: ${{ github.event.inputs.preview == 'true' && 'Deploy to Preview' || 'Deploy to Production' }}

on:
workflow_call:
inputs:
preview:
required: true
type: boolean
description: 'Whether to deploy to the preview domain or not'
ssh-script:
required: true
type: string
description: 'The script to run on the server to set the environment variables'
docker-image:
required: true
type: string
description: 'The docker image to use for the server'
compose-name:
required: true
type: string
description: 'The name of the docker-compose project to use'
secrets:
DEPLOY_HOST:
required: true
description: 'The hostname or IP address of the server to deploy to'
DEPLOY_USER:
required: true
description: 'The username to use for SSH authentication'
DEPLOY_SSH_KEY:
required: true
description: 'The SSH private key to use for authentication'
DEPLOY_PORT:
required: true
description: 'The SSH port to use for authentication'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Copy files to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_PORT }}
source: "deployments/docker-compose.prod.yml,deployments/server_alpha_db.sql,deployments/provisioning/*,configs/*.yaml"
target: "/home/serveralpha/preview/pr-${{ github.event.pull_request.number }}"

- name: Setup SSH
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_PORT }}
script: |
set_key_value() {
key="$1"
value="$2"
file="$3"
if grep -q "^$key=" "$file"; then
sed -i "s/^$key=.*/$key=$value/" "$file"
else
echo "$key=$value" >> "$file"
fi
}
${{ github.event.inputs.ssh-script }}
docker pull ${{ github.event.inputs.docker-image }}
docker-compose -f deployments/docker-compose.prod.yml -p ${{ github.event.inputs.compose-name }} down
docker-compose -f deployments/docker-compose.prod.yml -p ${{ github.event.inputs.compose-name }} up -d
1 change: 1 addition & 0 deletions .github/workflows/pr-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: PR Cleanup
run-name: Cleanup PR-${{ github.event.pull_request.number }} Preview

on:
pull_request:
Expand Down
Loading

0 comments on commit 31d564c

Please sign in to comment.