Skip to main content

CI/CD and automation · Intermediate · 24 min

Deploy from GitLab CI with environments, protected variables and manual jobs

By Padding · Updated 18 Nov 2026
GitLab 19.6 · Ubuntu 26.04 · OpenSSH and rsync from Ubuntu · check-jsonschema 0.39.0

You will have a GitLab pipeline that deploys a small site to review, staging and production environments over SSH, with a separate protected, masked file-type key for each environment, a manual production job that never overlaps another deploy, a stop job for review apps and an AWS upload that uses a short-lived ID token instead of a stored key.

On this page

A pipeline that can deploy holds the keys to your servers, and every branch can change the pipeline file. GitLab gives you three tools to keep those keys where they belong. Environments name the places you deploy to and record every deployment. CI/CD variables can be protected, so their values only reach pipelines on protected branches, and scoped to one environment, so a staging job never sees the production key. Jobs can be manual, so production waits for a person, and can share a resource group, so two deploys never write to the same place at once.

In this tutorial you deploy a one-page site called Tally to one Ubuntu server, in three places: a review app for each merge request, staging on every merge to main, and production when someone presses the play button. Each place gets its own server user and its own SSH key. At the end you add a job that uploads each release to Amazon S3 with a short-lived token GitLab issues per job, so no AWS key is stored in GitLab at all.

Everything on your computer lives in ~/gitlab-deploy-demo: the site repository in site/ and the keys in keys/, outside the repository so they can never be committed. Fences are labelled with where they run: your computer or the server.

Step 1 · Create the site and its build script

On your computer, create the repository and a build script. The script writes public/index.html and stamps it with the commit it was built from, so you can see at a glance which version each environment is running:

Bash
mkdir -p ~/gitlab-deploy-demo/site/scripts ~/gitlab-deploy-demo/keys
chmod 700 ~/gitlab-deploy-demo/keys
cd ~/gitlab-deploy-demo/site
git init
cat > scripts/build.sh <<'SH'
#!/bin/sh
set -eu
rm -rf public
mkdir public
cat > public/index.html <<HTML
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Tally</title></head>
<body>
<h1>Tally</h1>
<p>Build ${CI_COMMIT_SHORT_SHA:-local}</p>
</body>
</html>
HTML
echo "Built public/ for commit ${CI_COMMIT_SHORT_SHA:-local}"
SH
printf 'public/\n' > .gitignore
sh scripts/build.sh

CI_COMMIT_SHORT_SHA is set by GitLab in every job. On your computer it is unset, so the page says local.

Check it worked

Bash
cd ~/gitlab-deploy-demo/site
grep -c '<p>Build local</p>' public/index.html

The script printed Built public/ for commit local, and grep counts 1.

Step 2 · Give each environment its own user on the server

On the server, as your sudo user. Make three users with no password, one per environment, and a directory each one owns. A key that can log in as deploy-review can then write review apps and nothing else: it cannot touch staging or production, even if it leaks.

Bash
sudo apt update
sudo apt install -y rsync
for env in review staging production; do
  sudo adduser --disabled-password --gecos "" "deploy-$env"
  sudo install -d -m 755 -o "deploy-$env" -g "deploy-$env" "/srv/www/$env"
done

Your web server then serves /srv/www/staging and /srv/www/production on their own names, and /srv/www/review/<branch> on a wildcard name such as *.review.example.com. /tutorials/host-multiple-sites-with-nginx-server-blocks shows the Nginx side; this tutorial covers getting the files there.

Check it worked

Bash
ls -ld /srv/www/review /srv/www/staging /srv/www/production

Each directory is listed with its own owner: deploy-review, deploy-staging and deploy-production.

Step 3 · Make one key per environment and record the server's host key

On your computer. Make three key pairs with no passphrase (a CI job has no one to type it), and name each after its environment:

Bash
cd ~/gitlab-deploy-demo/keys
for env in review staging production; do
  ssh-keygen -q -t ed25519 -N "" -C "gitlab-deploy-$env" -f "$env"
done
ls

Print the three public keys. You will paste each line on the server:

