Improve dev workflow (#49) #20
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: Docker Image api | |
on: | |
push: | |
branches: ['**'] | |
paths: | |
- 'api/**' | |
- '.github/workflows/docker-api.yml' | |
workflow_dispatch: | |
branches: ['**'] | |
jobs: | |
docker-api: | |
runs-on: ubuntu-latest | |
outputs: | |
img_tag: ${{ steps.preserve_img_tag.outputs.img_tag }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Compute image tag | |
run: | | |
echo "IMG_TAG=$(echo ${GITHUB_SHA} | sed -E 's/refs\/(heads|tags)\///g' | sed -e 's/\//-/g')" >> $GITHUB_ENV | |
- name: Preserve image tag | |
id: preserve_img_tag | |
run: | | |
echo "img_tag=${{ env.IMG_TAG }}" >> $GITHUB_OUTPUT | |
- name: Kaniko build | |
uses: julsemaan/action-kaniko@master | |
with: | |
registry: ghcr.io | |
image: julsemaan/anyfile-notepad/api | |
tag: ${{ env.IMG_TAG }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
cache: true | |
cache_registry: ghcr.io/julsemaan/anyfile-notepad/api/cache | |
path: api/ | |
extra_args: --push-retry 5 | |
deploy: | |
needs: docker-api | |
if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/production' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Use staging target servers if necessary | |
run: | | |
echo "TARGET_SERVERS=${{ secrets.ANSIBLE_STAGING_TARGET }}" >> $GITHUB_ENV | |
echo "TARGET_IMAGE=staging" >> $GITHUB_ENV | |
if: github.ref == 'refs/heads/staging' | |
- name: Use production target servers if necessary | |
run: | | |
echo "TARGET_SERVERS=${{ secrets.ANSIBLE_PRODUCTION_TARGET }}" >> $GITHUB_ENV | |
echo "TARGET_IMAGE=production" >> $GITHUB_ENV | |
if: github.ref == 'refs/heads/production' | |
- name: Setup deploy-env | |
run: | | |
echo "::set-output name=TARGET_SERVERS::$TARGET_SERVERS" | |
echo "::set-output name=TARGET_IMAGE::$TARGET_IMAGE" | |
id: deploy-env | |
- name: Deploy app | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AFN_MANAGER_HOST }} | |
username: ${{ secrets.AFN_MANAGER_USERNAME }} | |
key: ${{ secrets.AFN_MANAGER_KEY }} | |
port: ${{ secrets.AFN_MANAGER_PORT }} | |
script: | | |
${{ secrets.AFN_MANAGER_DEPLOY_API_SCRIPT }} ghcr.io/julsemaan/anyfile-notepad/api:${{ steps.deploy-env.outputs.TARGET_IMAGE }} ${{ steps.deploy-env.outputs.TARGET_SERVERS }} | |
pr_afn_k8s: | |
needs: docker-api | |
concurrency: | |
group: prs-to-afn-k8s | |
cancel-in-progress: false | |
if: github.ref == 'refs/heads/master' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
repository: julsemaan/afn-k8s | |
token: ${{ secrets.GHA_REPO_TOKEN }} | |
fetch-depth: 0 | |
- uses: frenck/action-setup-yq@v1 | |
- name: PR tag change to afn-k8s | |
env: | |
IMG_TAG: ${{needs.docker-api.outputs.img_tag}} | |
GH_TOKEN: ${{ secrets.GHA_REPO_TOKEN }} | |
run: | | |
BRANCH_ID=`date "+%Y%m%d"` | |
BRANCH_NAME=tmp/$BRANCH_ID | |
# Checkout the remote ref if it already exist, otherwise, start from main | |
git checkout origin/$BRANCH_NAME || true | |
git checkout -b $BRANCH_NAME | |
yq -i '.api.image.tag = "'$IMG_TAG'"' charts/afn/values.yaml | |
git config --global user.email "devnull@semaan.ca" | |
git config --global user.name "Semaan Bot" | |
git commit --allow-empty -a -m "bump $CHANGE_TYPE to $IMG_TAG" | |
git push origin HEAD | |
(gh pr list --state open --limit 1 --head $BRANCH_NAME | grep $BRANCH_NAME) || \ | |
gh pr create -B main -H $BRANCH_NAME --title "Update images $BRANCH_ID" --body 'Created by Github action' | |