Build Docker image and push to DockerHub #68
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: Build Docker image and push to DockerHub | |
on: | |
push: | |
branches: | |
- master | |
tags: | |
- v* | |
jobs: | |
build-and-push: | |
runs-on: self-hosted-amd64-1cpu | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Import secrets | |
uses: hashicorp/vault-action@v2 | |
id: secrets | |
with: | |
exportEnv: false | |
url: ${{ secrets.VAULT_URL }} | |
role: ${{ secrets.VAULT_ROLE }} | |
method: kubernetes | |
path: kubernetes-ci | |
secrets: | | |
kv-gitlab-ci/data/github/shared/dockerhub-creds user | DOCKERHUB_USER ; | |
kv-gitlab-ci/data/github/shared/dockerhub-creds password | DOCKERHUB_PASSWORD ; | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ steps.secrets.outputs.DOCKERHUB_USER }} | |
password: ${{ steps.secrets.outputs.DOCKERHUB_PASSWORD }} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: wallarm/gotestwaf | |
tags: | | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=raw,value=edge,enable={{is_default_branch}} | |
- name: Build and push | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
clean-old-cache: | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Delete old cached docker layers | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
set -eu -o pipefail | |
LAST_WORKFLOW_TIME=$( | |
gh run list \ | |
--json workflowName,startedAt \ | |
--jq ".[] | select( .workflowName == \"${{ github.workflow }}\") | .startedAt" \ | |
| head -n 1 | |
) | |
echo "Time of the last running '${{ github.workflow }}' workflow: $LAST_WORKFLOW_TIME" | |
while true; do | |
OLD_CACHE_IDS=$( | |
gh api \ | |
-H "Accept: application/vnd.github+json" \ | |
--jq ".actions_caches[] | select(.last_accessed_at < \"$LAST_WORKFLOW_TIME\") | .id" \ | |
/repos/wallarm/gotestwaf/actions/caches \ | |
| tr '\n' ' ' | |
) | |
if [ -z "$OLD_CACHE_IDS" ]; then | |
echo "Done" | |
break | |
fi | |
echo "ID of caches to delete: $OLD_CACHE_IDS" | |
for cache_id in $OLD_CACHE_IDS; do | |
gh api \ | |
--method DELETE \ | |
-H "Accept: application/vnd.github+json" \ | |
/repos/wallarm/gotestwaf/actions/caches/$cache_id | |
done | |
done |