Skip to content

Commit

Permalink
Extract build/deploy into actions reusable by both test/prod pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
varesa committed Apr 12, 2024
1 parent 8a9f799 commit d3980ff
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 103 deletions.
62 changes: 62 additions & 0 deletions .github/build-action/action.yml
@@ -0,0 +1,62 @@
name: build-action

inputs:
registry_username:
required: true
registry_password:
required: true

runs:
using: composite
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Check if commit already pushed to registry
id: check_registry
shell: bash
run: |
python3 -c "
import sys
import urllib.request
import json
import base64
with urllib.request.urlopen('https://ghcr.io/token?scope=repository:wappuradio/webbi:pull') as f:
token = json.load(f)['token']
req = urllib.request.Request(
'https://ghcr.io/v2/wappuradio/webbi/tags/list',
headers={'Authorization': f'Bearer {token}'}
)
with urllib.request.urlopen(req) as f:
if sys.argv[1] in json.load(f)['tags']:
print('exists=true')
else:
print('exists=false')" \
\
"${{ github.sha }}" \
>> $GITHUB_OUTPUT
- name: Log in to registry
if: steps.check_registry.outputs.exists == 'false'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ inputs.registry_username }}
password: ${{ inputs.registry_password }}

- name: Build and push
if: steps.check_registry.outputs.exists == 'false'
id: docker
uses: docker/build-push-action@v5
with:
push: true
tags: |
ghcr.io/wappuradio/webbi:${{ github.ref_name }}
ghcr.io/wappuradio/webbi:${{ github.sha }}
outputs:
image:
value: ghcr.io/wappuradio/webbi:${{ github.sha }}
23 changes: 23 additions & 0 deletions .github/deploy-action/action.yml
@@ -0,0 +1,23 @@
name: deploy-action

inputs:
kubeconfig:
required: true
image:
required: true
env:
required: true

runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: tale/kubectl-action@v1
with:
base64-kube-config: "${{ inputs.kubeconfig }}"

- name: Apply deployment.yaml
shell: bash
run: cat deployment.yaml | sed 's;IMAGE;${{ inputs.image }};' | sed 's/ENV/${{ inputs.env }}/' | kubectl apply -n webbi-${{ inputs.env }} -f -
62 changes: 11 additions & 51 deletions .github/workflows/prod.yml
Expand Up @@ -9,54 +9,13 @@ jobs:
build:
runs-on: actions-runner
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Check if commit already pushed to registry
id: check_registry
run: |
python3 -c "
import sys
import urllib.request
import json
import base64
with urllib.request.urlopen('https://ghcr.io/token?scope=repository:wappuradio/webbi:pull') as f:
token = json.load(f)['token']
req = urllib.request.Request(
'https://ghcr.io/v2/wappuradio/webbi/tags/list',
headers={'Authorization': f'Bearer {token}'}
)
with urllib.request.urlopen(req) as f:
if sys.argv[1] in json.load(f)['tags']:
print('exists=true')
else:
print('exists=false')" \
\
"${{ github.sha }}" \
>> $GITHUB_OUTPUT
- name: Build and push
if: steps.check_registry.outputs.exists == 'false'
id: docker
uses: docker/build-push-action@v5
- name: Checkout
uses: actions/checkout@v4
- name: build-action
uses: ./.github/build-action
with:
push: true
tags: |
ghcr.io/wappuradio/webbi:${{ github.ref_name }}
ghcr.io/wappuradio/webbi:${{ github.sha }}
outputs:
image: ghcr.io/wappuradio/webbi:${{ github.sha }}
registry_username: ${{ github.actor }}
registry_password: ${{ github.token }}

deploy:
runs-on: actions-runner
Expand All @@ -67,9 +26,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: tale/kubectl-action@v1
- name: deploy-action
uses: ./.github/deploy-action
with:
base64-kube-config: ${{ secrets.KUBECONFIG_BASE64 }}
kubeconfig: "${{ secrets.KUBECONFIG_BASE64 }}"
image: "ghcr.io/wappuradio/webbi:${{ github.sha }}"
env: "prod"

- run: cat deployment.yaml | sed 's;IMAGE;${{ needs.build.outputs.image }};' | sed 's/ENV/prod/' | kubectl apply -n webbi-prod -f -
63 changes: 11 additions & 52 deletions .github/workflows/test.yml
Expand Up @@ -9,54 +9,13 @@ jobs:
build:
runs-on: actions-runner
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Check if commit already pushed to registry
id: check_registry
run: |
python3 -c "
import sys
import urllib.request
import json
import base64
with urllib.request.urlopen('https://ghcr.io/token?scope=repository:wappuradio/webbi:pull') as f:
token = json.load(f)['token']
req = urllib.request.Request(
'https://ghcr.io/v2/wappuradio/webbi/tags/list',
headers={'Authorization': f'Bearer {token}'}
)
with urllib.request.urlopen(req) as f:
if sys.argv[1] in json.load(f)['tags']:
print('exists=true')
else:
print('exists=false')" \
\
"${{ github.sha }}" \
>> $GITHUB_OUTPUT
- name: Build and push
if: steps.check_registry.outputs.exists == 'false'
id: docker
uses: docker/build-push-action@v5
- name: Checkout
uses: actions/checkout@v4
- name: build-action
uses: ./.github/build-action
with:
push: true
tags: |
ghcr.io/wappuradio/webbi:${{ github.ref_name }}
ghcr.io/wappuradio/webbi:${{ github.sha }}
outputs:
image: ghcr.io/wappuradio/webbi:${{ github.sha }}
registry_username: ${{ github.actor }}
registry_password: ${{ github.token }}

deploy:
runs-on: actions-runner
Expand All @@ -67,9 +26,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: tale/kubectl-action@v1
- name: deploy-action
uses: ./.github/deploy-action
with:
base64-kube-config: ${{ secrets.KUBECONFIG_BASE64 }}

- run: cat deployment.yaml | sed 's;IMAGE;${{ needs.build.outputs.image }};' | sed 's/ENV/test/' | kubectl apply -n webbi-test -f -
kubeconfig: "${{ secrets.KUBECONFIG_BASE64 }}"
image: "ghcr.io/wappuradio/webbi:${{ github.sha }}"
env: "test"

0 comments on commit d3980ff

Please sign in to comment.