Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deployment workflow to replace use of long-lived credentials #496

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions .github/workflows/tag-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ env:
jobs:
push-to-staging:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
if: ${{ github.event.label.name == 'staging:request' }}
steps:
- uses: actions/checkout@v3
Expand All @@ -20,32 +23,32 @@ jobs:
for number in $(gh pr list --label "staging:active" --json number | jq -r '.[].number'); do
gh pr edit ${number} --remove-label "staging:active"
done
- name: Build
run: docker build -t foo .
- name: Push to ECR
id: ecr
uses: jwalton/gh-ecr-push@v1
- uses: aws-actions/configure-aws-credentials@v2
with:
access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
region: eu-west-2
local-image: foo
image: ${ECR_NAME}:${{ github.sha }}
- name: Update image tag and branch name
run: export IMAGE_TAG=${{ github.sha }} && export BRANCH=staging && cat kubernetes-deploy-staging.tpl | envsubst > kubernetes-deploy-staging.yaml
- name: Authenticate to the cluster
role-to-assume: ${{ secrets.ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.ECR_REGION }}
- uses: aws-actions/amazon-ecr-login@v1
id: login-ecr
- run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
cat kubernetes-deploy-staging.tpl | envsubst > kubernetes-deploy-staging.yaml
env:
KUBE_CERT: ${{ secrets.KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
run: |
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
BRANCH: "staging"
- run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
- name: Apply the updated manifest
run: |
kubectl -n ${KUBE_NAMESPACE} apply -f kubernetes-deploy-staging.yaml
kubectl -n ${KUBE_NAMESPACE} apply -f kubernetes-deploy.yaml
env:
KUBE_CERT: ${{ secrets.KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
- name: Update PR with success
run: |
gh pr comment $PRNUM --body "🚀 Deploying to [staging environment](https://moj-frontend-staging.apps.live.cloud-platform.service.justice.gov.uk/)"
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-deploy-staging.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
spec:
containers:
- name: prototype
image: 754256621582.dkr.ecr.eu-west-2.amazonaws.com/${ECR_NAME}:${IMAGE_TAG}
image: ${REGISTRY}/${REPOSITORY}:${IMAGE_TAG}
env:
- name: USERNAME
valueFrom:
Expand Down