Push and Deploy #22
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: golang-pipeline | |
on: | |
push: | |
branches: [main, staging] | |
workflow_dispatch: | |
inputs: | |
deploy: | |
description: 'Deploy' | |
type: choice | |
default: staging | |
options: | |
- production | |
- staging | |
required: true | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags') | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Run Unit Tests | |
run: go test | |
build-push: | |
runs-on: ubuntu-latest | |
needs: test | |
if: startsWith(github.ref, 'refs/tags') | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Extract Version | |
id: version_step | |
run: | | |
echo "##[set-output name=version;]VERSION=${GITHUB_REF#$"refs/tags/v"}" | |
echo "##[set-output name=version_tag;]$GITHUB_REPOSITORY:${GITHUB_REF#$"refs/tags/v"}" | |
echo "##[set-output name=latest_tag;]$GITHUB_REPOSITORY:latest" | |
- name: Print Version | |
run: | | |
echo ${{steps.version_step.outputs.version_tag}} | |
echo ${{steps.version_step.outputs.latest_tag}} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v1 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: PrepareReg Names | |
id: read-docker-image-identifiers | |
run: | | |
echo VERSION_TAG=$(echo ${{ steps.version_step.outputs.version_tag }} | tr '[:upper:]' '[:lower:]' | sed 's/zgunz42/kangmicin/') >> $GITHUB_ENV | |
echo LASTEST_TAG=$(echo ${{ steps.version_step.outputs.latest_tag }} | tr '[:upper:]' '[:lower:]' | sed 's/zgunz42/kangmicin/') >> $GITHUB_ENV | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 | |
with: | |
context: . | |
push: true | |
tags: | | |
${{ steps.meta.outputs.tags }} | |
${{ env.VERSION_TAG }} | |
${{ env.LASTEST_TAG }} | |
labels: ${{ steps.meta.outputs.labels }} | |
deploy-prod: | |
runs-on: ubuntu-latest | |
needs: build-push | |
if: github.ref === 'refs/heads/main' && ${{ github.event.inputs.deploy == 'production' }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Create environment file | |
run: | | |
echo "VERSION=${GITHUB_REF#$"refs/tags/v"}" > ./kubernetes-manifests/overlay/production/.env | |
echo "MQTT_BROKER_URL=mosquitto.mosquitto:1883" >> ./kubernetes-manifests/overlay/production/.env | |
echo "MQTT_USERNAME=${{ secrets.MQTT_USERNAME }}" >> ./kubernetes-manifests/overlay/production/.env | |
echo "MQTT_PASSWORD=${{ secrets.MQTT_PASSWORD }}" >> ./kubernetes-manifests/overlay/production/.env | |
- name: 'Kustomize Build' | |
id: kustomize | |
uses: karancode/kustomize-github-action@master | |
with: | |
kustomize_version: '5.3.0' | |
kustomize_install: true | |
kustomize_build_dir: './kubernetes-manifests/overlay/production' | |
kustomize_comment: false | |
kustomize_output_file: "servercopilot/rendered.yaml" | |
token: ${{ secrets.GITHUB_ACCESS_TOKEN }} | |
- name: Upload to server | |
uses: zgunz42/cloudflared-scp-action@v1.1.1 | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
private_key: ${{ secrets.SSH_PRIVKEY }} | |
port: ${{ secrets.PORT }} | |
from: servercopilot/rendered.yaml | |
to: /home/adi/server/production/servercopilot.yaml | |
deploy-staging: | |
runs-on: ubuntu-latest | |
needs: build-push | |
if: ${{ github.event.inputs.deploy == 'staging' }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Create environment file | |
run: | | |
echo "VERSION=${GITHUB_REF#$"refs/tags/v"}" > ./kubernetes-manifests/overlay/staging/.env | |
echo "MQTT_BROKER_URL=mosquitto.mosquitto:1883" >> ./kubernetes-manifests/overlay/staging/.env | |
echo "MQTT_USERNAME=${{ secrets.MQTT_USERNAME }}" >> ./kubernetes-manifests/overlay/staging/.env | |
echo "MQTT_PASSWORD=${{ secrets.MQTT_PASSWORD }}" >> ./kubernetes-manifests/overlay/staging/.env | |
- name: 'Kustomize Build' | |
id: kustomize | |
uses: karancode/kustomize-github-action@master | |
with: | |
kustomize_version: '5.3.0' | |
kustomize_install: true | |
kustomize_build_dir: './kubernetes-manifests/overlay/stagging' | |
kustomize_comment: false | |
kustomize_output_file: "servercopilot/rendered.yaml" | |
token: ${{ secrets.GITHUB_ACCESS_TOKEN }} | |
- name: Upload to server | |
uses: zgunz42/cloudflared-scp-action@v1.1.1 | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
private_key: ${{ secrets.SSH_PRIVKEY }} | |
port: ${{ secrets.PORT }} | |
from: servercopilot/rendered.yaml | |
to: /home/adi/server/staging/servercopilot.yaml |