diff --git a/.github/workflows/cd_manual.yaml b/.github/workflows/cd_manual.yaml new file mode 100644 index 0000000..0d54a01 --- /dev/null +++ b/.github/workflows/cd_manual.yaml @@ -0,0 +1,47 @@ +name: "CD" + +on: + workflow_dispatch: + inputs: + TAG: + required: true + description: 'Docker container tag' + default: staging + +env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + +jobs: + build_an_push: + name: Build and Push a docker image + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set env + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: sidecache + IMAGE_TAG: ${{ github.event.inputs.TAG || env.RELEASE_VERSION }} + run: | + # Build a docker container and push it to ECR + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg release_version=${IMAGE_TAG} . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" diff --git a/pkg/lock/redis.go b/pkg/lock/redis.go index 7db880d..7b86d72 100644 --- a/pkg/lock/redis.go +++ b/pkg/lock/redis.go @@ -2,14 +2,17 @@ package lock import ( "errors" + "sync" + "time" + "github.com/google/uuid" "github.com/zeriontech/sidecache/pkg/cache" - "time" ) type RedisLock struct { - redis *cache.RedisRepository + redis *cache.RedisRepository values map[string]string + mutex sync.Mutex } const UnlockScript = ` @@ -25,12 +28,18 @@ func NewRedisLock(redis *cache.RedisRepository) *RedisLock { } func (lock *RedisLock) Acquire(key string, ttl time.Duration) error { + lock.mutex.Lock() + defer lock.mutex.Unlock() + val := uuid.NewString() lock.values[key] = val return lock.redis.SetNX(key, val, ttl) } func (lock *RedisLock) Release(key string) error { + lock.mutex.Lock() + defer lock.mutex.Unlock() + val, ok := lock.values[key] if !ok { return errors.New("unknown key")