Bash
cat ~/gitlab-deploy-demo/keys/*.pub

On the server, put each key in place for its user. Replace the three values with the lines you just printed. restrict in front of a key turns off port forwarding, agent forwarding and terminals for that key, which a deploy never needs. The install line creates each user's authorized_keys as an empty private file first; these users are new, so there is nothing in it to lose:

Bash
REVIEW_PUB='ssh-ed25519 AAAA...replace-this gitlab-deploy-review'
STAGING_PUB='ssh-ed25519 AAAA...replace-this gitlab-deploy-staging'
PRODUCTION_PUB='ssh-ed25519 AAAA...replace-this gitlab-deploy-production'
Bash
authorise() {
  sudo install -d -m 700 -o "$1" -g "$1" "/home/$1/.ssh"
  sudo install -m 600 -o "$1" -g "$1" /dev/null "/home/$1/.ssh/authorized_keys"
  printf 'restrict %s\n' "$2" | sudo tee "/home/$1/.ssh/authorized_keys" >/dev/null
}
authorise deploy-review "$REVIEW_PUB"
authorise deploy-staging "$STAGING_PUB"
authorise deploy-production "$PRODUCTION_PUB"
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

The last line prints the server's own host key fingerprint, starting 256 SHA256:. Keep it on screen.

Back on your computer, set the server's name (replace it) and fetch its host key. GitLab's runners will use this file to check they are talking to your server, so fetch it here, from a network you trust, and never inside a job:

Bash
DEPLOY_HOST=deploy.example.com
Bash
cd ~/gitlab-deploy-demo/keys
ssh-keyscan -t ed25519 "$DEPLOY_HOST" > known_hosts
ssh-keygen -lf known_hosts

GitLab only accepts Masked for a value that is one line with no spaces, and a private key file has many lines. Encode each private key as a single line of base64, which has no spaces. The pipeline decodes it again before use. tr -d '\n' makes this work the same on macOS and Linux:

Bash
cd ~/gitlab-deploy-demo/keys
for env in review staging production; do
  base64 < "$env" | tr -d '\n' > "$env.b64"
done
ls -l

The .b64 files are the private keys in another form. Treat them exactly like the keys.

Check it worked

The fingerprint from ssh-keygen -lf known_hosts matches the one the server printed, character for character. If it does not, stop: something between you and the server answered instead. Then check one encoded key is maskable and still decodes to a working key:

Bash
cd ~/gitlab-deploy-demo/keys
wc -l < staging.b64
grep -c ' ' staging.b64 || true
base64 -d < staging.b64 | cmp - staging && echo "decodes to the same key"

You see 0 (no line breaks at all), 0 (no spaces), then decodes to the same key.

Step 4 · Store the keys as protected, masked file variables

In GitLab, first check that main is protected: Settings, then Repository, then Protected branches. The default branch is protected when the project is created. A protected variable reaches only pipelines that run on protected branches or protected tags, so this list decides which branches can ever see your staging and production keys.

Then open Settings, then CI/CD, then Variables, and choose Add variable for each row below. For a file variable, paste the contents of the named file as the value.

| Key | Value | Type | Environments | Visibility | Protect variable |
| --- | --- | --- | --- | --- | --- |
| DEPLOY_HOST | your server's name | Variable | All | Visible | off |
| SSH_KNOWN_HOSTS | contents of keys/known_hosts | File | All | Visible | off |
| DEPLOY_KEY | contents of keys/staging.b64 | File | staging | Masked | on |
| DEPLOY_KEY | contents of keys/production.b64 | File | production | Masked | on |
| DEPLOY_KEY | contents of keys/review.b64 | File | review/* | Masked | off |
| DEPLOY_USER | deploy-staging | Variable | staging | Visible | off |
| DEPLOY_USER | deploy-production | Variable | production | Visible | off |
| DEPLOY_USER | deploy-review | Variable | review/* | Visible | off |

What the settings do:

  • Type: File makes GitLab write the value to a temporary file in the job and set the variable to that file's path. The key never sits in the job's environment as text, and a tool that wants a file gets one.
  • Environments scopes a value to jobs whose environment:name matches. Three variables share the name DEPLOY_KEY, and each job gets only the one for its environment. review/* matches every review app.
  • Masked hides the value if it is ever printed in a job log. GitLab refuses the setting for a value with spaces or line breaks, which is why you encoded the keys. Masked and hidden also stops anyone reading the value in the settings later; you can only choose it when you create a variable.
  • Protect variable on the staging and production keys means a merge request from any branch gets nothing for them, even if someone edits the pipeline to name environment: production. The review key is not protected, because review apps run in merge request pipelines on unprotected branches. Anyone who can push a branch could change the pipeline and use that key, which is why it can write only /srv/www/review.

On GitLab Premium and Ultimate you can also protect the production environment itself, so only named people can run its jobs, and require approvals. On the Free tier, protected branches and variables are the gate.

Check it worked

The variables list shows DEPLOY_KEY three times with the environments production, review/* and staging, a File label on the keys and known hosts, and the protected and masked marks on the staging and production keys. If GitLab refused to save a key as masked, the value has a line break in it: paste the .b64 file again rather than the private key.

Step 5 · Write the deploy script and try it by hand

On your computer. The deploy script is the same for every environment; the job's variables decide where it goes. It decodes the key into a private temporary file, deletes it on exit, and copies public/ with rsync. StrictHostKeyChecking=yes with the known hosts file makes SSH refuse any server whose host key does not match:

Bash
cd ~/gitlab-deploy-demo/site
cat > scripts/deploy.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail

target="${1:?usage: deploy.sh /srv/www/<directory>}"
: "${DEPLOY_HOST:?DEPLOY_HOST is not set}"
: "${DEPLOY_USER:?DEPLOY_USER is not set}"
: "${DEPLOY_KEY:?DEPLOY_KEY is not set}"
: "${SSH_KNOWN_HOSTS:?SSH_KNOWN_HOSTS is not set}"
if [ ! -f public/index.html ]; then
  echo "public/index.html is missing: run the build first" >&2
  exit 1
fi

key="$(mktemp)"
trap 'rm -f "$key"' EXIT
base64 -d < "$DEPLOY_KEY" > "$key"

ssh_cmd="ssh -i $key -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$SSH_KNOWN_HOSTS"
rsync -a --delete -e "$ssh_cmd" public/ "$DEPLOY_USER@$DEPLOY_HOST:$target/"
echo "Deployed ${CI_COMMIT_SHORT_SHA:-local} to $DEPLOY_USER@$DEPLOY_HOST:$target"
SH

mktemp creates the file readable only by you, so the decoded key is private from the moment it exists.

Now do by hand what a staging job does. In a job, GitLab points DEPLOY_KEY and SSH_KNOWN_HOSTS at temporary files; here you point them at your own files:

Bash
cd ~/gitlab-deploy-demo/site
export DEPLOY_HOST
export DEPLOY_USER=deploy-staging
export DEPLOY_KEY=~/gitlab-deploy-demo/keys/staging.b64
export SSH_KNOWN_HOSTS=~/gitlab-deploy-demo/keys/known_hosts
bash scripts/deploy.sh /srv/www/staging

Check it worked

The script prints Deployed local to deploy-staging@...:/srv/www/staging. Read the page back over SSH with the same key:

Bash
cd ~/gitlab-deploy-demo/keys
ssh -i staging -o IdentitiesOnly=yes -o UserKnownHostsFile=known_hosts "deploy-staging@$DEPLOY_HOST" cat /srv/www/staging/index.html | grep '<p>Build'

It prints <p>Build local</p>. Now prove the staging key cannot reach production:

Bash
cd ~/gitlab-deploy-demo/site
bash scripts/deploy.sh /srv/www/production || echo "deploy refused, exit status $?"
unset DEPLOY_USER DEPLOY_KEY

rsync reports Permission denied for /srv/www/production, and you see deploy refused with a non-zero status. This is the separation the scoped variables give you in the pipeline.

Step 6 · Write the pipeline with staging, a manual production job and resource groups

Create .gitlab-ci.yml at the root of the repository:

Bash
cd ~/gitlab-deploy-demo/site
cat > .gitlab-ci.yml <<'YAML'
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

stages:
  - build
  - review
  - staging
  - production

default:
  image: ubuntu:26.04

build:
  stage: build
  script:
    - sh scripts/build.sh
  artifacts:
    paths:
      - public/
    expire_in: 1 week

.deploy:
  needs: [build]
  before_script:
    - apt-get update -qq
    - apt-get install -y -qq openssh-client rsync > /dev/null

deploy_staging:
  extends: .deploy
  stage: staging
  script:
    - bash scripts/deploy.sh /srv/www/staging
  environment:
    name: staging
    url: https://staging.example.com
  resource_group: staging
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

deploy_production:
  extends: .deploy
  stage: production
  needs: [build, deploy_staging]
  script:
    - bash scripts/deploy.sh /srv/www/production
  environment:
    name: production
    url: https://www.example.com
    deployment_tier: production
  resource_group: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
YAML

Replace the two url values with your own addresses. How the deploy parts work:

  • environment:name ties each job to an environment. GitLab creates the environment on the first deploy, lists it under Operate, then Environments, with a link to url, and keeps a history of every deployment, so you can see what is live and redeploy an earlier one. It also decides which scoped DEPLOY_KEY and DEPLOY_USER the job receives.
  • .deploy starts with a dot, so it is a template, not a job. extends: .deploy copies its needs and before_script into both deploy jobs. deploy_production sets its own needs, which replaces the template's list: it waits for staging and still downloads public/ from build.
  • when: manual inside rules puts a play button on deploy_production instead of running it. Inside rules, a manual job defaults to allow_failure: false, so the pipeline shows as blocked until someone runs it, and the commit does not look finished while production is behind. If you would rather the pipeline show as passed while production waits, add allow_failure: true to the rule.
  • resource_group: production means only one job in that group runs at a time across all pipelines. Press play on two pipelines at once and the second waits for the first, so two rsyncs never write to /srv/www/production together. By default GitLab does not promise which waiting job runs next; the resource group's process_mode (set through the API) can make it oldest first.
  • The deploy jobs only exist on main, which is protected, so the protected keys can reach them.

Check it worked

Bash
cd ~/gitlab-deploy-demo/site
check-jsonschema --builtin-schema vendor.gitlab-ci .gitlab-ci.yml

It prints ok -- validation done. The schema checks the file's shape. GitLab's Pipeline editor, under Build, is the final check on rules such as needs naming a real job.

Step 7 · Add review apps and a stop job

A review app deploys each merge request to its own address, so a reviewer can open the change instead of imagining it. That helps when the people checking a change are not developers, or are checking it on an inexpensive Android phone over mobile data: they open one link from the merge request. Add two jobs to the end of the file:

Bash
cd ~/gitlab-deploy-demo/site
cat >> .gitlab-ci.yml <<'YAML'

review_app:
  extends: .deploy
  stage: review
  script:
    - bash scripts/deploy.sh "/srv/www/review/$CI_COMMIT_REF_SLUG"
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    url: https://$CI_COMMIT_REF_SLUG.review.example.com
    on_stop: stop_review_app
    auto_stop_in: 3 days
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

stop_review_app:
  stage: review
  needs: []
  variables:
    GIT_STRATEGY: none
  before_script:
    - apt-get update -qq
    - apt-get install -y -qq openssh-client > /dev/null
  script:
    - key="$(mktemp)"
    - base64 -d < "$DEPLOY_KEY" > "$key"
    - ssh -i "$key" -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$SSH_KNOWN_HOSTS" "$DEPLOY_USER@$DEPLOY_HOST" "rm -rf /srv/www/review/$CI_COMMIT_REF_SLUG"
    - rm -f "$key"
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    action: stop
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: manual
      allow_failure: true
YAML

How it fits together:

  • review/$CI_COMMIT_REF_SLUG gives each branch its own environment, grouped under review on the Environments page. CI_COMMIT_REF_SLUG is the branch name lowercased, with anything other than a-z and 0-9 turned into -, so it is safe in a path, a host name and the rm -rf command.
  • on_stop names the job that tears the environment down. GitLab runs it when the merge request is merged or its branch is deleted, when someone presses Stop on the environment, or when auto_stop_in runs out (three days here, checked by a background task about once an hour). The stop job must have the same rules as the deploy job and environment:action: stop.
  • GIT_STRATEGY: none skips the checkout, because the branch may already be deleted when the job runs. That is why the stop job carries its commands inline instead of calling a script from the repository.
  • allow_failure: true matters here. Without it, a manual job in rules would block every merge request pipeline until someone stopped the review app.

Check it worked

Validate the file, then do by hand what the two jobs do: deploy a review app for a branch called add-totals with the review key, and remove it again:

Bash
cd ~/gitlab-deploy-demo/site
check-jsonschema --builtin-schema vendor.gitlab-ci .gitlab-ci.yml
export DEPLOY_USER=deploy-review
export DEPLOY_KEY=~/gitlab-deploy-demo/keys/review.b64
CI_COMMIT_REF_SLUG=add-totals
bash scripts/deploy.sh "/srv/www/review/$CI_COMMIT_REF_SLUG"
ssh -i ~/gitlab-deploy-demo/keys/review -o IdentitiesOnly=yes -o UserKnownHostsFile="$SSH_KNOWN_HOSTS" "$DEPLOY_USER@$DEPLOY_HOST" "ls /srv/www/review"
ssh -i ~/gitlab-deploy-demo/keys/review -o IdentitiesOnly=yes -o UserKnownHostsFile="$SSH_KNOWN_HOSTS" "$DEPLOY_USER@$DEPLOY_HOST" "rm -rf /srv/www/review/$CI_COMMIT_REF_SLUG && ls -A /srv/www/review | wc -l"
unset DEPLOY_USER DEPLOY_KEY CI_COMMIT_REF_SLUG

The validator prints ok -- validation done, the first ls lists add-totals, and after the removal the count is 0.

Step 8 · Reach AWS with an ID token instead of a stored key

The deploy keys above are long-lived secrets. For a cloud provider you can avoid storing anything: GitLab issues each job a signed JSON Web Token (an ID token) on request, and the cloud trusts tokens from your project and branch in exchange for credentials that expire within the hour. Here a job archives each production release to an S3 bucket that way. This step creates AWS resources: S3 bills for storage and requests, so remove them when you finish (the last step shows how).

The token's sub claim names the project, the ref type and the ref, in the form project_path:your-group/tally:ref_type:branch:ref:main. The role's trust policy accepts a token only when that claim matches, so a token from any other branch is refused. Write it on your computer; replace 111122223333 with your AWS account ID and your-group/tally with your project's path. On a self-managed GitLab, use your instance's host name in place of gitlab.com throughout.

Bash
mkdir -p ~/gitlab-deploy-demo/aws
cd ~/gitlab-deploy-demo/aws
cat > trust-policy.json <<'JSON'
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "Federated": "arn:aws:iam::111122223333:oidc-provider/gitlab.com" },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "gitlab.com:aud": "sts.amazonaws.com",
          "gitlab.com:sub": "project_path:your-group/tally:ref_type:branch:ref:main"
        }
      }
    }
  ]
}
JSON
cat > upload-policy.json <<'JSON'
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::tally-releases-replace-this/releases/*"
    }
  ]
}
JSON
python3 -m json.tool trust-policy.json > /dev/null && python3 -m json.tool upload-policy.json > /dev/null && echo "both policies are valid JSON"

The role can only add objects under releases/ in one bucket. Replace tally-releases-replace-this with a bucket name of your own (names are global across AWS). Create the bucket, the identity provider, the role and its policy with the AWS CLI, signed in as an administrator on your computer:

Bash
cd ~/gitlab-deploy-demo/aws
aws s3 mb s3://tally-releases-replace-this --region eu-west-1
aws iam create-open-id-connect-provider --url https://gitlab.com --client-id-list sts.amazonaws.com
aws iam create-role --role-name gitlab-tally-releases --assume-role-policy-document file://trust-policy.json
aws iam put-role-policy --role-name gitlab-tally-releases --policy-name upload-releases --policy-document file://upload-policy.json

If your account already has a gitlab.com provider, skip the second command. In GitLab, add two plain, visible variables with no environment scope: AWS_ROLE_ARN set to the role's ARN (arn:aws:iam::111122223333:role/gitlab-tally-releases) and RELEASE_BUCKET set to the bucket name. Neither is a secret. Then add the job to the end of the pipeline:

Bash
cd ~/gitlab-deploy-demo/site
cat >> .gitlab-ci.yml <<'YAML'

archive_release:
  stage: production
  image:
    name: amazon/aws-cli:2.42.4
    entrypoint: [""]
  needs: [build, deploy_production]
  id_tokens:
    AWS_ID_TOKEN:
      aud: sts.amazonaws.com
  variables:
    AWS_DEFAULT_REGION: eu-west-1
  script:
    - token_file="$(mktemp)"
    - printf '%s' "$AWS_ID_TOKEN" > "$token_file"
    - export AWS_WEB_IDENTITY_TOKEN_FILE="$token_file"
    - export AWS_ROLE_SESSION_NAME="gitlab-$CI_PIPELINE_ID"
    - aws s3 cp public "s3://$RELEASE_BUCKET/releases/$CI_COMMIT_SHORT_SHA/" --recursive
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
YAML

id_tokens asks GitLab for a token with the audience sts.amazonaws.com and puts it in AWS_ID_TOKEN. The AWS CLI sees AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE together, swaps the token for temporary credentials and uses them; nothing in GitLab could be copied and reused next week. entrypoint: [""] is needed because the image's own entrypoint is the aws command, which would swallow the job's shell. The job waits for deploy_production, so it archives only what reached production.

Check it worked

Bash
cd ~/gitlab-deploy-demo/site
check-jsonschema --builtin-schema vendor.gitlab-ci .gitlab-ci.yml
grep -c 'environment:' .gitlab-ci.yml

The validator prints ok -- validation done, and grep counts 4 jobs that name an environment. After the first production deploy, aws s3 ls s3://tally-releases-replace-this/releases/ on your computer lists one folder named after the commit's short SHA, holding index.html.

Step 9 · Push, deploy and stop a review app

Commit the pipeline and the scripts. The keys are in ~/gitlab-deploy-demo/keys, outside the repository, so git status never offers them:

Bash
cd ~/gitlab-deploy-demo/site
git add .gitignore .gitlab-ci.yml scripts
git commit -m "Deploy Tally to review, staging and production"
git status --short | wc -l

The last line prints 0: everything is committed. Add your GitLab project as the remote and push. Replace your-group/tally:

Bash
git remote add origin git@gitlab.com:your-group/tally.git
git push -u origin main

Open Build, then Pipelines. On main you see build, then deploy_staging running on its own, then deploy_production with a play button, and the pipeline marked blocked. Open the staging URL and check the page shows the commit's short SHA. Press play on deploy_production; when it passes, archive_release runs.

Then open a merge request from a branch, for example after changing the heading in scripts/build.sh. Its pipeline runs build and review_app, and the merge request shows a link to the review app. Press Stop on that environment under Operate, then Environments, or merge the request, and stop_review_app removes the directory.

Check it worked

Operate, then Environments lists staging and production, each with the commit it is running and a link, and a review folder holding one environment per open merge request. On the server, ls /srv/www/review is empty once the merge request is merged.

Clean up

If you set this up only to learn, remove everything, because the keys and the S3 bucket outlive the tutorial. Delete the variables in Settings, then CI/CD, then Variables, stop and delete the environments on the Environments page, and on the server remove the users with their directories. This deletes the deployed sites, so check you are on the right server first:

Bash
for env in review staging production; do
  sudo deluser --remove-home "deploy-$env"
done
sudo rm -rf /srv/www/review /srv/www/staging /srv/www/production

In AWS, delete what the AWS step created (aws s3 rb with --force deletes every object in the bucket first):

Bash
aws s3 rb s3://tally-releases-replace-this --force
aws iam delete-role-policy --role-name gitlab-tally-releases --policy-name upload-releases
aws iam delete-role --role-name gitlab-tally-releases
aws iam delete-open-id-connect-provider --open-id-connect-provider-arn arn:aws:iam::111122223333:oidc-provider/gitlab.com

Skip the last command if other projects use the gitlab.com provider. Finally delete the keys on your computer:

Bash
rm -rf ~/gitlab-deploy-demo/keys

Next steps

  • The same staging-then-approval flow on GitHub, with required reviewers: /tutorials/require-approval-before-production-deploys-with-github-environments.
  • More on short-lived cloud credentials, including a tighter IAM setup: /tutorials/deploy-to-aws-from-github-actions-with-oidc.
  • A safer SSH deploy target that only accepts a release command: /tutorials/deploy-to-a-server-over-ssh-with-github-actions.
  • Review apps for every pull request with a hosting platform: /tutorials/create-preview-environments-for-pull-requests.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.