144 lines
4.8 KiB
YAML
144 lines
4.8 KiB
YAML
name: "Portainer CI/CD Pipeline"
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
stack-name:
|
|
description: "Name of the Portainer stack to deploy"
|
|
required: true
|
|
type: string
|
|
portainer-endpoint:
|
|
description: "Portainer endpoint ID"
|
|
required: true
|
|
type: string
|
|
repo-compose-file:
|
|
description: "Path to docker-compose file"
|
|
required: false
|
|
default: "docker-compose.yml"
|
|
type: string
|
|
image-name:
|
|
description: "Docker image name"
|
|
required: true
|
|
type: string
|
|
secrets:
|
|
REGISTRY_USERNAME:
|
|
description: "Docker registry username"
|
|
required: true
|
|
REGISTRY_PASSWORD:
|
|
description: "Docker registry password"
|
|
required: true
|
|
CI_GITEA_TOKEN:
|
|
description: "Token for version bump and push"
|
|
required: true
|
|
PORTAINER_TOKEN:
|
|
description: "Portainer API token"
|
|
required: true
|
|
REGISTRY_URL:
|
|
description: "Docker registry URL (e.g. ghcr.io)"
|
|
required: true
|
|
PORTAINER_URL:
|
|
description: "Base URL of the Portainer API (e.g. https://portainer.example.com/api)"
|
|
required: true
|
|
|
|
jobs:
|
|
# --------------------------------------------------------
|
|
# Semantic Release (Version bump + changelog)
|
|
# --------------------------------------------------------
|
|
release:
|
|
name: Semantic Release
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.CI_GITEA_TOKEN }}
|
|
IMAGE_NAME: ${{ inputs.image-name }}
|
|
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
token: ${{ secrets.CI_GITEA_TOKEN }}
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: 20
|
|
|
|
- name: Install semantic-release
|
|
run: npm install -g semantic-release @semantic-release/changelog @semantic-release/git @semantic-release/exec
|
|
|
|
- name: Run semantic-release
|
|
run: npx semantic-release
|
|
env:
|
|
GIT_AUTHOR_NAME: "CI Bot"
|
|
GIT_AUTHOR_EMAIL: "ci@etwo.dev"
|
|
GIT_COMMITTER_NAME: "CI Bot"
|
|
GIT_COMMITTER_EMAIL: "ci@etwo.dev"
|
|
|
|
- name: Install yq
|
|
run: |
|
|
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq
|
|
chmod +x /usr/bin/yq
|
|
|
|
- name: Update docker-compose.yml
|
|
run: |
|
|
VERSION=$(cat .release-version)
|
|
echo "Updating docker-compose.yml with image ${IMAGE_NAME}:${VERSION}"
|
|
yq -i ".services.\"${IMAGE_NAME}\".image = \"${REGISTRY_URL}/${IMAGE_NAME}:${VERSION}\"" docker-compose.yml
|
|
|
|
- name: Commit and push docker-compose.yml changes
|
|
run: |
|
|
VERSION=$(cat .release-version)
|
|
git config user.name "CI Bot"
|
|
git config user.email "ci@etwo.dev"
|
|
git add docker-compose.yml
|
|
git commit -m "chore(release): update compose to ${VERSION} [skip ci]" || echo "No changes to commit"
|
|
git push origin HEAD
|
|
|
|
# --------------------------------------------------------
|
|
# Build and Push Docker Image
|
|
# --------------------------------------------------------
|
|
build:
|
|
name: Build and Push Docker Image
|
|
runs-on: ubuntu-latest
|
|
needs: release
|
|
env:
|
|
IMAGE_NAME: ${{ inputs.image-name }}
|
|
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
|
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Log in to Docker registry
|
|
run: |
|
|
echo "$REGISTRY_PASSWORD" | docker login $REGISTRY_URL -u "$REGISTRY_USERNAME" --password-stdin
|
|
|
|
- name: Build Docker image
|
|
run: |
|
|
VERSION=$(cat .release-version || echo "latest")
|
|
echo "Building docker image with tag $VERSION"
|
|
docker build -t $REGISTRY_URL/${IMAGE_NAME}:$VERSION .
|
|
docker push $REGISTRY_URL/${IMAGE_NAME}:$VERSION
|
|
|
|
# --------------------------------------------------------
|
|
# Deploy to Portainer (using your Action)
|
|
# --------------------------------------------------------
|
|
deploy:
|
|
name: Deploy to Portainer
|
|
runs-on: ubuntu-latest
|
|
needs: build
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Deploy stack via Portainer API
|
|
uses: https://git.etwo.dev/actions/portable@v1
|
|
with:
|
|
portainer-url: ${{ secrets.PORTAINER_URL }}
|
|
portainer-token: ${{ secrets.PORTAINER_TOKEN }}
|
|
portainer-endpoint: ${{ inputs.portainer-endpoint }}
|
|
stack-name: ${{ inputs.stack-name }}
|
|
repo-url: ${{ github.server_url }}/${{ github.repository }}
|
|
repo-ref: ${{ github.ref }}
|
|
repo-compose-file: ${{ inputs.repo-compose-file }}
|